Java Control Statements – Jump, Looping, Iteration | Decision Making Statements in Java

A programming language uses control statements to control the flow of execution of a program. In Java programming, we can control the flow of execution of a program based on some conditions. In this tutorial, we will learn about the Classification of Control Statements in Java, their Syntax, Flowcharts, Example Programs, etc.

Control Statements in Java

Java control statements can be put into the following three categories: selection, iteration, and jump.

Selection Statements: The Selection statements allow your program to choose a different path of execution based on a certain condition.

Iteration Statements: The Iteration statements allow program execution to repeat one or more statements.

Jump Statements: Java Jump Statements transfer the control to other parts of the program.

Java Control Statements

Do Check:

Java Selection Statements | Decision Making Statements in Java

The Java selection statements allow your program to choose a different path of execution based on a certain condition. Java selection statements provide different type of statements such as:

  • if statement
  • if-else statement
  • nested if statement
  • if-else-if ladder statement
  • Switch case

Simple if Statement

It is the most basic one among all other java control flow statements. It determines whether a code should be executed or not depending on the condition.

Syntax:

if (condition) { 
Statement 1; //executed if condition is true
}
Statement 2; //executed irrespective of the condition

Flowchart:

If Statement Flowchart

if Condition Example Program

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:

x + y is greater than 20

if else Statement

In this statement, if the condition is met if block is executed otherwise else block is executed.

Syntax:

if (condition)
{
// Executes this block if
// condition is true
}
else
{
// Executes this block if
// condition is false
}

Flowchart:

If Else Statement Flowchart

if else Statement Example Program

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:

x + y is greater than 20

Nested if Statement

An if statement present inside an if block is known as nested if block. It is similar to if.. else statement just that they are defined in another if.. else statement.

Syntax:

if (condition1) {
Statement 1; //executed if first condition is true
if (condition2) {
Statement 2; //executed if second condition is true
}
else {
Statement 3; //executed if second condition is false
}
}

Flowchart:

Nested If Statement Flowchart

Nested if 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:

Delhi

Switch Statement

Switch Statement is used to execute a single statement from multiple conditions. We can use this with short, long, enum, int, byte, etc. Key points to remember while using the Switch Statement is provided below.

      • We can specify one or N number of Case Values for a Switch Expression.
      • Case Values that are duplicates are not permissible and the compiler returns a compile-time error if unique values aren’t used.
      • Case value needs to be either literal or constant and variables aren’t allowed.
      • You can use a break statement to terminate the statement sequence and it is optional. If you don’t specific it next case would be executed.

Syntax:

switch (expression)
{
case value1:
statement1;
break;
case value2:
statement2;
break;
.
.
case valueN:
statementN;
break;
default:
statementDefault;
}

Flowchart:

Switch Case Flowchart

Switch Case Example Program

public class Student implements Cloneable {  
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:

2

if-else-if Ladder

In this case, the user can decide among multiple options. If Statements are Executed from top down. If any one of the conditions controlling the if is true then the statement associated with it will be executed and the rest of the ladder is bypassed. If no condition is true then the final else statement is executed.

Syntax:

if (condition)
statement;
else if (condition)
statement;
.
.
else
statement;

Flowchart:

if else if Ladder Flowchart

if-else-if Ladder Example

// Java program to illustrate if-else-if ladder
class ifelseifDemo
{
public static void main(String args[])
{
int i = 20;

if (i == 10)
System.out.println("i is 10");
else if (i == 15)
System.out.println("i is 15");
else if (i == 20)
System.out.println("i is 20");
else
System.out.println("i is not present");
}
}

Output:

i is 20

Iteration Statements in Java | Looping Statements in Java

Java Iteration statements allow program execution to repeat one or more statements. Java Iteration statements provide different type of statements such as:

      • While loop
      • Do While loop
      • For loop

While Loop

It is known as the most common loop used in programming languages. While Loop evaluates a particular condition. If the condition is true then the code is executed or else the process is continued till the specific condition is false.

The Condition you specify in the while loop needs to be a Boolean Expression. You will get an error if you use int or string.

Syntax:

while (condition)
{
statementOne;
}

Flowchart:

While Loop Flowchart

While Loop Example Program

public class whileTest
{
public static void main(String args[])
{
int i = 5;
while (i <= 15)
{
System.out.println(i);
i = i+2;
}
}
}

Output:

5
7
9
11
13
15

Do While Loop

Do While Loop is the same as While Loop. The only difference is that the condition in the do-while loop is executed after the execution of the body. That is the loop is executed at least once.

Syntax:

do{
//code to be executed
}while(condition);

Flowchart:

Do While Flowchart

Do While Loop Example Program

public class Main 
{ 
public static void main(String args[]) 
{ 
int i = 20; 
do 
{ 
System.out.println(i); 
i = i+1; 
} while (i <= 20); 
} 
}

Output:

20

For Loop

We use For Loop in Java to iterate and evaluate a code multiple times. If you are aware of the number of iterations you can use a for loop.

Syntax:

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

Flowchart:

For Loop Flowchart

For Loop Java Control Statements Example

public class forLoop
{
public static void main(String args[])
{
for (int i = 1; i <= 10; i++)
System.out.println(i);
}
}

Output:

5
6
7
8
9
10

Jump Statements in Java | Branching Statements

Branching Statements are used to jump from one statement to another statement. Java Jump statements transfer the control to other parts of the program. java Jump statements provide different type of statements such as:

      • break statement
      • continue statement
      • return statement

Break

In Java Break Statement is used to terminate a sequence in a switch statement or to exit a loop. It can also be used as a civilized form of goto.

We can use the break for immediate termination of a loop, bypassing the conditional expression and any code leftover in the body of the loop. If you use to break in a set of nested loops will break out of the innermost loop.

Syntax:

break; or break <label>;

Flowchart:

Break Flowchart

Break Statement Usage Example Program

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:

0
1
2
3
4
5
6

Continue

In order to jump to the next iteration of the loop we use the continue statement. This Statement continues the current flow of the program and skips a part of the code at the given condition.

Syntax:

continue; or continue<label>;

Flowchart:

Continue Flowchart

Continue Statement Usage Example Program

// Java program to illustrate using
// continue in an if statement
class ContinueDemo
{
    public static void main(String args[])
    {
        for (int i = 0; i < 10; i++)
        {
            // If the number is even
            // skip and continue
            if (i%2 == 0)
                continue;
  
            // If number is odd, print it
            System.out.print(i + " ");
        }
    }
}

Output:

1 3 5 7 9

Return

We use a return statement to explicitly return from a method. It causes a program control to transfer back to the caller of the method.

Return Statement Example Program

// Java program to illustrate using return
class Return
{
    public static void main(String args[])
    {
        boolean t = true;
        System.out.println("Before the return.");
      
        if (t)
            return;
  
        // Compiler will bypass every statement 
        // after return
        System.out.println("This won't execute.");
    }
}

Output:

Before the return.