Java’s Selection Statements
Java 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. You will be pleasantly surprised by the
power and flexibility contained in these two statements.
if
It is examined in detail here. The if statement is Java’s
conditional branch statement. It can be used to route program execution through
two different paths. Here is the general form of the if statement:
if (condition ) statement 1 ;
else statement 2 ;
Here, each statement may be a single statement or a compound
statement enclosed in curly braces (that is, a block ). The condition is any
expression that returns a boolean value.
The else clause is optional.
The if works like this: If thecondition is true, then
statement1 is executed. Otherwise, statement2 (if it exists) is executed. In no
case will both statements be executed. For example, consider the following:
int a, b;
// ...
if(a < b) a = 0;
else b = 0;
Nested ifs
A nested if is an if statement that is the target of another
if or else . Nested if s are very
common in programming. When you nest if s, the main thing to
remember is that an
else statement always refers to the nearest if statement that
is within the same block
as the else and that is not already associated with an else .
Here is an example:
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)
As the comments indicate, the final else is not associated
with if(j<20), because it is not in the same block (even though it is the
nearest if without an else ). Rather, the final else is associated with
if(i==10) . The inner else refers to if(k>100), because it is the closest if
within the same block.
The if-else-if Ladder
A common programming construct that is based upon a sequence
of nested ifs is the if-else-if ladder . It looks like this:
if( condition )
statement;
else if (condition)
statement ;
else if( condition )
statement ;
.
.
else
statement ;
The if statements are executed from the top down. As soon as
one of the conditions controlling the if is true , the statement associated
with that if is executed, and the rest of the ladder is bypassed. If none of
the conditions is true, then the final else statement will be executed. The
final else acts as a default condition; that is, if all other conditional tests
fail, then the last else statement is performed. If there is no final else and
all other conditions are false, then no action will take place.
switch
The switch statement is Java’s multiway branch statement. It
provides an easy way to dispatch execution to different parts of your code
based on the value of an expression.As such, it often provides a better
alternative than a large series ofif-else-if statements.
Here is the general form of a switch statement:
switch ( expression) {
case value1 :
// statement sequence
break;
case value2 :
// statement sequence
break;
.
.
case valueN :
// statement sequence
break;
default:
// default statement sequence
}
The expression must be of type byte, short, int, or char ;
each of the values specified in the case statements must be of a type
compatible with the expression. Each case value must be a unique literal (that
is, it must be a constant, not a variable). Duplicate case values are not
allowed.
// A simple example of the switch.
class SampleSwitch {
public static void main(String args[]) {
for(int i=0; i<6; i++)
switch(i) {
case 0:
System.out.println("i is zero.");
break;
case 1:
System.out.println("i is one.");
break;
case 2:
System.out.println("i is two.");
break;
case 3:
System.out.println("i is three.");
break;
default:
System.out.println("i is greater than 3.");
}
}
}
The output produced by this program is shown here:
i is zero.
i is one.
i is two.
i is three.
i is greater than 3.
i is greater than 3.
Nested switch Statements
You can use a switch as part of the statement sequence of an
outerswitch . This is called a nested switch . Since a switch statement defines
its own block, no conflicts arise between the case constants in the inner
switch and those in the outer switch . For example, the following fragment is
perfectly valid:
switch(count) {
case 1:
switch(target) { // nested switch
case 0:
System.out.println("target is zero");
break;
case 1: // no conflicts with outer switch
System.out.println("target is one");
break;
}
break;
case 2: // ...
Here, the case 1: statement in the inner switch does not
conflict with the case 1: statement in the outer switch. The count variable is
only compared with the list of cases at the outer level. If count is 1, then
target is compared with the inner list cases.
No comments:
Post a Comment