Javatpoint Logo
Javatpoint Logo

Control Flow in Java

Java compiler executes the java code from top to bottom. The statements 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.

Java provides three types of control flow statements.

  1. Decision Making statements
  2. Loop statements
  3. Jump statements

Decision-Making statements:

Decision-making statements evaluate the Boolean expression and control the program flow depending upon the condition result. There are two types of decision-making statements in java, I.e., If statement and switch statement.

If Statement:

In Java, the "if" statement is used to evaluate a condition. The control of the program is diverted depending upon the condition result that is a Boolean value, either true or false. In java, there are four types of if-statements given below.

  1. if statement
  2. if-else statement
  3. else-if statement
  4. Nested if-statement

Let's understand the if-statements one by one.

1. if statement:

This 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.

Output:

x + y is greater than 20

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.

Consider the following example.

Output:

x + y is greater than 20

3. lse-if statement

The 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 any block of code. We can also define an else statement at the end of the chain.

Consider the following example.

Output:

Delhi

4. Nested if-statement

In nested if-statements, the if statement contains multiple if-else statements as a separate block of code. Consider the following example.

Output:

Delhi

Switch Statement:

In Java, Switch statements are similar to if-else-if statements. The switch statement enables us to check the variable for the range of values defined for multiple case statements. The switch statement is easier to use instead of if-else-if statements. It also enhances the readability of the program. The syntax to use the switch statement is given below.

Consider the following example to understand the flow of the switch statement.

Output:

2

While using switch statements, we must notice that the case expression will be of the same type as the variable. However, it will also be a constant value. The switch permits only int, string, and Enum type variables to be used.

Loop Statements

In programming, sometimes we need to execute the block of code repeatedly while some condition evaluates to true. However, loop statements are used to execute the set of instructions in a repeated order. The execution of the set of instructions depends upon a particular condition.

In Java, we have three types of loops that execute similarly. However, there are differences in their syntax and condition checking time.

  1. for loop
  2. while loop
  3. do-while loop

Let's understand the loop statements one by one.

Java for loop

In java, for loop is similar to C and C ++. It enables us to initialize the loop variable, check the condition, and increment/decrement in a single line of code. The syntax to use the for loop is given below.

The flow chart for the for-loop is given below.

Control Flow in Java

Consider the following example to understand the proper functioning of the for loop in java.

Output:

The sum of first 10 natural numbers is 55

Java for-each loop

Java provides an enhanced for loop to traverse the data structures like array or collection. In the for-each loop, we don't need to update the loop variable. The syntax to use the for-each loop in java is given below.

Consider the following example to understand the functioning of the for-each loop in java.

Output:

Printing the content of the array names:

Java
C
C++
Python
JavaScript

Java while loop

The while loop is also used to iterate over the number of statements multiple times. However, if we don't know the number of iterations in advance, it is recommended to use a while loop. Unlike for loop, the initialization and increment/decrement doesn't take place inside the loop statement in 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, then the loop body will be executed; otherwise, the statements after the loop will be executed.

The syntax of the while loop is given below.

The flow chart for the while loop is given in the following image.

Control Flow in Java

Consider the following example.

Output:

Printing the list of first 10 even numbers 

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. However, it is recommended to use the do-while loop if we don't know the condition in advance, and we need the loop to execute at least once.

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.

The flow chart of the do-while loop is given in the following image.

Control Flow in Java

Consider the following example to understand the functioning of the do-while loop in java.

Output:

Printing the list of first 10 even numbers 
0
2
4
6
8
10

Jump Statements

Jump statements are used to transfer the control of the program to the specific statements. In other words, jump statements transfer the execution control to the other part of the program. There are two types of jump statements in java, i.e., 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 the control to the next statement outside the current flow. It is used to break the loop and switch statement. However, it breaks only the inner loop in the case of the nested loop.

The break statement cannot be used independently in the java program, i.e., it can only be written inside the loop or switch statement.

The break statement example with loop

Consider the following example in which we have used the break statement with the for loop.

Output:

0
1
2
3
4
5
6

break statement example with labeled for loop

Output:

0
1
2
3
4
5

Java continue statement

Unlike break statement, the continue statement doesn't break the loop, whereas, it skips the 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.

Output:

0
1
2
3
5
1
2
3
5
2
3
5





Youtube For Videos Join Our Youtube Channel: Join Now

Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA