Professional Documents
Culture Documents
Selection Control
Statements
Melgine M. Bauat, MSIT
Instructor
Topic Outline
if (booleanExpression) {
statement(s);
}
if ( i < 10 ) {
if (booleanExpression) {
statement(s);
} else {
statement(s);
}
Example:
if (booleanExpression) {
statement(s);
} else if (booleanExpression) {
statement
} else {
statement(s);
}
The IF-ELSEIF-ELSE Statement
The Nested if Statement
n1 >= n2
-1.0 >= 4.5
n2 >= n3
-> FALSE.
4.5 >= -5.3
Then,
The Program
will proceed
will proceed
to line 14
to (ELSE)
TRUE
ELSE because the IF in line 6 is
The IF-ELSE inside
Therefore OUTER
the largest
FALSE.
ELSE iswilthe
number bevalue
tested.
of n2
The program will not execute
Line 17
Largest to Line4.5
Number:
(4.5) 21
line 7 to line 13
Lets try to analyze this
The switch Statement
The switch-expression must yield a value of char, byte, short, or int type and must always be
enclosed in parentheses.
The value1, ..., and valueN must have the same data type as the value of the switch-expression.
The resulting statements in the case statement are executed when the value in the case statement
matches the value of the switch-expression. (The case statements are executed in sequential
order.)
The keyword break is optional, but it should be used at the end of each case in order to terminate
the remainder of the switch statement. If the break statement is not present, the next case
statement will be executed.
The switch Statement
switch (numOfYears) {
case 7: annualInterestRate = 7.25;
case 15: annualInterestRate = 8.50;
case 30: annualInterestRate = 9.0;
default: System.out.println(
"Wrong number of years“);
}
The switch Statement
Note: the default case in switch is like else in if else
The switch Statement
Note: the default case in switch is like else in if else
The switch Statement
Note: the default case in switch is like else in if else
The Conditional Operator
(booleanExp) ? exp1 : exp2
if (x > 0) {
y = 1;
} else {
y = -1;
}
is equivalent to
y = (x > 0) ? 1 : -1;
exp1 = 1
exp2 = -1
THANK YOU FOR LISTENING