Control Flow Statements are used to break the flow of execution of
the program by employing the following categories:
·
Decision making (Selection statements)—if, nested ifs,
if-else-if ladder, switch.
·
Looping (Iteration statements)—while, do-while, for.
·
Branching (Jump statements)—break, continue, return.
Selection statements: These statements works on the conditions
which are known only at the run time.
1) if
statement: It is also known
as conditional branch statement. The set of statements are executed if the
condition is true.
·
Syntax:
if(condition) statement1;
else
statement2;
|
·
Explanation:
·
Each
statement is enclosed within a pair of curly braces, which is also known as the
Block.
·
The
condition here can be any expression which returns a Boolean value i.e. True or
False.
·
The
else clause is optional here. If the condition is true, then statement1 is
executed. Otherwise, if condition is false then else clause works i.e.
statement2 is executed.
2) Nested
if statement: This means using one if or else if
statement inside another if or else if statement.
·
Syntax:
if(condition1){
// executes when the condition1 is true
if(condition2){
// executes when the
condition2 is true
}
}
|
·
Note: The main thing to remember
is that the else clause always refers to the nearest if clause that is within
the same block.
3) if-else-if ladder: This executes when particular
conditions are true and else works when none of the above conditions are true.
if(condition)
statement;
else if(condition)
statement;
else if (condition)
statement;
.
.
.
.
else
statement;
|
·
Explanation:
·
The if statements are executed from the top down approach.
·
When one of the condition controlling if statements are true,
the statements associated with particular if is executed and remaining ladder
is bypassed.
·
If none of the condition is true then the final else
statement will be executed. Final else clause acts as the default condition.
4) switch statement: It is a multiway branch statement. It
allows a variable to be tested for equality against a list of values. Value is
called case.
switch(expression){
case value 1:
//statement sequence
break;
case value 2:
//statement sequence
break;
.
.
.
.
case value n:
//statement sequence
break;
default;
//default statement
sequence
}
|
·
Explanation:
·
The expression must be byte, int, short, char.
·
The values in the case must be of a type compatible with the expression.
·
The expression value is compared with the case value. If the
match is found the statement sequence is executed of that particular case.
·
If none of the value is matched with the expression value
then the default statement is executed.
·
The default statement is optional.
·
If there is no default and no case is matched then no action
is taken.
·
The break statement is used to terminate the specific
statement sequence.
·
The break statement is also optional. But it is always
advised to use break in switch statements.
Looping statements: These are also known as the Iteration statements in Java. A
loop executes repeatedly the same set of instructions until a termination
condition is met.
1) while loop: while loop is used to repeat a task for a certain
amount of time. The set of statements are executed when the condition is true.
The execution stops when the condition becomes false.
while(condition){
//body of loop
}
|
·
Explanation:
·
The condition can be Boolean expression.
·
The body of the loop is executed till the condition remains
true.
·
When the condition is false then the control passes to the
next line of code following the loop.
2) do-while loop: The difference between these two loops are that
do-while evaluates its expression at the bottom of the loop instead of top, so
the statements in the do block are executed at once.
do{
//body of the loop
}while(condition);
|
·
Explanation:
·
It first executes the body of the loop and then evaluates the
conditional expression.
·
If the expression is true the loop will repeat. Otherwise it
terminates.
3) for loop: for loop provides a compact way to iterate over a range of
values. It loops until a particular condition is satisfied.
for(initialization; condition; iteration){
//body of the loop
}
|
·
Explanation:
·
The initialization expression is used to initialize the loop
and is always executed only once as the loop begins.
·
Next is that the condition is evaluated and if the condition
is false the loop terminates and if it is true then the body of the loop is
executed.
·
The iteration portion increments or decrements the loop
variable.
·
The process repeats until the controlling expression becomes
false.
Branching statements: Java supports three jump
statements i.e. break, continue and return. These statements are used to transfer
the control to another part of the program.
1) Break statement: It can be used to exit a loop.
·
It is used to terminate the statements in switch.
·
It can be used as the form of “goto”.
·
User can break out of one or more blocks of code by using
break.
·
The blocks are not required to be part of a loop or a switch,
it can be any block.
·
break works with a label and provides benefits of goto.
break label;
|
·
Label is the name of the label which is used to identify the
block of code.
·
The control is transferred out from the named block of code.
·
The labeled block of code should enclose the break statement.
2)
Continue statement: The continue statement
transfers the control directly to the conditional expression that controls the
loop in while and do-while loop.
·
In loop statements where early iteration is needed the
continue statement provides a structured way for it.
3) Return statement: This is the last control statement.
It is used to return explicitly from a method.
·
It causes the control of program to transfer back to the
invoker of the method.
·
It terminates the method in which it is executed.
Tags
Java