DEV Community

Divya bharathi G
Divya bharathi G

Posted on

Control Flow Statements in Java

Control Flow Statements:

  • In Java, control flow statements decide the order in which statements are executed in a program.
  • They help your program make decisions, repeat tasks, and jump to different parts of code.

Types of control flow statements in java:

  1. Decision making statement.
  2. Looping statement.
  3. Jump statement.

1.Decision making statement.

  • Decision making statement take decision based on the value.

if statement:

  • if is a java keyword.
  • Execute code only if condition is true.

Syntax:

if (condition)
{
    // statements
}
Enter fullscreen mode Exit fullscreen mode

Else statement:

  • else is a java keyword.
  • Execute code only if condition is false.

Syntax:

if (condition)
{
    // statements if true
} 
else 
{
    // statements if false
}
Enter fullscreen mode Exit fullscreen mode

Else-if statement:

  • else if is a java keyword.
  • If we want to check multiple condition,we go for else if statement.

Syntax:

if (condition1)
{
    // statements if condition1 is true.
} 
else if (condition2)
{
    // statements if condition2 is true
} 
else
{
    // Statements if none of the condition is true.
}
Enter fullscreen mode Exit fullscreen mode

Example:

package project1;
public class elseif
{

    public static void main(String[] args) 
    {
        int mark = 60;

        if(mark >= 75)
        {
            System.out.println("Distinction");
        }
        else if(mark >= 60)
        {
            System.out.println("First class");
        }
        else if(mark >= 50)
        {
            System.out.println("Second class");
        }
        else
        {
            System.out.println("Fail");
        }

    }

}
Enter fullscreen mode Exit fullscreen mode

Switch statement:

  • Used when there are multiple fixed choices.
switch (expression)
{
    case value1:
        // statements
        break;
    case value2:
        // statements
        break;
    default:
        // statements
}
Enter fullscreen mode Exit fullscreen mode

Example:

p

ackage project1;

public class Switch {

    public static void main(String[] args)
    {
        int day = 2;

        switch(day)
        {
        case 1: //1==2
            System.out.println("Monday working day");
            break;

        case 2:
            System.out.println("Tuesday working day");
            break;

        case 3:
            System.out.println("Wednesday working day");
            break;

        case 4:
            System.out.println("Thursday working day");
            break;

        case 5:
            System.out.println("Friday working day");
            break;

        default:
            System.out.println("Holiday");
        }

    }

Enter fullscreen mode Exit fullscreen mode

2.Looping statement:

  • Looping statements are used to execute a block of code repeatedly as long as a condition is satisfied.

While loop:

  • The while loop executes a block of code as long as the condition is true.

Syntax:

while (condition)
{
    // statements
}
Enter fullscreen mode Exit fullscreen mode

Example:

public static void main(String[] args)
 {
        int i =1;
        while(i <=10)
        {
            System.out.print(i);
            i++;
        }
}
Enter fullscreen mode Exit fullscreen mode

Explanation

  • The variable i is initialized before the loop starts.
  • The condition i <= 10 is checked before each iteration.
  • The value of i is incremented inside the loop body, ensuring the loop eventually terminates.

For loop:

  • It combines initialization, condition checking, and increment/decrement in a single line.

Syntax:

for (initialization; condition; increment/decrement) 
{
    // statements
}
Enter fullscreen mode Exit fullscreen mode

Example:

for(int i =1;i <=5;i++)
{
System.out.println(i); 
}

Enter fullscreen mode Exit fullscreen mode

Explanation

  • Initialization runs once (int i = 1)
  • Condition is checked before every iteration (i <= 5)
  • Increment happens after each iteration (i++)

Top comments (0)