Control Flow Statements in Java

Video Tutorial
FREE
If Else thumbnail
This video belongs to
Java Course - Mastering the Fundamentals
12 modules
Certificate
Topics Covered

Overview

Control Flow statements in programming control the order in which the computer executes statements in a file. They allow us to regulate the flow of our program's execution.

Java provides a varied number of control statements that help to manage the flow of the program. The focus of the study of this article will be decision-making statements in Java. Let us jump right in and know more about them.

Introduction to Java Control Flow Statements

Consider a scenario where there is a potential that covid, which is spreading throughout India, could spread to your campus. You must decide whether to return home after a short time away from the college or continue attending it.

In this instance, the choice of which path to choose depends on our decision. In our daily lives, we run into a number of these issues and must decide how to proceed. Control flow statements in java allow us to tackle such situations in programming.

Every programming language carries out the codes in the script's order, from the first line in the file to the last. Control flow statements give us the ability to manage the program's flow. Without them, the interpreter would simply run the code in the order it is written in the file, giving us no control over the sequence or flow.

Consider attempting to compare two integers using a computer program. What would be the best way to accomplish that? Let's find out.

Types of Control Flow Statements in Java

There are three types of Control Flow Statements in Java :

  1. Decision-Making Statement

  2. Loop Statements

  3. Jump Statements

1. Decision-Making Statement:

Assist in making decisions. It decides which statement to execute and when. Java provides the facility to make decisions using selection statements or selection constructs. They evaluate the decision based on provided conditions and proceed with the flow of the program in a certain direction.

Java provides two types of decision-making statements :

  • if Statement
  • switch Statement

Selection is a decision-making process. The outcome of a decision determines which process the system takes.

For example, a program might tell a user that he or she is old enough to drive a car. If the user's age meets the required driving requirement, the system will follow one path and perform one action; otherwise, it will follow another route and perform another action. Below is a flow chart of choice building in Java :

decision-making statements in java

The above flow-chart clearly explains the process of decision-making statements. It first evaluates the test condition and is based on the result. It either follows one course of action or the other.

Let’s understand more about each decision statement.

If Statement

"If" Statement is the simplest decision-making statement. It is used to decide whether a block of code should be executed or not based on the test condition. If the condition is evaluated to true, then that block is executed; otherwise, it is skipped.

Syntax of If Statement :

Here condition will either be evaluated as true or false. If the value is true, then a block of code will be executed. Otherwise, it will ignore it and skip to the statement just below the if command.

Below is the flow diagram of the If-statement:

flow diagram of If-statement

The test expression is first evaluated as indicated in the above diagram, and if the evaluation yields a true result, the body of the If statement is then executed; otherwise, the 'if' block is skipped and execution takes place below it.

Let's understand with an example:

Output:

The program in the example above chooses its course based on the situation. If the number of covid cases is less than 45, the condition is false, and the statement within the if command skips to the next sentence; however, if the number of covid cases is larger than 45, the statement within the if command is carried out.

Note: If we don't put curly braces { } after the if condition then by default if statement will consider the immediate one statement to be inside the block code.

If-Else Statement

"If-Else" is an extension of the If-statement. if statement only tells what to do if the condition evaluates to true. It does not tell anything when the condition is false. If-Else provides the provision of an else block, which is executed when the condition is false. It has a provision to accommodate one more block of code.

For Example: If it is raining outside, then Take an umbrella; else No need to take an umbrella!

Syntax of if-else :

Here, the condition is first evaluated as true or false. If it evaluates to true, then the program executes the block of code inside the if statement otherwise, if it evaluates to false, then the program executes the block inside the else statement.

Flow Diagram of If-Else Statement:

Flow Diagram of If-Else Statement

The test expression is first evaluated, and if it returns true, it moves into the body of the if statement; if it returns false, it moves into the body of the else statement.

Let's get a clear understanding through an example :

Output:

In the above program, the condition checks if the weather is rainy or not. Since it yields a false value, the program skips the if command and goes inside the else block, and executes the statement "No need of an umbrella". That's how we can easily make choices using Decision Making Statements.

If-Else-If Ladder

"If-Else-If" ladder consists of an if statement followed by multiple else-if statements. It is used to evaluate a condition using multiple statements. The chain of if statements are executed from the top-down.

When a first if condition evaluates to false, a second else if condition is checked, and so on, until the final else if condition is checked. An else block or an else if block are both acceptable ways to end it.

Syntax of If-Else-If Ladder :

The program checks each if condition, and as soon as one of the if condition yields true, it executes the statement inside that if block and skip the rest of the ladder. If none of the conditions evaluates to be true, then the program executes the statement of the final else block.

For Example:

Output:

In the above example, the program checks for the first if condition. Upon getting a false value, it jumps to the next else if block. Since the value of age is between 60 and 50, the condition evaluates to true, and the program executes the statement inside the second else if block and skips the below statements.

Below is the flow diagram of If-else-if ladder: flow-diagram of If-else-if ladder

In the above flow diagram, the program first checks the condition, and if it evaluates to true, then it executes the code inside the first if condition and skips the rest of the conditions. Otherwise, it jumps to check the next condition, and if it also evaluates to false, then it checks the next condition. It continues to do so until it has checked all the conditions.

Nested-If

As the name suggests, "nested-if" contains an if statement inside another if statement. Java allows us to nest if equations with another if or an else if statement.

Syntax of nested-if:

In nested if, one if statement is the aim of another if or else. The program reaches the nested if, if and only if the first if condition is evaluated to true; otherwise, it skips to the next part.

Here's an example of the nested-if statement:

Output:

In the above example, the first condition yields a true value as the age is greater than 18, so the program evaluates the next condition and executes the statement inside the nested if block.

Flow-Diagram of Nested-if: Flow-Diagram of Nested-if

In Nested-If, if the test Condition evaluates to false, then it directly jumps to the body of the else block and skips the nested condition, otherwise, if it evaluates to true, then it checks the nested test condition, and according to the result, it enters the body of nested if or nested else block. After evaluating the nested condition, it comes out and jumps to the code just below the if statement.

Conclusion

  • Control flow statements regulate the flow of the program.

  • Java provides the facility to make decisions using selection statements or selection constructs.

  • If a statement is used to decide whether a block of code should be executed or not based on the test condition.

  • If-Else is an extension of the If-statement. If-Else provides the provision of an else block, which is executed when the condition is false.

  • If-Else-If ladder consists of an if statement followed by multiple else if statements. It is used to evaluate a condition using multiple statements.

  • In Nested-If, one if statement is the aim of another if or else. The program reaches the nested if, if and only if the first if condition is evaluated to true; otherwise, it skips to the next part.