There three types of statements in java.
- Selection statements
- Iteration statements
- Jump statements
1. Selection statements
It is to manage the flow of programs that is to be executed based
on the dynamic conditions which can be only realized during the run time. It
provides flexibility.
If statement- It provides different paths for execution of program.
Syntax:
If (condition
provided)
statement a;
Else
Statement b;
This means if the condition is true, then statement a is
executed but if false then statement b is executed.
For example
int a, b;
If (x < y)
a=0;
Else
b=0;
Now there is one type of procedure using if statement i.e. nested if statement and it is very
common method in programming world.
For example
If (a == 10)
{
If (b < 15)
i = j;
If (c
> 50)
p
= q;
Else
i
= p;
}
Else
i = q;
Now, the second if statement in the parenthesis is associated with
else. Another type of procedure is of using if statement is the if-else-if
ladder statement.
For example
If (condition)
Statement;
Else if (condition)
Statement;
Else if (condition)
Statement;
.
.
.
.
.
Else
Statement;
Switch statements: It is a multi branched statement.
Syntax:
Switch (expression)
{
Case value 1:
Statement
Break;
Case value 2:
Statement
Break;
.
.
.
Case vale n:
Statement
Break;
Default:
Statement;
}
The functioning of the switch statement is compares the given
value with all the cases and when the match found the program sequence is
executed.
For example:
Class sampswitch {
Public static void main (string args [])
{
Switch (i)
{
Case0:
System.out.println
(“i is zero.”);
Break;
Case1:
System.out.println(“i
is one.”)
Break;
Default:
System.out.println
(“i is greater than 4.”)
}
Output:
i is zero.
i is one.
i is greater than 4.
i is greater than 4.
Example of nested switch loop program
Switch (countfigures)
{
Case 1:
Switch
(targetvalue)
{//nested
switch//
Case 0:
System.out.println
(“target is zero”;
Break;
Case 1:
System.out.println
(“target is one”;
Break;
}
Break;
Case 2: //…
}
Important features of switch statement are. Switch statement can
only check for equality unlike if. There cannot be two cases constant in the
same switch with identical values. It is more effective than using the set of
nested if statement.
Iteration statements
These statements are used to create loop. There are three types of
iteration statement used in java.
While statement:
Its function is to repeat or block statement while its controlling
expression is true.
Syntax:
While (condition)
{
// body of loop
}
The loop is executed when the condition is true. When it is false
the control will pass to the next line of the code following the loop.
For example:
Class while
{
Public static
void main (string args [])
{
Int n =
5;
While
(n>0)
{
System.out.println
(“tick “+ n);
n—;
}
}
}
Output
It will tick 5 times
Tick 5
Tick 4
Tick 3
Tick 2
Tick 1
Do-while statement: This loop executes its body at least once because it contains its
condition expression at the bottom of the loop.
Syntax:
do
{
System.out.println
(“tick”+ n);
n— ;
}
While n > 0);
}
}
For statement: It is a very powerful as well as very versatile construct. The
condition what is provided in for statement until and unless the condition is
not been satisfied it won’t execute. If the condition is evaluated and the
value is false then it is terminated.
Syntax:
For (initialize; condition; iterate)
{
//Body
}
For example:
Class fortick
{
Public static
void main (string args [])
{
Int n;
For
(n=11; n>0; n—);
}
}
Jump statements:
Java supports three jump statements which are as follows.
1. Break
2. Continue
3. Return
Break: It is used to terminate a statement which is in sequence in a
switch statement and it can be used for exiting a loop. Some time It can be
used instead of goto.
Continue: It provides an early iteration for a loop.
For example:
Class continue
{
Public static
void main (string args [])
{
For (int
i=0; i<5; i ++)
{
System.out.print
(i + ““ );
If
(i%2 ==0) continue;
System.out.print
(““ );
}
}
}
Return: It is used to return from a method and It is a transfer back
calling of the method.
For example:
Class return {
Public static
void main (string args [])
{
Boolean
t = true;
System.out.println
(“return”);
If (t)
return;
System.out.println
(“this won’t work”);
}
}