Control Flow Statements in Java

Control Flow Statements in Java

In this article, I am going to discuss the Control Flow Statements in Java. Please read our previous article, where we discussed Identifiers and Reserved Words in Java with examples. At the end of this article, you will understand what are Control Flow Statements and its need as well as you will also understand how to use Control Flow Statements in Java application.

What are Control Flow Statements in Java?

In this part of the Java tutorial, we will talk about program flow control. We will use several keywords that enable us to control the flow of a Java program.

Generally, the statements inside your java code are executed from top to bottom, in the order that they appear. Control flow statements, change, or break the flow of execution by implementing decision making, looping, and branching your program to execute particular blocks of code based on the conditions. Statements can be executed multiple times or only under a specific condition. The if, else, and switch statements are used for testing conditions, the while and for statements to create cycles, and the break and continue statements to alter a loop.

Types of Control Flow Statements in Java:

There are 3 types of control flow statements supported by the Java programming language:

  1. Decision-making statements: if-then, if-then-else, switch
  2. Looping statements: for, while and do-while
  3. Branching statements: break, continue, return
Decision-making Statements in Java:

Decision Making in programming is similar to decision making in real life. In programming also we face some situations where we want a certain block of code to be executed when some condition is fulfilled. Java decision-making statements allow you to make a decision, based upon the result of a condition. It happens when jumping of statements or repetition of certain calculations is not necessary.

Types of decision-making statements:

In Java, the decision-making statements are as follows:

  1. if Statement
  2. if-else statement
  3. if-else if .. statement
  4. Case statement
if statement in Java:

It is used to decide whether a certain statement or block of statements will be executed or not i.e if a certain condition is true then a block of statement is executed otherwise not.

Syntax:
if (expression)
{
     statement;
}

Flow Chart of if Statement:

If Statement Control Flow Chart in Java

Sample program for if statement:
public class IfStatementDemo 
{
    public static void main(String[] args) 
    {
        boolean condition = true;
         
        if(condition) {
            System.out.println("Condition is true");
        } 
    }
}

Output: Condition is true

if-else statement in Java:

We can use the else keyword to create a simple branch. If the expression inside the square brackets following the if keyword evaluates to false, the statement following the else keyword is automatically executed.

Syntax:
if (expression)
{
     statement-1;
}
else{
     statement-2;
}

Flow Chart of the if-else statement:

If Else Statement Control Flow Chart in Java

Sample Program for if-else statement :
public class IfElseStatementDemo 
{
    public static void main(String[] args) 
    {
        boolean condition = false;
         
        if(condition) {
            System.out.println("Condition is true");
        } 
        else
        {
            System.out.println("Condition is false");
        }
    }
}

Output: Condition is false

if-else-if statement in Java:

We can create multiple branches using the else if keyword. The else if keyword tests for another condition if and only if the previous condition was not met. Note that we can use multiple else if keywords in our tests.

Syntax:
if (expression)
{
      statement-1;
}
else if{
     statement-2;
}
else {
     statement-3;
}

Flow Chart of the if-else-if statement:

If Else If Control Statement Flow Chart in Java

Sample Program for if-else-if statement:
public class IfElseIfStatementDemo 
{
    public static void main(String[] args) 
    {
        int i = 10;
         
        if(i > 10) 
        {
            System.out.println("Condition is greater than 10");
        } 
        else if(i < 10) 
        {
            System.out.println("Condition is less than 10");
        } 
        else
        {
            System.out.println("Condition is equal to 10");
        }
    }
}

Output: Condition is equal to 10

The switch statement in Java:

A switch statement gives you the option to test for a range of values for your variables. If you think your if-else-if statements are long and complex, you can use a switch statement instead.

Syntax:
switch(expression)
{
case value1:
Statement-1;
break;

case value2:
Statement-2;
break;

.
.
.

Case valueN:
Statement-N;
break;
}

Flow chart of a switch statement:

Switch Statement Flow Chart in Java

Sample Program for Switch Statement :
class SwitchCaseDemo 
{ 
    public static void main(String args[]) 
    { 
        int age = 2; 
        switch (age) 
        { 
        case 1: 
            System.out.println("You are one year old."); 
            break; 
        case 2: 
            System.out.println("You are two years old."); 
            break; 
        default: 
            System.out.println("You are more than two years old."); 
        } 
    } 
}

Output: You are two years old.

In the next article, I am going to discuss Looping Statements in Java with examples. Here, in this article, I try to explain Control Flow Statements in Java with examples and I hope you like this Control Flow Statements in Java article. I would like to have your feedback. Please post your feedback, question, or comments about this Control Flow Statements in Java article.

Leave a Reply

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