Control statements: the execution flow controllers

The Control statements:

Rupam Jha
JavaMadeTranquil
Published in
6 min readNov 2, 2020

--

As the name suggests, a control statement is a statement which determines whether or not the other statements of a program will be executed. It basically controls the complete flow of a program.

Hello Comrades,

Today we are looking forward to discussing one of the fundamental topics in any programming language. This article will give you a brief explanation on what are control statements and how they control the flow of execution of any program. Starting with giving you all an overview of the categories of a control statement. The control statement is broadly categorized into three statements

  • selection statements[conditional statement/decision making statement]
  • iteration statements[looping statement]
  • jump statements[branching statement]

Selection statements: The selection statements allow the program to test several conditions and execute instructions based on which condition is true. Hence a selection statement is also termed as a conditional statement. Java typically supports three types of selection statements: single selection statement[if.. statement], double selection statements[if..else statement] and multiple selection statements[switch statements].

if..statement: The “if ” block gets executed only when the condition is set to true.

syntax :
if(condition)
{
//statements (if block)
}
//other statements
eg: public class abc{public static void main(String [] args){

int i =10;

if(i>5){

System.out.println("i is greater than 5");

}

System.out.println("i is less than 5");
}}O/p:i is greater than 5

if..else statement: The “if ” block gets executed only when the condition is true, otherwise the “ else” block gets executed.

syntax: 
if(condition)
{
// statement; (if block statement)
}else{
// statement; (else block statement)
}
eg: public class abc{public static void main(String [] args){

int i =0;

if(i>5){

System.out.println("i is greater than 5");

}else{

System.out.println("i is less than 5");
}}O/p:i is less than 5

if..else-if statement: The “if” block gets executed when the condition is true, otherwise the “else-if” block gets executed; and further when condition of “else-if” block is not true, the “else” block gets executed.

syntax: 
if(condition){
// statement;(if block)
}else if(condition){
// statement;(else if block)
}else{
// statement;(else block)
}
eg: public class abc{public static void main(String [] args){

int i =100;

if(i>5){

System.out.println("i is greater than 5");

}else if(i<5){

System.out.println("i is less than 5");
}else{System.out.println(" i is " + i ); //(value of i)}O/p:i is 100

switch statement: The switch statement gives access to the program to select one action[case] among multiple actions[cases] during the execution. If none of the cases match, the default will be executed. It is optional to write “break” statement at the end of each case.

syntax:
Switch( variable / value/ expression)
{
Case :
//statement[];
Case :
//statement[];
Case :
//statement[];
Case :
//statement[];
Case :
//statement[];
default:
//statement[];
}
eg: public class abc{public static void main(String[] args){int number =5;
Switch(choice){
case 1:
System.out.println("Number is 1");
case 3:
System.out.println("Number is 3");
case 5:
System.out.println("Number is 5");
case 7:
System.out.println("Number is 7");
default;
Sytem.out.println("No number is found");
}
0/p:Number is 5
Number is 5
No number is found

iteration statement: As the name suggests, these statements executes a block of code in iteration for multiple times, which means it basically runs a block till the looping condition is satisfied. These statement are also broadly classified into for loop, while loop and do-while loop and for-each loop statements.

for loop: These statements execute until the condition is false. We use these statements, where number of iterations are known to us.

syntax:
for(initialization, condition, increment/decrement of loop){
//statement / block of code(Body)
}
eg: public class abc{public static void main(String [] args){for(int i =0; i<10; i++){System.out.println("value of i is : "+i);}// string concatenation}}o/p:
value of i is : 1
value of i is : 2
value of i is : 3
value of i is : 4
value of i is : 5
value of i is : 6
value of i is : 7
value of i is : 8
value of i is : 9

while loop: This statement executes till the condition sets out to be false, also this while loop executes if and only if the the condition is set to true, unlike do..while loop.

syntax: 
while(condition){
//statement
}
eg: public class abc{public static void main(String [] args){int i = 5;while(i<7){System.out.println("value of i is : "+i);}// string concatenationi++;}}o/p:
value of i is : 5
value of i is : 6

do..while loop :Unlike the while loop (which gets executed only if the condition is set to true), a do-while loop executes the loop first and then checks for the condition; which means this do-while loop will execute at-least once.

It has semicolon at the end of the while condition unlike the while loop.

syntax: 
do{
//statement;
while(condition);{
//statement;
}
}
eg: public class abc{public static void main(String [] args){int a = 2; do{System.out.println("i am in main method");}while(a<=2);System.out.println("value of i is : + i); a++;}

Note: do-while loop is called exit controlled loop because they tend to check the condition after the block is executed i.e. they check the condition for exit and if the given condition for exit evaluates to true, control will exit the loop body, else control will enter again into the loop.Such control structure is often referred to as “post-test loop”; whereas for loop and while loop are called entry controlled loop, which tests the condition before the code is executed.

for-each loop: The for-each loop or say enhanced for loop provides an alternative approach for traversing the array or collections in Java. It is know as for-each loop as it traverses each element one by one.

syntax:
for(type variable : array){
//statements utilizing variable;
}
eg: public class abc{public static void main(String[] args){int[] a ={100,101,102,103,104,105,106,107,65,98,80,55};for(int currentInteger : a){int highestNumberValue = Math.max(a);Sytem.out.println("The highest number is : " + highestNumberValue);}}}o/p: The highest number is : 107

Note: The for loop is best for iterating over the name-value pairs where as the for-each loop is best for iterating over values.

jump statements: These statements cause unconditional jumps to other statements elsewhere in the code. Their primary use is to interrupt the switch statements and loops. These are further classified as break statement , continue statement and return statement.

break statement: As mentioned in the previous article, break is a keyword used within any control statements to terminate the execution of the current loop or any switch cases.

syntax:
break;
break<label>;
eg: public class abc{public static void main(String[] args){int a =1;for(int i=0; i < a; i++){System.out.println("value of i is : " + i); if(i==2){break;}}}o/p:
value of i is : 1

continue statement: continue being another keyword is typically used to continue the execution of the current executing loop with next iteration. When a continue statement is encountered in a looping statement, the execution control skips the rest of the statements in the looping block and directly jumps to the beginning of the loop. When used with a while and do-while loop statements, the execution control directly jumps to the condition, where as when used with for statement, the continue statement directly jumps to the modification(increment / decrement) of the for loop.

syntax:
continue;
continue<label>;
eg: public class abc{public static void main(String[] args){int a =1;for(int i=0; i < a; i++){System.out.print("value of i is : " + i);if(i==2)continue;System.out.println("class abc");}}o/p:
value of i is : 1 class abc
value of i is : 2 3 class abc

return statement: The return statement terminates the execution of a method and returns control to the calling method, with or without any return value[ based on the return type of the method is set to void or not]. A return statement cannot appear in a method whose return type is void.

syntax:
return expression(optional);
eg: public class abc{static int a = 10;public static void main(String... args){System.out.print("in main method");add(6);System.in.println("executed add method");sub(2);System.in.println("executed sub method");}public static int add(int i){int j = a+i;System.out.println("in add method");return j;}public static void sub(int i){ a-i;System.out.println("in subtract method");}}}
o/p:
in main method
in add method
executed add method
in subtract method
executed sub method

That is it on this article from my end. For any queries pr suggestion you can reach me here.

Until next time…

Peace Out!

Rupam Pawan Jha

--

--

Rupam Jha
JavaMadeTranquil

Senior DevOps Engineer. Automation Enthusiast. Sharing my experiences on AWS, CI/CD, Docker, Kubernetes, and IaC.