Control Statements in Java

Introduction 

 
A program executes from top to bottom, except when we use control statements.   Then, we can control the order of execution of the program, based on logic and the values.
 
In Java, control statements can be divided into the following three categories:
  • Selection Statements
  • Iteration Statements
  • Jump Statements 

Selection Statements

 
Selection statements allow you to control the flow of program execution, on the basis of the outcome of an expression or state of a variable, known during runtime.
 
Selection statements can be divided into the following categories: 
  • The if and if-else statements
  • The if-else statements
  • The if-else-if statements
  • The switch statements 

The if statements

 
The first contained statement (that can be a block) of an if statement, only executes when the specified condition is true.  If the condition is false and there is no keyword, then the first contained statement will be skipped and execution continues with the rest of the program.  The condition is an expression that returns a boolean value.
 
Example 
  1. package com.deepak.main;  
  2. import java.util.Scanner;  
  3. public class IfDemo {  
  4.  public static void main(String[] args) {  
  5.   int age;  
  6.   Scanner inputDevice = new Scanner(System.in);  
  7.   System.out.print("Please enter Age: ");  
  8.   age = inputDevice.nextInt();  
  9.   if (age > 18)  
  10.    System.out.println("above 18 ");  
  11.  }  
  12. }   
Output
 
The if statements 
 

The if-else statements

 
In if-else statements, if the specified condition in the if the statement is false, then the statement after the else keyword (that can be a block) will execute.
 
Example
  1. package com.deepak.main;  
  2. import java.util.Scanner;  
  3. public class IfElseDemo {  
  4.  public static void main(String[] args) {  
  5.   int age;  
  6.   Scanner inputDevice = new Scanner(System.in);  
  7.   System.out.print("Please enter Age: ");  
  8.   age = inputDevice.nextInt();  
  9.   if (age >= 18)  
  10.    System.out.println("above 18 ");  
  11.   else  
  12.    System.out.println("below 18");  
  13.  }  
  14. }   
Output
 
The if else statements 
 

The if-else-if statements

 
The statement following the else keyword can be another if or if-else statement.
 
That would look like this:
  1. if (condition)  
  2.  statements;  
  3. else if (condition)  
  4.  statements;  
  5. else if (condition)  
  6.  statement;  
  7. else  
  8.  statements;  
Whenever the condition is true, the associated statement will be executed and the remaining conditions will be bypassed.  If none of the conditions are true, then the else block will execute.
 
Example
  1. package com.deepak.main;  
  2. import java.util.Scanner;  
  3. public class IfElseIfDemo {  
  4.  public static void main(String[] args) {  
  5.   int age;  
  6.   Scanner inputDevice = new Scanner(System.in);  
  7.   System.out.print("Please enter Age: ");  
  8.   age = inputDevice.nextInt();  
  9.   if (age >= 18 && age <= 35)  
  10.    System.out.println("between 18-35 ");  
  11.   else if (age > 35 && age <= 60)  
  12.    System.out.println("between 36-60");  
  13.   else  
  14.    System.out.println("not matched");  
  15.  }  
  16. }   
Output
 
if else if Statements 
 

The Switch Statements

 
The switch statement is a multi-way branch statement.  The switch statement of Java is another selection statement that defines multiple paths of execution, of a program.  It provides a better alternative than a large series of if-else-if statements.
 
Example
  1. package com.deepak.main;  
  2. import java.util.Scanner;  
  3. public class SwitchDemo {  
  4.  public static void main(String[] args) {  
  5.   int age;  
  6.   Scanner inputDevice = new Scanner(System.in);  
  7.   System.out.print("Please enter Age: ");  
  8.   age = inputDevice.nextInt();  
  9.   switch (age) {  
  10.    case 18:  
  11.     System.out.println("age 18");  
  12.     break;  
  13.    case 19:  
  14.     System.out.println("age 19");  
  15.     break;  
  16.    default:  
  17.     System.out.println("not matched");  
  18.     break;  
  19.   }  
  20.  }  
  21. }   
Output
 
The Switch Statements  
 
An expression must be of a type of byte, short, int, or char.  Each of the values specified in the case statement must be of a type compatible with the expression.  Duplicate case values are not allowed.  The break statement is used inside the switch to terminate a statement sequence.  The break statement is optional in the switch statement.
 

Iteration Statements

 
Repeating the same code fragment several times, until a specified condition is satisfied, is called iteration.  Iteration statements execute the same set of instructions, until a termination condition is met.
 
Java provides the following loop for iteration statements: 
  • The while loop
  • The for loop
  • The do-while loop
  • The for-each loop

The while loop

 
It continually executes a statement (that is usually a block) while a condition is true.  The condition must return a boolean value.
 
Example 
  1. package com.deepak.main;  
  2. public class WhileDemo {  
  3.  public static void main(String[] args) {  
  4.   int i = 0;  
  5.   while (i < 5) {  
  6.    System.out.println("Value :: " + i);  
  7.    i++;  
  8.   }  
  9.  }  
  10. }   
Output
 
The while loop 
 

The do-while loop

 
The only difference between a while and a do-while loop, is that do-while evaluates its expression at the bottom of the loop, instead of the top.  The do-while loop executes at least one time, then it will check the expression prior to the next iteration.
 
