Many times in a program, you need a way to determine if a specific condition has been met. For example, you might need to know whether a part of your program executed properly. In such cases, you can use Boolean values, which are represented in Java by the boolean data type. Boolean values are unique in that they can be only one of two possible values: true or false.
You declare a boolean value like this:
boolean identifier;or
boolean identifier = value;In the second example, value must be true or false. In an actual program, you might write something like this:
boolean file_okay = true;
Following are some statements which give you some
ideas of how else the boolean value can be used in your Java programs.
They demonstrate that a boolean value does not have to be assigned
true or false
explicitly, as these two values can be obtained from the execution
of other statements.
A simple if statement includes the keyword
if followed by a logical expression, which, as you learned
above, is an expression that evaluates to either
true or false.
In the following example,
statements gets executed if the value of
boolean_expressionis true.
int total = 15;
boolean answer3 = (total == 15); // answer3 now contains the value 'true'
boolean answer4 = (total != 15); // answer4 now contains the value 'false'
boolean answer5 = (total < 15); // answer5 now contains the value 'false'
boolean answer6 = (total <= 15); // answer6 now contains the value 'true'
boolean answer7 = (total > 15); // answer7 now contains the value 'false'
boolean answer8 = (total >= 15); // answer8 now contains the value 'true'
boolean answer9 = (total == 15 / 3 * 2); // figure this out yourself.
boolean answer9 = (total != 10 - 12 % 7 * 2 / 5 + 3)// figure this out yourself.
2.The if and if-else Statement
Most conditional branching occurs when the program executes an if statement,
which compares data and decides what to do next based on the result of the
comparison. For example, you've probably seen programs that print menus
on-screen. To select a menu item, you often type the item's selection number.
When the program receives your input, it checks the number
you entered and decides what to do. You'd probably use an if
statement in this type of program.
if (boolean_expression)
statements;
If there are more than one statement to be executed, these expressions are
surrounded by parentheses (called "block"). You follow the parentheses
with the statement that you want executed if the logical expression is
true.
For example, look at this if statement:
if (age1 == 10)
{
System.out.println ("The age1 is 10");
System.out.println ("The person is a kid");
}
Having learned how to use an ' if'
statement, it is not difficult to understand the '
if-else' statement.
The syntax of The if-else Statement is as follows:
if (boolean_expression) statement1; else statement2;Examples:
if (age == 10) System.out.println ("The age is 10"); else System.out.println ("The age is not 10"); if (age1 == 10) { System.out.println ("The age1 is 10"); System.out.println ("The person is a kid"); } else { System.out.println ("The age1 is not 10"); System.out.println ("The person may not be a kid"); }Note that if's, and if-else 's can be nested and the rules for blocks mentioned for 'if' statement also apply for ' if-else' statements. Be sure that your braces match up.