Control Statements are the statements which controls flow of
execution of programs. These statements either allows the execution of block of
statements or escape the execution of block of statement based on some
condition. There are many control statements which are mainly divided in two
types:
Conditional
Control Statements: Conditional control statements are those which control
the execution of program based on some condition. Some conditional control statements are:
i.
‘If’
Statement:
Syntax:
if (Boolean
value)
{
//Statement(s).
}
In simple ‘if’ statement block of statement inside the
if will be executed only if the Boolean value is true otherwise the control is
transferred to the next executable statement after if statement.
ii.
‘If
else’ Statement:
Syntax:
if (Boolean
value)
{
//Statement(s).Block1.
}
else
{
//Statement(s).Block2.
}
In ‘if else’ statement only one of block 1 or block2
will be executed as per Boolean value.
If Boolean value is true block1 will be executed and if Boolean value is
false then block2 will be executed.
iii.
‘If
else if’ Statement:
Syntax:
if (Boolean
value1)
{
//Statement(s).Block1.
}
Else if(Boolean
value2)
{
//Statement(s).Block2.
}
.
.
.
Else if(Boolean
value n)
{
//Statement(s).Block
n.
}
Else
{
//Statement(s);
}
In ‘if else if’ statements any block of statements
will be executed on this basis of Boolean value. As soon as one of the ‘if’
executes one of the blocks inside if then control will move to the first
executable line after ‘if’ statement.
If you have only one statement inside ‘if’ statement
then you can ignore curly (‘{}’) brackets. If you are ignoring the curly
brackets then first stamen after the condition will be considered as block of
‘if’ statement.
iv.
‘Switch’
Statement:
Using ‘switch’ statement you can execute block of
statements on the basis of some value of variable.
Syntax:
switch
()
{
case1:
statement(s)
case2:
statement(s)
case3:
statement(s
case4:
statement(s)
default:
statement(s)
}
·
The variable provided in switch statement can be
numeric type. From Java5 you can use ‘enum’ and from Java7 you can use ‘String’
type also.
·
The value of variable will be verified with constant
or literal provided with case statement.
·
All the statements will be executed from where the case
is matched to variable.
·
If any matching case is found corresponding block of
statement will be executed. By default all the blocks of statements must be
executed. Then you can use ‘break’ statement as last statement of each block
as:
Syntax:
switch ()
{
case1: statement(s)
break;
case2: statement(s)
break;
case3: statement(s
break;
case4: statement(s)
break;
default: statement(s)
break;
}
If none of
case is matched then block of statements under default case will be executed.
Default case can be provided as first block or as last block or in between.
Only variables are allowed inside the bracket of switch
statements. Only constants and key literal are allowed with case keyword.
v.
‘For’
Statement:
‘For’ statement is looping or iterative control
statement and can be used to execute the statement or block of statements
iteratively for some limited number if times which is defined in the condition
part of the ‘for’ statement.
Three parts of for statements are:
ü Initialization-executed
only once.
ü Condition.
ü Updation.
And after
these it contains statements to be executed.
Syntax:
for (initialization;condition;updation)
{
//Statement(s);
}
All
the three parts i.e. initialization, condition and updation are optional in
‘for’ statement and if these are ignored then it will lead to an infinite loop
(a loop that will never terminate.) But we can terminate such loop by pressing
ctrl+c.
Ø In
initialization part variables can be declared and initialized.
Ø Condition
part is used to make decision whether to enter the loop or not. Condition part
must be of Boolean type. If value is true control will enter the loop.
Ø Updation
part performs updation on variable which is used to manage the loop iteration.
Ø More than
one variable can be declared in initialization part and more than one updation
can be made in updation part separated by comma.
Ø ‘for’ is an
entry control looping
statement i.e the condition is checked to decide whether or not to enter
the loop.
vi.
‘While’
Statement:
‘While’ is also an entry control looping statement i.e the
condition is checked to decide whether or not to enter the loop.
Syntax:
while()
{
//Statement(s).
}
In ‘while’ statement the condition part is mandatory and it
must of Boolean type. It can be anything but result of the condition must be of
Boolean type. If Boolean value is true then statement inside the block will be
executed. If false the next statement after the while loop will be executed.
vii.
‘do-while’
Statement:
When we want to execute the block of statement at
least once then we use ‘do-while’ statement. So the minimum number of execution
of statements inside the ‘do-while’ statement will be one.
Syntax:
do
{
//Statment(s).
}
while
()
In case of ‘do-while’ statement the ‘do’ block will be
executed first and then Boolean value is checked. The Boolean value is checked
to decide whether or not to enter the loop again. So we can say that it is exit control looping
statement as Boolean value is checked to decide whether to or not to
enter the loop at the exit point. If Boolean value is true control is shifted
again inside the ‘do’ block and if it is false then control is shifted to the
next executable statement.
Unconditional
Control Statements:
A statement which transfers the flow of control of the
program to next line or block without checking for any type of conditions are called
as unconditional control statements.
i.
‘break’
Statement:
‘break’ statement is used to terminate block of
statements which is currently under execution. It can be use in ‘switch’ as
well as in other looping statements to break the normal flow of execution in
that block and to shift the control to next statement after the block.
Syntax:
break[label];
Ø In nested
loops ‘break’ statement can be used to terminate the outer loop from inner
loop. To terminate outer from inner loop we must provide the name to outer loop
and we can name the outer loop by using lables.
Syntax:
break;
ii.
‘continue’
Statement:
To skip the execution of part of loop or loop
iteration we use ‘continue’ statement. It can be used only with the looping
statement.
If we use the ‘continue’ statement with ‘for’ loop
then control will be shifted to the updation part of the ‘for’ loop. And when
‘continue’ statement is used with ‘while’ or ‘do-while’ loop control is
transferred to the condition part of looping statement.
0 comments:
Post a Comment