×
>
<

Aptitude

Control Structures

Java provides statements that can be used to control the flow of Java code. Such statements are called control flow statements. It is one of the fundamental features of Java, which provides a smooth flow of program.


Java provides three types of control flow statements.

  1. Decision Making
  2. Loop Statements
  3. Jump Statements
Decision Making statements ?

These statements decide which statement to execute and when. Decision-making statements evaluate the Boolean expression and control the program flow depending upon the result of the condition provided.

Generally these are of three types :

  1. if
  2. if-else
  3. switch case
If statement
  • An if statement is used to make a comparison between two or more set of values, and if that condition or comparison is satisfied then the part of the code written in the if statement will get executed .
  • if statement block’s is defined by curly braces{}. When the condition is true then the statement given in this block is executed.
  • If the condition is false then skip the entire block.
Syntax of if statement
  
if(condition)
{
      //statements to be executed if condition is true.
}
  

Example :

  
public class Student {    
    public static void main(String[] args) {    
        int x = 10;    
        int y = 12;    
        if(x < y) {    
            System.out.println("x is less than y");    
        }    
    }      
} 
  
output
  
x is less than y
  
if else statement

In some cases when if condition is not satisfied an if else statement will be the next statement that the compiler will now go to and execute if conditions matched.

if else statement is considered to be part of an if statement. But else block is added in it. The statement given in Else block is executed when the condition of if is false.

Syntax of if else statement
  
public class Student {  
    public static void main(String[] args) {  
        int x = 12;  
        int y = 8;  
        if(x < y) {  
            System.out.println("x is less than y");  
        }else {  
            System.out.println("x is greater than y");  
        }  
    }  
} 
  
output
  
x is greater than y
  
else if statement

if you want to put another condition in the middle of if and else, then you can do this by defining else if block.

else-if statement
  
if(condition)
{     
      //statement to be executed if condition is true
}
else if(condition)
{  
    //statement to be executed if else if condition is true
}
else(condition)
{
    // statement to be executed if, neither if nor else if is true.
}
  

Example :

  
public class Student {  
    public static void main(String[] args) {  
        String city = "Mumbai";  
        if(city == "Delhi") {  
            System.out.println("city is Delhi");  
        }else if (city == "Kolkata") {  
            System.out.println("city is Kolkata");  
        }else if(city == "Mumbai") {  
            System.out.println("city is Mumbai");  
        }else {  
            System.out.println(city);  
        }  
    }  
}
  
output
  
city is Mumbai
  
'

There are a lot of other comparisons we can do in an if statement like

  
if(a==b)   //if a is equal to b
if(a!=b)  //if a is not equal to b
if(a==b && a>5)   //if a is equal to b and a is greater than 5
if(a==b || a>5)  //if a is equal to b or a is greater than 5
  
Switch cases ?

In a switch case we provide (or take input from the user) a key and give it to the switch and the switch compares that value with its cases and if it is equal it will execute that case and if no cases match then it runs a default case.

Syntax of Switch case statement
  
switch(value)
{    
   case (condition):
       //statement to be executed if condition is true
       break;
   case (condition):
      //statement to be executed if condition is true
      break;
   default:
     //statement to be executed if none of the case is true
}
  

Example :

  
public class Student implements Cloneable {  
    public static void main(String[] args) {  
        int num = 3;  
        switch (num){  
            case 0:  
                System.out.println("number is 0");  
            break;  
            case 1:  
                System.out.println("number is 1");  
                break; 
            case 2:  
                System.out.println("number is 2");  
                break;
            case 3:  
                System.out.println("number is 3");  
                break;
            default:  
                System.out.println(num);  
        }  
    }  
}
  
output
  
number is 3
  
Loop Statements

Sometimes we need to execute the block of code repeatedly while some condition evaluates to true. However, loop statements are used to execute the set of instructions in a repeated order. The execution of the set of instructions depends upon a particular condition.

Here, for loop is similar to C and C++. It enables us to initialize the loop variable, check the condition, and increment/decrement in a single line of code. We use the for loop only when we exactly know the number of times, we want to execute the block of code.

Syntax :

  
for(initialization, condition, increment/decrement) {    
    //block of statements    
}
  

Example :

  
public class Calculattion {  
    public static void main(String[] args) {  
         
        int sum = 0;  
        for(int j = 1; j<=10; j++) {  
            sum = sum + j;  
        }  
        System.out.println("The sum of first 10 natural numbers is " + sum);  
    }  
}
  
output
  
The sum of first 10 natural numbers is 55
  
While Loop

The while loop is used to iterate over the number of statements multiple times.But it is used in case where we don't know the number of iterations in advance, it is recommended to use a while loop. Unlike for loop, the initialization and increment/decrement doesn't take place inside the loop statement in while loop

It is also known as the entry-controlled loop since the condition is checked at the start of the loop. If the condition is true, then the loop body will be executed; otherwise, the statements after the loop will be executed..

Syntax :

  
while(condition){    
    //looping statements    
}
  

Example :

  
public class Calculation {    
    public static void main(String[] args) {    
       
        int i = 0;    
        System.out.println("Printing the list of first 10 even numbers \n");    
        
        while(i<=10) {    
            System.out.println(i);    
            i = i + 2;    
        }    
    }    
}
  
output
  
Printing the list of first 10 even numbers 

0
2
4
6
8
10
  
Do While Loop

The do-while loop is quite same like the while loop, insted it checks the condition at the end of the loop after executing the loop statements. When the number of iteration is not known and we have to execute the loop at least once, we can use do-while loop.

It is also known as the exit-controlled loop since the condition is not checked in advance. The syntax of the do-while loop is given below.

Syntax :

  
do{    
    //statements    
} while (condition);   
  

Example :

  
public class Calculation {    
    public static void main(String[] args){
        int i = 0;    
        System.out.println("Printing the list of first 10 even numbers \n");    
        do {    
            System.out.println(i);    
            i = i + 2;    
        }while(i<=10);    
    }    
}
  
output
  
Printing the list of first 10 even numbers 
0
2
4
6
8
10
  
Jump Statements

These statements are used to transfer the control of the program to the specific statements. In other words, jump statements transfer the execution control to the other part of the program. There are two types of jump statements in Java, i.e., break and continue.

Java break statement

As the name suggests, the break statement is used to break the current flow of the program and transfer the control to the next statement outside a loop or switch statement. However, it breaks only the inner loop in the case of the nested loop.

The break statement cannot be used independently in the Java program, i.e., it can only be written inside the loop control statements.

The break statement example with for loop

  
public class BreakExample {  
    public static void main(String[] args) {  
        for(int i = 0; i<= 10; i++) {  
            System.out.println(i);  
            if(i==6) {  
                break;  
            }  
        }  
    }  
}   
  
Output
  
0
1
2
3
4
5
6
  
Java continue statement

Unlike break statement, the continue statement doesn't break the loop, whereas, it skips the specific part of the loop and jumps to the next iteration of the loop immediately.

The continue statement cannot be used independently in the Java program like break , i.e., it can only be written inside the loop control statements.

The continue statement example with for loop

  
public class ContinueExample {  
  
    public static void main(String[] args) {  
        
        for (int j = 0; j<=5; j++) {  
              
            if(j == 2) {  
                continue;  
            }  
            System.out.println(j);  
        }  
    }  
     
  
}   
  
Output
  
0
1
3
4
5