Example
  1. package com.deepak.main;  
  2. public class DoWhileDemo {  
  3.  public static void main(String[] args) {  
  4.   int i = 0;  
  5.   do {  
  6.    System.out.println("value :: " + i);  
  7.    i++;  
  8.   }  
  9.   while (i < 5);  
  10.  }  
  11. }   
Output
 
The do while loop 
 

The for loop

 
A for loop executes a statement (that is usually a block) as long as the boolean condition evaluates to true.  A for loop is a combination of the three elements initialization statement, boolean expression and increment or decrement statement.
 
Syntax:
  1. for ( < initialization > ; < condition > ; < increment or decrement statement > ) {  
  2.  < block of code >  
  3. }  
The initialization block executes first before the loop starts. It is used to initialize the loop variable.
 
The condition statement evaluates every time prior to when the statement (that is usually a block) executes.  If the condition is true then only the statement (that is usually a block) will execute.
 
The increment or decrement statement executes every time, after the statement (that is usually a block).
 
Example
  1. package com.deepak.main;  
  2. public class WhileDemo {  
  3.  public static void main(String[] args) {  
  4.   int i = 0;  
  5.   while (i < 5) {  
  6.    System.out.println("Value :: " + i);  
  7.    i++;  
  8.   }  
  9.  }  
  10. }   
Output
 
The for loop 
 

For each loop

 
This was introduced in Java 5.  This loop is basically used to traverse the array or collection elements. 
 
Example
  1. package com.deepak.main;  
  2. public class ForEachDemo {  
  3.  public static void main(String[] args) {  
  4.   int[] i = {  
  5.    1,  
  6.    2,  
  7.    3,  
  8.    4,  
  9.    5  
  10.   };  
  11.   for (int j: i) {  
  12.    System.out.println("value :: " + j);  
  13.   }  
  14.  }  
  15. }   
Output
 
The For each loop 
 

Jump Statements

 
Jump statements are used to unconditionally transfer the program control to another part of the program.
 
Java provides the following jump statements: 
  • break statement
  • continue statement
  • return statement 

Break Statement

 
The break statement immediately quits the current iteration and goes to the first statement, following the loop.  Another form of break is used in the switch statement.
 
The break statement has the following two forms: 
  • Labelled Break Statement
  • Unlabeled Break Statement
Unlabeled Break Statement: This is used to jump program control out of the specific loop on the specific condition.
 
Example
  1. package com.deepak.main;  
  2. public class UnLabeledBreakDemo {  
  3.  public static void main(String[] args) {  
  4.   for (int  
  5.    var = 0;  
  6.    var < 5;  
  7.    var ++) {  
  8.    System.out.println("Var is : " +  
  9.     var);  
  10.    if (var == 3)  
  11.     break;  
  12.   }  
  13.  }  
  14. }   
Output
 
Break Statement 
 

Labelled Break Statement

 
This is used for when we want to jump the program control out of nesting loops or multiple loops.
 
Example
  1. package com.deepak.main;  
  2. public class LabeledBreakDemo {  
  3.  public static void main(String[] args) {  
  4.   Outer: for (int var1 = 0; var1 < 5; var1++) {  
  5.    for (int var2 = 1; var2 < 5; var2++) {  
  6.     System.out.println("var1:" + var1 + ", var2:" + var2);  
  7.     if (var1 == 3)  
  8.      break Outer;  
  9.    }  
  10.   }  
  11.  }  
  12.  
Output
 
Labeled Break Statement 
 

Continue Statement

 
The continue statement is used when you want to continue running the loop, with the next iteration, and want to skip the rest of the statements of the body, for the current iteration.
 
The continue statement has the following two forms: 
  • Labelled Continue Statement
  • Unlabeled Continue Statement 

Unlabeled Continue Statement

 
This statement skips the current iteration of the innermost for, while and do-while loop.
 
Example 
  1. package com.deepak.main;  
  2. public class UnlabeledContinueDemo {  
  3.  public static void main(String[] args) {  
  4.   for (int var1 = 0; var1 < 4; var1++) {  
  5.    for (int var2 = 0; var2 < 4; var2++) {  
  6.     if (var2 == 2)  
  7.      continue;  
  8.     System.out.println("var1:" + var1 + ", var2:" + var2);  
  9.    }  
  10.   }  
  11.  }  
  12. }   
Output
 
Labeled Continue Statement 
 

Labeled Continue Statement

 
This statement skips the current iteration of the loop with the specified label.
 
Example
  1. package com.deepak.main;  
  2. public class LabeledContinueDemo {  
  3.  public static void main(String[] args) {  
  4.   Outer: for (int var1 = 0; var1 < 5; var1++) {  
  5.    for (int var2 = 0; var2 < 5; var2++) {  
  6.     if (var2 == 2)  
  7.      continue Outer;  
  8.     System.out.println("var1:" + var1 + ", var2:" + var2);  
  9.    }  
  10.   }  
  11.  }  
  12. }   
Output
 
Unlabeled Continue Statement 
 

Return Statement

 
The return statement is used to immediately quit the current method and return to the calling method.  It is mandatory to use a return statement for non-void methods to return a value. 
 
Example
  1. package com.deepak.main;  
  2. public class ReturnDemo {  
  3.  public static void main(String[] args) {  
  4.   ReturnDemo returnDemo = new ReturnDemo();  
  5.   System.out.println("No : " + returnDemo.returnCall());  
  6.  }  
  7.  int returnCall() {  
  8.   return 5;  
  9.  }  
  10. }   
Output
 
output 


Similar Articles