Java Control Statements help to transfer control to a different part of a program. Java has three types of control statements, decision, iteration, and jump statements. In this article, I will be covering these control statements.

  1. Decision Statements
  2. Iteration Statements
  3. Jump Statements

Java Control Statements

Decision Statements

Decision statements help to change the flow of execution based on a condition. Java has two decision statements, if and switch.

If Statement

An if statement evaluates a boolean condition. If it is true, the code within the if statement gets executed. The if statement can have an optional else part that gets executed if the condition is false.

Sample Code

String str1 = "Hello World"; //Line 1

if (str1.startsWith("Hello")) {//Line 2
	System.out.println("str1 starts with Hello");//Line 3
} 
else {//Line 4
	System.out.println("str1 does not start with Hello");//Line 5
}

 

  • Line 1 creates a String variable str1 with the value Hello World
  • Line 2 uses the if statement to check if str1 starts With Hello. Since this is true, Line 3 is executed
  • Line 4 specifies an else The code in the else block gets executed only if the condition in the if the statement is false.

Output:

str1 starts with Hello

Switch Statement

A switch statement is a multi-way branch statement. It can be used to check a value and execute one of several blocks of code.

Sample Code

String orderStatus="IN_TRANSIT"; //Line 1
switch (orderStatus) { //Line 2
	case "PLACED": //Line 3
		System.out.println("Your order is successfully placed!");
		break;
	case "IN_TRANSIT": //Line 4
		System.out.println("Your order is dispatched!");
		break;
	case "DELIVERED": //Line 5
		System.out.println("Your order has been delivered");
		Break; 
	default:
		System.out.println("Invalid order status specified!");
		break;
	}
  • Line 1 declares a variable called orderStatus and assigns it the value ‘IN_TRANSIT’
  • Line 2 specifies the switch keyword with orderStatus
  • Lines 3,4,5 specify case Each case statement is followed by a value.
  • The variable in the switch statement (orderStatus) is compared with the value with each case statement and as soon as a match is found, the code in that case block is executed
  • Since orderStatus is assigned the value ‘IN_TRANSIT’, the case statement at Line 4 gets executed
  • The break statement in each case statement is optional, it prevents the switch block from checking further case statements
  • The default statement at the end is optional, it executes if the variable in the switch statement does not match any case

Output:

Your order is dispatched!

Iteration Statements

Iteration statements can be used to repeat a block of code as long as a condition is true. Java has four iteration statements; while, do-while, for, and for-each.

While

The while loop checks for a condition that evaluates to a boolean value. It repeats the code in the body of the loop as long as the condition is true.

Sample Code

int count = 0; //Line 1
		
while (count < 4) { //Line 2
System.out.println("Hello World!"); //Line 3
	count++; //Line 4
}
System.out.println("Loop completed!"); //Line 5
  • Line 1 declares a variable count and initializes it to 0
  • Line 2 uses a while It checks the condition count < 4 . Since this is true, the loop is entered and Line 3 is executed
  • Line 4 then increments count and the condition at Line 2 is checked again
  • This continues till the condition at Line 2 becomes false after which the loop is exited and Line 5 gets executed

Output:

Hello World!
Hello World!
Hello World!
Hello World!
Loop completed!

Do-While Loop

The do-while loop also repeats a block of code as long as a condition is true. However, it checks the condition at the end of the loop, ensuring that the loop is executed at least once.

Sample Code

int count = 5; //Line 1

do { //Line 2
	System.out.println("Hello World!"); //Line 3
	count++; //Line 4
}while (count < 4); //Line 5
System.out.println("Loop completed!"); //Line 6
  • Line 1 declares a variable count and initializes it to 5
  • Line 2 consists of the do keyword
  • Line 5 is the while It consists of the keyword while followed by the condition count < 4.
  • The code in the body of the loop (Lines 3-4) is executed after which the condition is checked. The loop is exited since the condition is false

Output:

Hello World!
Loop completed!

If a while loop was used here instead of the do-while loop, the body of the loop would not have been executed even once.

For Loop

A for loop iterates over a range of values.

Sample Code

for(int i=0; i < 4; i++) { //Line 1
	System.out.println("Hello World!"); //Line 2
}
System.out.println("Loop completed!");
  • Line 1 consists of the for
  • First, the loop control variable i is initialized to 0.
  • Next, the condition i<4 is checked and since it is true, the loop is entered and Line 2 gets executed
  • Finally, the variable i is incremented and the condition is checked again
  • This is continued till i reaches the value 4 at which point the condition becomes false so the loop is exited.

Output:

Hello World!
Hello World!
Hello World!
Hello World!
Loop completed!

For-each Loop

The for-each loop is used to iterate over an array or collection. It repeats the body of the loop for each element in the input array or collection.

Sample Code

String[] colours  = {"red","blue","green","black"}; // Line 1
for(String colour:colours) { //Line 2
	System.out.println("The next colour is "+colour); //Line 3
} //Line 4
System.out.println("Loop completed!");
  • Line 1 declares a colours array and initializes it to some values
  • Lines 2-4 specify a for-each loop
  • The loop retrieves each value from the colours array, assigns it to the colour variable and executes the body of the loop
  • The loop is exited once all the values from the colours array are exhausted

Output:

The next colour is red
The next colour is blue
The next colour is green
The next colour is black
Loop completed!

Jump Statements

Jump statements are used to transfer control to a different part of the code. Java supports two jump statements, break and continue.

Break

The break statement can be used from within a loop to terminate a loop and transfer control outside the loop. It can also be used in a switch statement to terminate a case statement as seen above.

Sample Code

String[] colours  = {"red","blue","green","black"}; //Line 1
		
for(String colour:colours) { //Line 2
	if(colour.equals("green")) { //Line 3
		System.out.println("Found colour green, exiting loop!");
		break; //Line 5
	}
	System.out.println("The next colour is "+colour); //Line 6
} //Line 7
System.out.println("Loop completed!");
  • Line 1 declares a colours array and initializes it to some values
  • Lines 2-7 specify a for-each loop that iterates through the colours array
  • Line 3 uses an if statement to check if the current colour is green and if so, it executes a break at Line 5
  • This causes the loop to be terminated as soon as the colour green is found

Output:

The next colour is red
The next colour is blue
Found colour green, exiting loop!
Loop completed!

Continue

The continue statement can be used from within a loop to skip the current iteration of the loop but continue the loop for the next iteration.

Sample Code

String[] colours  = {"red","blue","green","black"}; //Line 1
		
for(String colour:colours) { //Line 2
	if(colour.startsWith("b")) { //Line 3
		continue; //Line 4
	}
	System.out.println("The next colour is "+colour); //Line 5
} //Line 6
System.out.println("Loop completed!");
  • Line 1 declares a colours array and initializes it to some values
  • Lines 2-6 specify a for-each loop that iterates through the colours array
  • Line 3 uses an if statement to check if the current colour starts with the letter b and if so, it executes a continue at Line 4
  • This causes the loop to be skipped for colours that start with the letter b

Output:

The next colour is red
The next colour is green
Loop completed!

Conclusion

So, just to summarise, Java’s control statements can be classified into the decision, iteration, and jump statements. Decision statements change the flow of control based on a condition. Iteration statements repeat a block of code as long as a condition is true. Jump statements help to transfer control to a different part of the code.