In Focus

Control Statement in Java With Example

In this article you will learn about control statements in Java by example. In Java control statements can be divided into the following three categories: Selection Statements, Iteration Statements and Jump Statements.

A program executes from top to bottom except when we use control statements, we can control the order of execution of the program, based on logic and 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 not else 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.   
  3. import java.util.Scanner;  
  4.   
  5. public class IfDemo  
  6. {  
  7.     public static void main(String[] args) {  
  8.           
  9.         int age;  
  10.         Scanner inputDevice = new Scanner(System.in);  
  11.         System.out.print("Please enter Age: ");  
  12.         age = inputDevice.nextInt();  
  13.         if(age > 18)  
  14.             System.out.println("above 18 ");  
  15.     }  
  16.   
  17. }  
Output:

The if statements


The if-else statements

In if-else statements, if the specified condition in the if statement is false, then the statemet after the else keyword (that can be a block) will execute.

Example
  1. package com.deepak.main;  
  2.   
  3. import java.util.Scanner;  
  4.   
  5. public class IfElseDemo  
  6. {  
  7.     public static void main( String[] args )  
  8.     {  
  9.         int age;  
  10.         Scanner inputDevice = new Scanner( System.in );  
  11.         System.out.print( "Please enter Age: " );  
  12.         age = inputDevice.nextInt();  
  13.         if ( age >= 18 )  
  14.             System.out.println( "above 18 " );  
  15.         else  
  16.             System.out.println( "below 18" );  
  17.     }  
  18. }  
Output

The if else statements

The if-else-if statements

This statement following the else keyword can be another if or if-else statement.

That would looks like this:

if(condition)
   statements;
else if (condition)
   statements;
else if(condition)
   statement;
else
   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.   
  3. import java.util.Scanner;  
  4.   
  5. public class IfElseIfDemo  
  6. {  
  7.     public static void main( String[] args )  
  8.     {  
  9.         int age;  
  10.         Scanner inputDevice = new Scanner( System.in );  
  11.         System.out.print( "Please enter Age: " );  
  12.         age = inputDevice.nextInt();  
  13.         if ( age >= 18 && age <=35 )  
  14.             System.out.println( "between 18-35 " );  
  15.         else if(age >35 && age <=60)  
  16.             System.out.println("between 36-60");  
  17.         else  
  18.             System.out.println( "not matched" );  
  19.     }  
  20. }  
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.   
  3. import java.util.Scanner;  
  4.   
  5. public class SwitchDemo  
  6. {  
  7.     public static void main( String[] args )  
  8.     {  
  9.         int age;  
  10.         Scanner inputDevice = new Scanner( System.in );  
  11.         System.out.print( "Please enter Age: " );  
  12.         age = inputDevice.nextInt();  
  13.         switch ( age )  
  14.         {  
  15.             case 18:  
  16.                 System.out.println( "age 18" );  
  17.                 break;  
  18.             case 19:  
  19.                 System.out.println( "age 19" );  
  20.                 break;  
  21.             default:  
  22.                 System.out.println( "not matched" );  
  23.                 break;  
  24.         }  
  25.     }  
  26. }  
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 be a block) while a condition is true. The condition must return a boolean value.

Example

  1. package com.deepak.main;  
  2.   
  3. public class WhileDemo  
  4. {  
  5.     public static void main( String[] args )  
  6.     {  
  7.         int i = 0;  
  8.         while ( i < 5 )  
  9.         {  
  10.             System.out.println( "Value :: " + i );  
  11.             i++;  
  12.         }  
  13.     }  
  14. }  
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.   
  3. public class DoWhileDemo  
  4. {  
  5.     public static void main( String[] args )  
  6.     {  
  7.         int i = 0;  
  8.         do  
  9.         {  
  10.             System.out.println( "value :: " + i );  
  11.             i++;  
  12.         }  
  13.         while ( i < 5);  
  14.     }  
  15. }  
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:

for(<initialization>;<condition>;<increment or decrement statement>){
<block of code>
}

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 be 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.   
  3. public class WhileDemo  
  4. {  
  5.     public static void main( String[] args )  
  6.     {  
  7.         int i = 0;  
  8.         while ( i < 5 )  
  9.         {  
  10.             System.out.println( "Value :: " + i );  
  11.             i++;  
  12.         }  
  13.     }  
  14. }  
Output

The for loop

The 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.   
  3. public class ForEachDemo  
  4. {  
  5.     public static void main( String[] args )  
  6.     {  
  7.         int[] i =  
  8.         { 12345 };  
  9.         for ( int j : i )  
  10.         {  
  11.             System.out.println( "value :: " + j );  
  12.         }  
  13.     }  
  14. }  
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:

  • Labeled 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.   
  3. public class UnLabeledBreakDemo  
  4. {  
  5.     public static void main( String[] args )  
  6.     {  
  7.         for ( int var = 0; var < 5; var++ )  
  8.         {  
  9.             System.out.println( "Var is : " + var );  
  10.             if ( var == 3 )  
  11.                 break;  
  12.         }  
  13.     }  
  14. }  
Output

Break Statement

Labeled Break Statement: This is used for when we want to jump the program control out of nested loops or multiple loops.

Example
  1. package com.deepak.main;  
  2.   
  3. public class LabeledBreakDemo  
  4. {  
  5.     public static void main( String[] args )  
  6.     {  
  7.         Outer: for ( int var1 = 0; var1 < 5; var1++ )  
  8.         {  
  9.             for ( int var2 = 1; var2 < 5; var2++ )  
  10.             {  
  11.                 System.out.println( "var1:" + var1 + ", var2:" + var2 );  
  12.                 if ( var1 == 3 )  
  13.                     break Outer;  
  14.             }  
  15.         }  
  16.     }  
  17. }  
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: 
  • Labeled 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.   
  3. public class UnlabeledContinueDemo  
  4. {  
  5.     public static void main( String[] args )  
  6.     {  
  7.         for ( int var1 = 0; var1 < 4; var1++ )  
  8.         {  
  9.             for ( int var2 = 0; var2 < 4; var2++ )  
  10.             {  
  11.                 if ( var2 == 2 )  
  12.                     continue;  
  13.                 System.out.println( "var1:" + var1 + ", var2:" + var2 );  
  14.             }  
  15.         }  
  16.     }  
  17. }  
Example

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.   
  3. public class LabeledContinueDemo  
  4. {  
  5.     public static void main( String[] args )  
  6.     {  
  7.         Outer: for ( int var1 = 0; var1 < 5; var1++ )  
  8.         {  
  9.             for ( int var2 = 0; var2 < 5; var2++ )  
  10.             {  
  11.                 if ( var2 == 2 )  
  12.                     continue Outer;  
  13.                 System.out.println( "var1:" + var1 + ", var2:" + var2 );  
  14.             }  
  15.         }  
  16.     }  
  17. }  
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.   
  3. public class ReturnDemo  
  4. {  
  5.     public static void main( String[] args )  
  6.     {  
  7.         ReturnDemo returnDemo = new ReturnDemo();  
  8.         System.out.println( "No : " + returnDemo.returnCall() );  
  9.     }  
  10.   
  11.     int returnCall()  
  12.     {  
  13.         return 5;  
  14.     }  
  15. }  
Output

output