Flow Control in Java Part-1

Statements in Java which are executed from top to bottom and can break the flow of execution of the program by the use of statements such as decision making, looping, branching or conditionally execution a block. Flow control describes the order in which the lines of code will execute at run time. Categorization of flow control statements is into 3 types:

Selection Statements

These are the statements which selects a block of code to execute on the basis of the test condition in it. There are 2 types of selection statements available in Java.

If else:

      •  In this statement both

if 

      • and

else

      • are keywords. Syntax for using this statement is:
if(test_condition_1)
{
//code to be executed when the given condition is true
}
else if(test_condition_2)
{
//code to be executed when previous condition is false and given condition is true
}
else
{
//code to be executed when all given conditions are false
}

Note- If else statements can be used in multiple ways, like only 1 condition is needed to be tested then only we can use if . For example,

if(10>9)
{
System.out.println("10 is greater than 9");
}

There is no limit to test conditions using else if. So one can use for as many condition as one wants. Test conditions should always return boolean value otherwise run time error will be given. For example,

//scenario 1
if(1)                //error
{
//code 
}

//scenario 2
if(true)             //correct
{
//code
}

Note- Assignment operator should not be used as they are not for comparison.

Switch: In a test condition where multiple options can be correct, we do not prefer to use if statement. Rather switch statements should be used. Just like else in if statement, there is a default block in switch statement which is executed only when no other cases are true. Syntax:

switch(test_condition)
{
case 1:
//code
break;
case 2: 
//code 
break;
case 3: 
//code 
break;
default:
//code
}

Just like if else statement there can be as many possible test cases as one wants. If break is not used at the end of the cases then all the following cases will be tested, which may result in consumption of time and memory. Allowed data types for the test condition of switch are byte, short, int, enum and char. If any other data type is given then compilation error will be given by the compiler. Labels for the cases should be valid compile time constant, if variables are used instead of constant compiler will give compile time error. Duplication of cases is not allowed. For example,

int ch = 'a';
switch(ch)
{
case 'a':
System.out.println("Given character is 'a'");
break;
default:
System.out.println("Given character is not 'a'");
}

Iterative Statements

As the name indicates, such statements which can execute the particular lines of codes multiple times. Based on the previous knowledge of number of iterations required there are 3 types of iterative statements.

While

      • If number of iterations required is not known then while statement is used. Only

boolean

      • values are allowed in its test conditions. It is an entry controlled loop. Its syntax is:
while(condition)
{
//code
}

For example,

int i=1;                            //initializing the value
while(i<5)                          //test condition
{
System.out.println(i);              //printing the value of i
i++;                                //updating the value to prevent infinite iterations
}

Output:

1
2
3
4

Do while: If number of iterations are not known before and still a code is needed to be executed at least once then do while statement is used. It is exit controlled loop. Syntax:

do
{
//code
}while(test_condition);

Once the code is executed test condition will be checked, if it is true then only another iteration will be there. For example,

int i = 1;
do
{
System.out.println(i);
i++;
}while(i<5);

Output:

1
2
3
4

For: If the number of iterations to be done is known before hand then it is recommended to use for loop. In this statement, initial variable, test condition and update statements are defined at the start of the loop. Syntax:

for(initialization; test_condition; update_statement)
{
//code
}

For example,

for(int i=1;i<5;i++)
{
System.out.println(i);
}

Output:

1
2
3
4

For each: Also known as enhanced for, it is the advance version of for loop. It is best for traversing items of arrays and collection. It traverses from 1st element to the last element. No other traversing is possible in it. Syntax:

for(int variable: collection/array)
{
//code
}

For example,

int[] ar = {10, 20, 30, 40};
for(int i: ar)
{
System.out.println(i);
}

Output:

10
20
30
40

Leave a Comment

Your email address will not be published. Required fields are marked *