How to Enter Boolean Expressions in Java

All programming languages can work with simple boolean values. A boolean value, unlike an integer or a character, has one of two states: true or false. Boolean expressions are like mathematical expressions, except that they have true/false boolean values instead of numbers, and logical operators (such as AND, OR and NOT) instead of operators like addition and multiplication. In the java programming language, entering boolean expressions is just a matter of learning the different logical operators.

Things You'll Need

  • Text editor
Show More

Instructions

    • 1

      Begin your boolean expression by declaring a boolean variable, followed by an equals sign. For example, you could type "boolean myBoolean =", without the quotation marks.

    • 2

      Continue the expression using logical operators, which are explained in steps three through eight, and the names of other boolean variables you've declared, or just the literal values TRUE and FALSE. At the end, type a ";" to finish the line and end the expression. The result of your boolean expression will be stored in the "myBoolean" variable declared in step one.

    • 3

      Use the AND operator by typing "&&". This returns TRUE only when both arguments (the boolean values on either side of the operator) are TRUE.

    • 4

      Use the OR operator by typing "||". This returns TRUE when either or both of the arguments are TRUE.

    • 5

      Use the NOT operator by typing "!". This returns the opposite value of the boolean value on its right side.

    • 6

      Use the operator "==" to test whether the arguments on either side are the same. If they are, it will return TRUE.

    • 7

      Use the operator "!=" to test whether the arguments on either side are different. If so, it returns TRUE.

    • 8

      Use parenthesis, "(" and ")", to control the order in which calculations happen, just like in a normal mathematical expression.

Tips & Warnings

  • Boolean expressions are great to use in conditional statements, such as "if/then" or "while" statements. You can put very complicated boolean expressions inside of them to control the flow of your program.

Related Searches:

References

Comments

Related Ads

Featured