Tuesday - 18 March, 2025 +91

Control Flow Statements in Java


This post is part of Java Tutorial. This post is applicable for all people who are following Java Tutorial.Today we are going to discuss various control flow statements available in Java.

In Java ( also in C,C++) the flow of control is sequential. I mean statements are executed line by line. After the execution of first statement, control goes to second statement and then third and so on. The program will be terminated after the execution of last statement. So statements are executed from top to bottom.

Some times we need to break up the flow of execution. We need to execute first statement and then based on certain condition we may go for second statement or we can directly jump to sixth statement. To do this type of task control flow statements are there in Java.

Control flow statements break up the flow of execution by employing decision making, looping, and branching. In Java decision making statements are if, else, switch, the looping statements are for, while, do-while, and the branching statements are break, continue, return.

if statement:
Using if we can execute a statement (or block of statements) based on the condition specified using if. If the condition is true then only the block of statements will be executed. Otherwise the block will be simply skipped.

Syntax of if statement:

if(condition)
{ // block start

//executable statement1
//executable statement2

// block end
}
Note that { } (curly braces) are not necessary if there is only one statement in the block.

Syntax of if-else statement:

if(condition)
{ // block start

//executable statement1
//executable statement2

// block end
}
else
{ // block start

//executable statement3
//executable statement4

// block end
}
Like this we can also define another ‘if’ after the ‘else’ part. This is termed as nested if.

switch statement:
switch works similar to if-else statements. It provides better readability as compared to if-else. The General Syntax of switch is:

switch(variable)
{

case value1: // code to be executed;
break;
case value2: // code to be executed;
break;
.
.
.
default : //code to be executed;

}
In switch the variable is matched with corresponding value. If any matching value is found, that case will be executed. Observe we have given break keyword after every case. This is necessary otherwise all the cases after the matching case will get executed. The keyword break is used here to transfer the control outside of switch once our code has been executed.

Keyword default will only get executed if all the cases fails to execute. That means if none of the cases match then only default case will be executed. Default can be present any where in the switch block but it will always get executed after testing all the cases. Generally people use to write default as the last choice in the switch block. Also default is optional in switch.

while statement:
Some times we need to execute a set of statements continuously as long as the condition is true. To do such type of tasks we have while statement.

The syntax of while statement is:

while(condition)
{

executable code;
.
.
.
}
As long as the condition specified with in while loop is true the block of code will get executed. If the condition becomes false the loop will be skipped. We have to write code in the while block in such a way that every time the loop comes closer to the breaking condition. Otherwise the loop will not be terminated at all. such type of loops are known as infinite loops. Remember in while loop if the condition evaluates to false at first time then the block of code will not be executed at all. This is also known as entry control loop.

do-while statement:
Do while loop is also similar to while loop except one difference. In do-while loop the condition is tested at bottom instead of at top.

The syntax of do- while statement is:

do
{

executable code;
.
.
.
} while(condition);
Since the condition is tested at bottom even though it evaluates to false the loop will be executed at least once. Hence it is also known as exit control loop. Also observe after do while condition semicolon is necessary.

0 comments:

Post a Comment