Control Statements In Java

Statements in java generally contain expression and end with a semi-colon.The two most important statements in java are 'sequential statements' and 'control statements'. These statements are executed by JVM one by one in a sequential manner hence they are called as sequential statements. If we want to write better and complex programs, we bette control on the flow of execution and this is possible by using control statements.

Types Of Control Statements

  • if...else statements
  • do...while statements
  • while loop
  • for loop
  • for-each loop
  • switch statements
  • break statements
  • continue statements
  • return statements

if….else Statements

These statements are used to perform a task depending on whether a given condition is true or false. Here the task represent a single statement or a group of statements. The condition is written inside the small braces ().

// To test if number is positive or negative
class Demo{
  public static void main(String[]args)
  {
int num = 2;  // declare and initialize num to 2
if(num == 0)
     System.out.println("It is zero");
else if (num > 0)
     System.out.println("num:"+ is positive);
else
    System.out.println("num:"+ is negative);
  }
}

In the above program if the first condition written after ifstatement is correct then the first statement is executed or if the condition is false then the second statement is executed and if the condition is still wrong in second statement also it will go to the next statement and execute it if it is true.

Output

c:\> javac Demo.java
c:\> java Demo
2 is positive

do….while Statements

This loop is used when there is a need to repeatedly execute a group of statements as long as a condition is true. If the condition is false, the repetition will be stopped and the flow of execution come out of do….while loop.

// To display numbers from 1 to 5
class Demo{
  public static void main(string[]args)
  {
    int x = 1;
    do{
      System.out.println(x);
      x++;
    } while(x<=5);
  }
}

Here, already the value of x is 1, so it will be displayed. Then x++ will increment the value of x by 1, hence the value becomes 2. Then the condition x <= 5 is tested. As the condition is true, the flow of execution will go back and then the value of x i.e 2 will be displayed. Then the x value is incremented and becomes 3. Since 3 is also <= 5, the flow goes back and displays x value. In this way, as long as x value doesn’t exceed 5, the loop repeats and hence we can see the numbers from 1 to 5.

Output

c:\> javac Demo.java
c:\> java Demo
1
2
3
4
5

while loop

The functioning of this loop is similar to do…while loop. This loop repeats a group of statements as long as a condition is true. In a do…while loop, the statements are executed without testing in the first time. From second time only the condition is observed. In a while loop, the condition is tested first and then only the statements are executed. This means it provides better control right from the beginning.

// To display numbers from 1 to 5
class Demo{
  public static void main(string[]args){
    int x = 1;
    while(x<=5)
    {
         System.out.println(x);  // display x
         x++;   // increments x
    }
  }
}

for loop

The for loop is same as do…while or while loop, but it is more compact syntactically. The for loop executes a group of statements as long as a condition is true. For loop is suitable for situations for the statements where the statements should be executed a fixed number of times.

// To display numbers from 1 to 5
class Demo{
  public static void main(string[]args){
    for( int x = 1; x <= 5; x++)
    {
         System.out.println(x);
    } 
  }
}

Output

c:\> javac Demo.java
c:\> java Demo
1
2
3
4
5

for - each loop

This loop is specifically designed to handle the elements of a collection. Collection represents a group of elements. For example, we can take an array as a collection or any class in java.util package can be considered as a collection. The reason is that an array stores a group of elements like integer values or strings. Similarly, java.util package are developed to handle a group of objects. The for- each loop repeatedly executes a group of statements for each element of the collection. It executes as many times as there are a number of elements in the collection.

class Demo{
 public static void main(string[]args)
 {
   int arr[] = {10, 20, 30, 40};  // declare an array with 4 elements
   for(int i : arr)  // use for each to retrieve elements from array
   {
       System.out.println(i);   // i represents each elemant of array
   }
 }
}

Output

c:\> javac Demo.java
c:\> java Demo
10
20
30
40

Switch Statements

When there are several options, and we have to choose only one option from the available ones, we can use switch statement. Depending on the selected option a particular task can be performed. A task represents one or more statements.

// To display color name depending on color value
 class Demo{
  public static void main(string[]args)
  {
     char color ='g'; // color is set to g
     switch(color)
     {
       case 'r' : System.out.println("Red");
       case 'g' : System.out.println("Green");
       case 'b' : System.out.println("Blue");
       default  : System.out.println("No color");
     }
   }
 }

Output

c:\> javac Demo.java
c:\> java Demo
Green
Blue
No color

The above output was supposed to be Green, but it is not as expected because when the color value is g it displayed green and after that it has come down to execute the rest of the statements under it leading to preceding output. The solution for this is to use break statement which helps to come out of the loop after displaying the desired value, it is shown below

// To display color name depending on color value
 class Demo{
  public static void main(string[]args)
  {
     char color ='g'; // color is set to g
     switch(color)
     {
       case 'r' : System.out.println("Red");
       break;
       case 'g' : System.out.println("Green");
       break;
       case 'b' : System.out.println("Blue");
       break;
       default  : System.out.println("No color");
     }
   }
 }

Output

c:\> javac Demo.java
c:\> java Demo
Green

Break Statements

The break statements are used in following ways

  • It is used inside a loop to come out of it.
  • It is used inside the switch block to come out of it.
  • Furthermore, it is used in nested blocks to go to the end of a block.

Continue Statements

These are used inside a loop to repeat the next iteration of the loop. When continue is executed, subsequent statements in the loop are not executed and control of execution goes back to the next repetition of the loop.

// Numbers in descending order
class Demo
{
  public static void main(String[]args)
  {
    for (int i = 10; i>= 1; i--)
    {
      if(i>5) continue; // go back in loop
      System.out.println(i+" ");
    }
  }
}

Output

c:\> javac Demo.java
c:\> java Demo
5
4
3
2
1

Return Statements

Return statement is used in a method to come out of it to the calling method. If we use return statement inside the main() method, then the entire program will be terminated, and we come out to the system prompt. Return method can be used as follows

return 1; // return 1 to calling method
return x; // return x value
return (x+y); // calculate the (x+y) value and returns it

Subscribe For More Content