You are on page 1of 18

ITPF 01– Object Oriented Programming 1

Selection Control
Statements
Melgine M. Bauat, MSIT
Instructor
Topic Outline

The if selection statement


The if-else selection statement
The if-elseif-else selection statement
The nested if selection statement
The switch statement
Conditional Operator
The IF Statement
The statements inside if will be executed if and only if the
booleanExpression is true.

if (booleanExpression) {
statement(s);
}

Example: This is true, therefore the System.out.println statement


inside the if will be executed.
int i = 5;

if ( i < 10 ) {

System.out.println(“5 is less than 10“);


}
The IF Statement
Adding a semicolon at the end of an if clause is a common mistake.
if (radius >= 0); <-Wrong
{
area = radius*radius*PI;
System.out.println("The area for the circle of
radius " + radius + " is "
+ area);
}
This mistake is hard to find, because it is not a compilation error or a
runtime error, it is a logic error.
This error often occurs when you use the next-line block style.
The IF ELSE Statement
The statements inside if will be executed if the
booleanExpression is true, else (false) , it will execute
the else statements.

if (booleanExpression) {
statement(s);
} else {
statement(s);
}

Example:

float grade = ______;


if ( grade >= 75.00) {
System.out.println(“You Passed the ITPF01“);
} else {
System.out.println(“You Failed, try again next year“);
}
The IF ELSE Statement
The IF-ELSEIF-ELSE Statement
The statements inside if will be executed if the
booleanExpression is true, if false, it will
test the else if clause then it will execute its
statement if the result is true. Finally, if the
booleanExpression of if and else if are both
false, it will execute the else statements.

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

The default case, which is optional, can be used to perform


actions when none of the specified cases is true.
·      
The order of the cases (including the default case) does not
matter. However, it is a good programming style to follow the
logical sequence of the cases and place the default case at the
end.
The switch Statement
Do not forget to use a break statement when one is needed. For example, the
following code always displays Wrong number of years regardless of what
numOfYears is. Suppose the numOfYears is 15. The statement annualInterestRate =
8.50 is executed, then the statement annualInterestRate = 9.0, and finally the
statement cout << "Wrong number of years".

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;

The booleanExp will test. If true, the value of


variable y will be the exp1, and if false, the value
of variable y will be the exp2.

exp1 = 1
exp2 = -1
THANK YOU FOR LISTENING

You might also like