So far, we have only seen
programs run straight from the start to finish. Hence, that is not always the
case. As a program
gets more and more complex, often time we would have to deal with decision
making. Without any further notice, let me introduce
two very important decision making tool that comes with java condition. The two tools are
the if statement
and switch statement.
The two
statements rely on the use of boolean expressions, which is true or false. The
boolean expression can be very complicated, however we use simple expressions
that compare the value of a variable with the value of some other variable. This
comparison uses one of the relational operators. All these operators are binary
operators which work on two operands.
| Table | Relational Operators |
| Operators | Descriptions |
| = = | Returns true if the expression on the left evaluates to the same value as the expression on the right |
| != | Returns true if the expression on the left does not evaluate to the same value as the expression on the right |
| < | Returns true if the expression on the left evaluates to a value that is less than the value of the expression on the right |
| <= | Returns true if the expression on the left evaluates to a value that is less than or equal to the expression on the right |
| > | Returns true if the expression on the left evaluates to a value that is greater than the value of the expression on the right |
| >= | Returns true if the expression on the left evaluates to a value that is greater than or equal to the expression on the right |
Example
Assume we have the following statements initialized
int i = 5;
int j = 10;
int k = 6;
now, let say we have the following:
Expression Value Explanation
i == 5 true The value of i is 5
i == 10 false The value of i is not 10.
i > 3 true i is 5, which is greater than 3
k > i + j false k is 6, i + j is 15
Note: the relational operator that test for
equality is two equal signs in a row ( == ). A single equal sign is the
assignment operator.
if( i == 5) is not the same as if ( i = 5)
Java if statements
In most cases, an if statement lets us execute
a single statement or a block of statements only if a boolean expression
evaluates to be true. The if statement has a basic form looks like this
if ( boolean - expression )
statement...
Example
int x = 0;
if ( y > 10 )
x = 4;
In the example above, we have x initialized to 0, then we will assign 4 to x only if the expression y > 10 is true.
Java if-else statements
An if-else statement adds an additional
element to a basic if statement. The else block execute if the boolean
expression
is not true. It's the basic following format.
if ( boolean-expression)
statement
else
statement
Example
int commission;
if( sales >= 1000)
commission = 0.03;
else
commission = 0.05;
In the example above, the commission is set to
3% if sales is greater than or equal to 1000. If the sales is less than 1000,
the commission is set to 5%.
We can have a nested if-else statement by
using the else-if statement. Basically, it's a series of if-else statements
with another if-else statements in each else part.
Example
int y = 0;
if ( x == 10)
y = 1;
else if ( x > 10)
y=3;
else if( x < 3)
y = 5;
else
y = 4;
Logic Operator
The two most often logic operator are && and ||. The && stands for
conditional And, and the || stands for conditional Or.
We will see some examples on how to use both of the logic operator later.
Java Comparing Strings
When comparing instead of using the relational operator ==, we use " equals "
Lets see some programming examples
ClickHere to download Grade.java
ClickHere to download ResGrade.java