Chapter 4: Decision Structures and Boolean Logic - Java Language Companion
Rlational Operators and the if statement
> Greater than
< Less than
>= Greater than or equal to
<= Less than or equal to
== Equal to
! = Not equal to
Format of if statement:
if (BooleanExpression)
{
statement;
statement;
etc;
}
If Boolean expression is TRUE, the statements inside the curly braces are executed. If FALSE, they’re skipped. (statements are conditionally executed)
If if statement has only ONE conditionally executed statement, doesn’t have to be in curly braces:
if (BooleanExpression)
statement;
Dual Alternative Decision Structures
Use if-else statement in Java to create a dual alternative decision structure. This is the format of the if-else statement:
if (BooleanExpression)
{
statement;
statement;
etc;
}
else
{
statement;
statement;
etc;
}
An if-else statement has two parts:
if clause
else clause
If TRUE, if clause is executed. If FALSE, else clause is executed
Has two sets of conditionally executed statements. One set executed only if Boolean expression is true, other set executed when it’s false. Both sets cannot be executed together.
If either set of conditionally executed statements contains only one statement, the curly braces aren’t required.
if (BooleanExpression)
statement;
else
statement;