Chapter – 5
Selection Statements
• Supports two selection statements :
• if and switch
• These statements allow you to control the flow of your program’s execution based
upon conditions known only during run time.
• General form of if statement is as follows:
• if (condition) statement1;
else statement2;
• Nested ifs
• The if-else-if Ladder
Nested Ifs
• if(i == 10) {
if(j < 20) a = b;
if(k > 100) c = d; // this if is
else a = c; // associated with this else
}
else a = d; // this else refers to if(i == 10)
The if-else-if ladder
• if(condition) • The if statements are executed from the top down.
• As soon as one of the conditions controlling the if is true, the
statement; statement associated with that if is executed, and the rest of the
else if(condition) ladder is bypassed.
• If none of the conditions is true, then the final else statement will
statement; be executed.
• The final else acts as a default condition; that is, if all other
else if(condition)
conditional tests fail, then the last else statement is performed.
statement; • If there is no final else and all other conditions are false, then no
action will take place.
...
else
statement;
switch
• switch (expression) { • The switch statement is Java’s multiway branch statement.
case value1: • Provides a better alternative than a large series of if-else-if
// statement sequence statements.
break; • The expression must be of type byte, short, int, or char; each of
case value2: the values specified in the case statements must be of a type
// statement sequence compatible with the expression.
break; • Each case value must be a unique literal (that is, it must be a
constant, not a variable).
...
• Duplicate case values are not allowed.
case valueN:
// statement sequence
break;
default:
// default statement sequence }
switch compared to if-else-if
• When it compiles a switch statement, the Java compiler will inspect each of the
case constants and create a “jump table” that it will use for selecting the path of
execution depending on the value of the expression.
• Therefore, if you need to select among a large group of values, a switch statement
will run much faster than the equivalent logic coded using a sequence of if-elses.
• The compiler can do this because it knows that the case constants are all the same
type and simply must be compared for equality with the switch expression.
• The compiler has no such knowledge of a long list of if expressions.
Iteration statements
• for
• while
• do-while
while - loop
• while(condition_statement→true){
• Statements to be executed when the condition becomes true and
execute them repeatedly until condition becomes false.
• }
• E.g.
int x =2;
while(x>5){
System.out.println(“value of x:”+x);
x++;
}
do while - loop
do{
statements to be executed at least once without looking at the condition.
The statements will be executed until the condition becomes true.
} while(condition_statement);
for - loop
for(initialization; condition; increment/decrement){
statements to be executed until the condition becomes false
}
E.g:
for(int x=0; x<10;x++){
System.out.println(“value of x:”+x);
}
For each version of the for loop
• Enhanced for loop
• The general form of the for-each version of the for is shown here:
• for(type itr-var : collection) statement-block
• Here, type specifies the type and itr-var specifies the name of an iteration variable that will
receive the elements from a collection, one at a time, from beginning to end.
• The collection being cycled through is specified by collection.
• With each iteration of the loop, the next element in the collection is retrieved and stored in
itr-var.
• The loop repeats until all elements in the collection have been obtained.
• Because the iteration variable receives values from the collection, type must be the same
as (or compatible with) the elements stored in the collection.
• CAN’T BE USED TO INITIALIZE AN ARRAY AND MODIFY ITS ELEMENTS → LIMITATION
Jump statements
• break
• continue
• return
break statement
• In Java, break statement has three uses:
• First, It terminates a statement sequence in a switch statement.
• Second, it can be used to exit a loop.
• Third, it can be used as a “civilized” form of goto.
• When used inside a set of nested loops, the break statement will only break out of
the innermost loop.
• Using break as a form of goto : break label; where label is the name of a label that
identifies a block of code.
• Keep in mind that you cannot break to any label which is not defined for an
enclosing block.
continue
• Continue makes the loop to skip the current execution and
continues with the next iteration.
• for(int i=0;i<50;i++){
if(i%13==0){
continue;
}
System.out.println(“Value of i:”+i);
}
Labeled break & continue
• Labeled break and continue statements will break or continue from the loop that
is mentioned.
• Used in nested loops.
return
• return statement can be used to cause execution to branch back to the caller of
the method.
break vs. continue
Any Questions???