Java Control Statements | Control Flow in Java
Java compiler executes the code from top to bottom. The statements in the code are executed according to the order in which they appear. However, Java provides statements that can be used to control the flow of Java code. Such statements are called control flow statements. It is one of the fundamental features of Java, which provides a smooth flow of program.
Java provides three types of control flow statements:
- Decision Making statements
- if statements
- switch statement
- Loop statements
- do while loop
- while loop
- for loop
- for-each loop
- Jump statements
- break statement
- continue statement
Decision-Making Statements:
As the name suggests, decision-making statements decide which statement to execute and when. Decision-making statements evaluate the Boolean expression and control the program flow depending upon the result of the condition provided. There are two types of decision-making statements in Java, i.e., If statement and switch statement.
1) If Statement:
In Java, the "if" statement is used to evaluate a condition. The control of the program is diverted depending upon the specific condition. The condition of the If statement gives a Boolean value, either true or false. In Java, there are four types of if-statements given below.
- Simple if statement
- if-else statement
- if-else-if ladder
- Nested if-statement
1) Simple if statement:
It is the most basic statement among all control flow statements in Java. It evaluates a Boolean expression and enables the program to enter a block of code if the expression evaluates to true.
Syntax of if statement is given below.
Syntax Example
if(condition) {
statement 1; // executes when condition is true
}
Consider the following example in which we have used the if statement in the Java code.
Code Example
public class Student {
public static void main(String[] args) {
int x = 10;
int y = 12;
if(x + y > 20) {
System.out.println("x + y is greater than 20");
}
}
}
Output
2) if-else statement
The if-else statement is an extension to the if-statement, which uses another block of code, i.e., else block. The else block is executed if the condition of the if-block is evaluated as false.
Syntax:
Syntax Example
if(condition) {
statement 1; // executes when condition is true
} else {
statement 2; // executes when condition is false
}
Consider the following example.
Code Example
public class Student {
public static void main(String[] args) {
int x = 10;
int y = 12;
if(x + y < 10) {
System.out.println("x + y is less than 10");
} else {
System.out.println("x + y is greater than 20");
}
}
}
Output
3) if-else-if ladder:
The if-else-if statement contains the if-statement followed by multiple else-if statements. In other words, we can say that it is the chain of if-else statements that create a decision tree where the program may enter in the block of code where the condition is true. We can also define an else statement at the end of the chain.
Syntax of if-else-if statement is given below.
Syntax Example
if(condition 1) {
statement 1; // executes when condition 1 is true
} else if(condition 2) {
statement 2; // executes when condition 2 is true
} else {
statement 3; // executes when all the conditions are false
}
Consider the following example.
Code Example
public class Student {
public static void main(String[] args) {
String city = "Delhi";
if(city == "Meerut") {
System.out.println("city is Meerut");
} else if (city == "Noida") {
System.out.println("city is Noida");
} else if(city == "Agra") {
System.out.println("city is Agra");
} else {
System.out.println(city);
}
}
}
Output
Nested if-statement
In nested if-statements, the if statement can contain an if or if-else statement inside another if or else-if statement.
Syntax of Nested if-statement is given below.
Syntax Example
if(condition 1) {
statement 1; // executes when condition 1 is true
if(condition 2) {
statement 2; // executes when condition 2 is true
} else {
statement 2; // executes when condition 2 is false
}
}
Consider the following example:
Code Example
public class Student {
public static void main(String[] args) {
String address = "Delhi, India";
if(address.endsWith("India")) {
if(address.contains("Meerut")) {
System.out.println("Your city is Meerut");
} else if(address.contains("Noida")) {
System.out.println("Your city is Noida");
} else {
System.out.println(address.split(",")[0]);
}
} else {
System.out.println("You are not living in India");
}
}
}
Output
Switch Statement
In Java, Switch statements are similar to if-else-if statements. The switch statement contains multiple blocks of code called cases, and a single case is executed based on the variable which is being switched. The switch statement is easier to use instead of if-else-if statements, enhancing readability.
- The case variables can be int, short, byte, char, or enumeration. String type is also supported since Java 7.
- Cases cannot be duplicates.
- Default statement executes if no case matches the value of expression. It is optional.
- Break statement terminates the switch block when the condition is satisfied. It is optional; if not used, the next case executes.
Syntax of Switch Statement:
Syntax Example
switch (expression) {
case value1:
statement1;
break;
...
case valueN:
statementN;
break;
default:
default statement;
}
Consider the following example:
Code Example
public class Student {
public static void main(String[] args) {
int num = 2;
switch (num) {
case 0:
System.out.println("number is 0");
break;
case 1:
System.out.println("number is 1");
break;
default:
System.out.println(num);
}
}
}
Output
Loop Statements
In programming, sometimes we need to execute a block of code repeatedly while a condition is true. Loop statements are used to execute instructions in a repeated order based on a particular condition.
Java for loop
Syntax:
Syntax Example
for(initialization; condition; increment/decrement) {
// block of statements
}
Consider the following example:
Code Example
public class Calculation {
public static void main(String[] args) {
int sum = 0;
for(int j = 1; j <= 10; j++) {
sum = sum + j;
}
System.out.println("The sum of first 10 natural numbers is " + sum);
}
}
Output
Java for-each loop
Syntax:
Code Example
for(data_type var : array_name/collection_name) {
// statements
}
Consider the following example:
Code Example
public class Calculation {
public static void main(String[] args) {
String[] names = {"Java", "C", "C++", "Python", "JavaScript"};
System.out.println("Printing the content of the array names:");
for(String name : names) {
System.out.println(name);
}
}
}
Output
Java
C
C++
Python
JavaScript
Java While Loop
The while loop is used to iterate over a number of statements multiple times. If we don't know the number of iterations in advance, it is recommended to use a while loop. Unlike the for loop, the initialization and increment/decrement don't take place inside the loop statement in the while loop.
It is also known as the entry-controlled loop since the condition is checked at the start of the loop. If the condition is true, the loop body will be executed; otherwise, the statements after the loop will be executed.
Syntax of While Loop:
Syntax Example
while (condition) {
// looping statements
}
Consider the following example:
Code Example
public class Calculation {
public static void main(String[] args) {
// TODO Auto-generated method stub
int i = 0;
System.out.println("Printing the list of first 10 even numbers n");
while (i <= 10) {
System.out.println(i);
i = i + 2;
}
}
}
Output
0
2
4
6
8
10
Java Do-While Loop
The do-while loop checks the condition at the end of the loop after executing the loop statements. When the number of iterations is not known and we have to execute the loop at least once, we can use a do-while loop.
It is also known as the exit-controlled loop since the condition is not checked in advance. The syntax of the do-while loop is given below.
Syntax of Do-While Loop:
Code Example
do {
// statements
} while (condition);
Consider the following example to understand the functioning of the do-while loop in Java:
Code Example
public class Calculation {
public static void main(String[] args) {
// TODO Auto-generated method stub
int i = 0;
System.out.println("Printing the list of first 10 even numbers n");
do {
System.out.println(i);
i = i + 2;
} while (i <= 10);
}
}
Output
0
2
4
6
8
10
Jump Statements
Jump statements are used to transfer the control of the program to specific statements. In other words, jump statements transfer the execution control to another part of the program. There are two types of jump statements in Java: break and continue.
Java Break Statement
As the name suggests, the break statement is used to break the current flow of the program and transfer control to the next statement outside a loop or switch statement. However, it breaks only the inner loop in the case of a nested loop.
The break statement cannot be used independently in a Java program; it can only be written inside a loop or switch statement.
Consider the following example in which we have used the break statement with the for loop:
Code Example
public class BreakExample {
public static void main(String[] args) {
// TODO Auto-generated method stub
for (int i = 0; i <= 10; i++) {
System.out.println(i);
if (i == 6) {
break;
}
}
}
}
Output
1
2
3
4
5
6
Here’s an example of the break statement with a labeled for loop:
Code Example
public class Calculation {
public static void main(String[] args) {
// TODO Auto-generated method stub
a:
for (int i = 0; i <= 10; i++) {
b:
for (int j = 0; j <= 15; j++) {
c:
for (int k = 0; k <= 20; k++) {
System.out.println(k);
if (k == 5) {
break a;
}
}
}
}
}
}
Output
1
2
3
4
5
Java Continue Statement
Unlike the break statement, the continue statement doesn't break the loop; instead, it skips a specific part of the loop and jumps to the next iteration of the loop immediately.
Consider the following example to understand the functioning of the continue statement in Java:
Code Example
public class ContinueExample {
public static void main(String[] args) {
// TODO Auto-generated method stub
for (int i = 0; i <= 2; i++) {
for (int j = i; j <= 5; j++) {
if (j == 4) {
continue;
}
System.out.println(j);
}
}
}
}
Output
1
2
3
5
1
2
3
5
2
3
5