Java Control Flow Statements

To plan or decide something before implementation is always essential. This will help execute every step more efficiently, and you have an idea about the action that to be performed next.

In computer language, decision-making is an important part. Predominantly, the execution starts from the top to the bottom. But sometimes, the decision is vital to know which block needs to perform next. Control flow in Java will help you to decide the next action. This article gives detailed information about control flow in Java, which helps execute code sequentially.

Java Control Flow

Java Control Flow Statements

Image Source

Java control flow controls the flow of execution of code in the program. In Java, by placing the decision making, branching, looping, and adding conditional blocks, you can control the code’s flow.

Types of control flow statement classified as:

  1. Decision-Making Statements
  2. Looping Statements
  3. Branching Statements

Decision-Making Statements

Decision Making Statements use when you want to decide which block of code will get executed.

if Statement

The if condition executes the code only if the condition is true; otherwise, the code will not get executed.

Syntax: 

Example:

Output:

The num1 is less than the num2

If condition is true, thats why the code executes, and we get the output.

if-else Statement

If-else statement similar to the if statement, it only had one extra code block that is else. When if statement condition is true, if block executes otherwise, the else block gets executed.

Example:

Output:

The num1 is equal to or greater than the num2

We get this output because if condition is false, the else block gets executed, and we get our output.

switch Statement

Till now, you have seen the if-else execution path. Now let’s learn the switch statement.

Output:

The name of the student is Anjali!

We get this output because the third statement gets executed. The break statement terminates the rest of the condition apart from the switch condition.

Looping Statements

In the looping statement, first, we make the decision and then execute the code multiple times.

for Loop

First, you need to check the condition. If the value is true, the piece of code written under the for statement gets executed. After the code block’s execution, the control moves back to the condition if the condition satisfies the block of code executes till the condition reaches out to be false.

Syntax:

Example:

The value of i initializes to zero, and the condition imparts that i need to be executed two times. And after the execution of each condition, the value of i gets incremented till the condition becomes false.

Output:

0. My name is Anjali!
1. My name is Anjali!
2. My name is Anjali!

while Loop

Condition and increment or decrement statements are similar to for loop, but the syntax is quite different.

Syntax:

Example:

Three times execution takes place, i.e., 0,1 and 2. The increment or decrement operator is written after the code. First, the condition will get the check after the code starts its execution, and then the value gets increment. The incremented or decremented value checks the condition; if the condition is true, the code’s execution begins.

Output:

0. My name is Anjali!
1. My name is Anjali!
2. My name is Anjali!

do-while Loop

In the do-while loop, it firsts execute the code then checks the condition.

Syntax:

Example:

Output:

0. My name is Anjali!
1. My name is Anjali!
2. My name is Anjali!

Branching Statements

Three types of branching statements are:

  1. break statement
  2. continue statement
  3. return statement

break Statement

We use a break statement to terminate the flow of execution. We mainly use break statements in switch-case conditions or loop conditions.

Example:

Terminates the execution after i=4;

Output:

0. My name is Anjali!
1. My name is Anjali!
2. My name is Anjali!
3. My name is Anjali!

continue Statement

The use of continuous operation is to skip the ongoing iteration and sends the control back to the loop’s condition.

Example:

This program is the find the count of occurrence of “a” in the given string.

In for loop, we use the continue statement to find the letter “a,” if the current iteration is not “a,” then, continue statement will skip the operation and send the control back to the loop.

Output:

We found ‘a’ in the inputString ‘Anjali Singh’ three times.

return Statement

The return statement is found at the bottom of the program. It simply exits from method code. The control flows go to the line from where the method is invoked.

Example:

Output:

9

Leave a Reply

Your email address will not be published. Required fields are marked *