Selection
Statements:
if statement:if(condition)
statement1;
else
statement2;
If the condition is true, then
statement1 is executed. Otherwise, statement2 is executed.
Nested if
statement:A nested if is an if statement
that is the target of another if or else. For example:
if(i==10)
{
if(j<20) a=b;
if(k>10)c=d;
else a=c;
}
else
a=d;
if-else-ifLadder:if(condition)
Statement;
elseif(condition)
statement;
elseif(condition)
statement;
.
else
statement;
switch
statement: The switch statement is Java’s multiway branch statement.
switch(expression){
case value1:
//statement
sequence
break;
case value2:
//statement
sequence
break;
.
.
case valueN:
//statement
sequence
break;
default:
//statement
sequence
}
The expression must be of type byte, short, int, or char; each of the
expression is compatible with the expression. Duplicate case values are not allowed.
Nested switch
statement: One can use a switch as part of the statement sequence of an outer switch. This is called nested switch.
switch(expression){
case 1:
switch(target){
case 0:
//statement
sequence
break;
case 1:
//statement
sequence
break;
}
case valueN:
//statement
sequence
break;
default:
//statement
sequence
}
Features of
switch statement:
o
The switchdiffers from theif in
that switch can only test for
equality, whereas if can evaluate
any type of Boolean expression. That is, the switch looks only for a match between the value of the expression
and one of its case constants.
o
No two case constants in the same switch
can have identical values. Of course, a switch statement and an enclosing
outer switch can have case constants in common.
o
A switch statement is usually more efficient than a set of nested ifs.
Iteration
Statements:
while loop:while(condition)
{
statement1;
//body of the loop
}
statement1;
//body of the loop
}while(condition)
statement1;
//body of the loop
}
o
Multiple
initialization using comma operator:
for(int
x=1,y=10;x<y;x++,y--){
System.out.println
(“a = ” , + a);
System.out.println(“b
= ” , + b);
}
o
For-Each
version of the for loop:
for(type
itr-var:collection) statement-block
int num[]={1,2,3,4,5};
int sum=0;
for(int
x:num) sum+=x;//for-each version of the loop
Jump
Statements: Java supports three jump
statements:break, continue, and return.
Using break
statements:
o
break
can be used to terminate a statement sequence in a switch statement.
o
It can be used to exit a loop.
o
It can be used as a ‘civilized’
form of goto:
break label;
class BreakLoop{
public
static void main(String args[ ]) {
outer:
for(int i=0;i<5;i++) {
System.out.println(“Pass
“+ i + “ : “);
for(int
j=0;j<50;i++) {
if(j==5)break outer;
System.out.println(j
+ “ “);
}
}
}
Output:Pass
0:0 1 2 3 4
Using continue
statements:
o
In while and do-while loops,
a continue statement control to be
transferred directly to the conditional expression that controls the loop.
o
In forloop, control goes first to the iteration portion of the for loop and then to the conditional
expression.
o
For all the three loops, any
intermediate code is bypassed.
o
As with the break statement,continuemay
specify a label to describe while enclosing loop to continue.
class
Mul
{
public static void main(String
args[])
{
outer:for(int
i=0;i<5;i++)
{
System.out.println();
for(int
j=0;j<5;j++)
{
if(j>i)
continue outer;
System.out.print((i*j)
+ "\t");
}
}
}
}
Output:
0
0
1
0
2 4
0
3 6 9
0
4 8 12 16
Using return statements:
o
Used to explicitly return from a
method. That is, it causes program control to transfer back to the caller of
the method.
o
The return statement immediately terminates the method in which it is
executed.
No comments:
Write comments