Java Control Statement

In this section, we are going to discuss the control statements. Different types of control statements: the selection statements (if-then, if-then-else and switch), looping statements (while, do-while and for) and jumping statements (break, continue,return).

Java Control Statement

In this section, we are going to discuss the control statements. Different types of control statements: the selection statements (if-then, if-then-else and switch), looping statements (while, do-while and for) and jumping statements (break, continue,return).

Java Control Statement

Java Control Statement

Control flow statement is a statement that is used to break the sequential execution of statement. control flow statement, controls the order of execution of the program based on the condition. Control flow statement is categorized into three categories, they are as follows:

  • Selection statement :  Java supports two selection statements, if and switch statement.
 1. The if statement : The if statement allow you to execute a block of code if the condition is true. If the value is false then it will execute the rest of the code. You can put a single statement or block of code within if statement. The simple if statement has following syntax:
if(conditional expression)
statement;

  2. The if-else statement : It is an upgraded version of if statement. If the condition in if,  become false then the statement in the else get executed. You can put a single code of statement or multiple code of statement  within the if-else statement. The following is the syntax as follows:

 if(condition)
  {
    //statement;
 }else
   {
    //statement;}
    

 3.  Switch case statement : The switch statement is a multi-way branching statement with several choices. switch is more easier then if-else statement. Suppose if you want to check more then ten statement then you need to check ten times, but using switch case you can put choices within the switch. The following is the syntax as follows:

switch (integral expression>) {
case label1: 
case label2: 
?
case labeln: 
default: 
} // end switch

Switch start with keyword switch and take only integral expression based on the input it will match from the cases. While executing the switch statement in middle if you want to come out from the case then use break keyword  to exit from the switch case.

Example : A simple code for selection statement.

public class Selection {
public static void main(String[] args) {
		int a = 10, b = 50, c = 20;
		int flag = -1;
		if (a > b && a > c) {
			flag = 1;
		} else if (b > c) {
			flag = 2;
		} else {
			flag = 3;
		}
		switch (flag) {
		case 1:
			System.out.println("a is the greater");
			break;
		case 2:
			System.out.println("b is the greater");
			break;
		case 3:
			 System.out.println("c is the greater");
			break;
		default:
			System.out.println("Invalid input");
		}
	}
}

When you compile and execute the program the output will be as follows:

Download Source Code

  • Looping statement : When we need to execute a block of code several number of times in a program, it is referred as looping statement. The loop statement will execute till condition become true,  Java has following looping statement:

for loop.

while loop.

do-while loop.

1. The for loop : A for loop is a control structure that allow to execute code specific number of times. A for loop is useful when you know how many times you want to execute the code in your program

Syntax : The syntax of a for loop is :

for(initialization; condition; increment or decrement)
{
 //statement;
}

2. The while loop : A while loop is a control structure that allow you to execute a task certain number of times.

Syntax : The while loop syntax is :

while(condition)
  {
   //statement;
  }
  

When the condition is true then only statement inside the while loop will execute. The loop might not run when the while condition is false. If the condition is false then the statement after while is executed.

3. The do-while loop : It is similar to while loop but one difference is that do-while will at least execute once without checking condition.

Syntax : The do-while loop syntax is as follows :

do
{
//statement;
while(test condition);}

Example : A simple program using do-while loop.

public class Dowhile {

   public static void main(String args[]){
      int a = 5;

      do{
         System.out.print("value of x : " + a );
         a++;
         System.out.print("\n");
      }while( a < 10 );
   }
} 

Similarly, for and while loop is used,  if the same code is written using for loop it look like as follows:

for(int a=5;a<10;a++)
 {
  System.out.println("value of a : " +a);
 }

So, When you compile and execute the above program :

Download Source Code

  • Jumping Statement : Java support three types of jumping statement break, continue, and return. These statement transfer control to another part of the program.    

1.The break statement : The break statement is used to exit from the loop. break is used inside a loop or in a switch statement. The break keyword is used to stop the execution from the innermost for loop and control will come out to the outermost for  loop.

Syntax :  The syntax of break statement is one line inside a loop.

break;

Example  :

 int a[]={10,20,30,40,50};
  for(i=0;i<a.length;i++)
  {
   if(a[i]==key)
   {
    System.out.println("value of a" +a);
     break;
     }
   }
 

2.The continue statement : The continue statement is used in any looping statement. It transfer the control to the next iteration of the loop. In other word this statements is used to skip the current iteration of the loop and execute the next statement.

Syntax

continue ;

0

Example :

int a[]={10,20,30,40,50};
  for(i=0;i<a.length;i++)
  {
   if(a[i]==key)
     continue;
   }

3. The Return statement : This jumping statement is used to transfer the control to the caller of the method. The return method is used to explicitly return from a method. Therefore return statement immediately terminate the method in which it is executed.

Example :

1
class Return
 {
  public static void main(String args[])
  {
   boolean t=true;
   if(t)
   return ;
   System.out.println("control wont execute");
   }
 }