Last updated : July 27, 2026

Control Statements Tutorials

Programming

Control  Statements

Control statement is used to control the flow of execution depending on some specific condition.

Types of Control Statement

  1. Conditional Control Statement

  2. Un-Conditional Control Statement

  3. Looping Control Statement

1. Conditional Control Statement : 

It is used to make decisions in code based on certain conditions.

Types of Conditional Control Statements

  1. if statement :

  2. if-else statement :

  3. if-else-if ladder :

  4. switch statement :

1. if Statement

Used when you want to execute code only if a condition is true

java

if (condition) {
    // code executes if condition is true
}

java

int age = 20;

if (age >= 18) {
    System.out.println("You are eligible to vote");
}

// output : You are eligible to vote

  • Condition must return boolean (true/false)

  • Executes only when condition = true

  • No action if condition is false


2. if-else Statement

Used when you want one block if true, another if false

java

if (condition) {
    // true block
} else {
    // false block
}

java

int number = 10;

if (number % 2 == 0) {
    System.out.println("Even number");
} else {
    System.out.println("Odd number");
}

// output :  Even number

  • Only one block executes

  • else is optional but useful

  • Improves decision making


3. if-else-if Ladder

Used when you have multiple conditions

java

if (condition1) {
    // block 1
} else if (condition2) {
    // block 2
} else if (condition3) {
    // block 3
} else {
    // default block
}

java

int marks = 75;

if (marks >= 90) {
    System.out.println("Grade A");
} else if (marks >= 70) {
    System.out.println("Grade B");
} else if (marks >= 50) {
    System.out.println("Grade C");
} else {
    System.out.println("Fail");
}

// output :  Grade B

  • Conditions checked top to bottom

  • First true condition executes

  • Remaining conditions are skipped

  • else is optional (default case)


4. SWITCH Statement

Used when comparing one variable with multiple fixed values

javascript

switch (variable) {
    case value1:
        // code
        break;

    case value2:
        // code
        break;

    default:
        // default code
}

javascript

int day = 3;

switch (day) {
    case 1:
        System.out.println("Monday");
        break;
    case 2:
        System.out.println("Tuesday");
        break;
    case 3:
        System.out.println("Wednesday");
        break;
    default:
        System.out.println("Invalid day");
}

// output  : Wednesday

Java 17 Modern Switch

Since you're using Java 17

plaintext

int day = 2;

switch (day) {
    case 1 -> System.out.println("Monday");
    case 2 -> System.out.println("Tuesday");
    case 3 -> System.out.println("Wednesday");
    default -> System.out.println("Invalid");
}

  • No need for break

  • Cleaner & modern

  • Used in real projects

2. Un-Conditional Control Statement

Unconditional Control Statements are used to change the normal flow of execution in a program without checking any condition. These are not dependent on any boolean expressions — they just perform an action directly.

Types of Unconditional Control Statements:

  1. break

  2. continue

  3. return

3. Looping Control Statement

Looping control statements are used to repeat a block of code multiple times until a certain condition is met.

Types of Loops in Java

for loop

  • When you already know the number of iterations

  • Example: printing numbers from 1 to 10

java

for(initialization; condition; update) {
    // code
}

Example:

java

public class Main {
    public static void main(String[] args) {
        
        for(int i = 1; i <= 5; i++) {
            System.out.println(i);
        }
        
    }
}

🔍 Explanation:

  • int i = 1 → starting point

  • i <= 5 → condition check

  • i++ → increment (increase value)

while loop

  • When the loop is condition-based

  • When you don’t know the exact number of iterations

java

while(condition) {
    // code
}

java

public class Main {
    public static void main(String[] args) {
        
        int i = 1;
        
        while(i <= 5) {
            System.out.println(i);
            i++;
        }
        
    }
}

🔍 Explanation:

  • First, the condition is checked

  • If the condition is false, the loop will not run at all

do-while loop

  • When you want the loop to run at least once, no matter what

java

do {
    // code
} while(condition);

java

public class Main {
    public static void main(String[] args) {
        
        int i = 1;
        
        do {
            System.out.println(i);
            i++;
        } while(i <= 5);
        
    }
}

 Basic Loop Questions

  1. Print numbers from 1 to 10 using a for loop.

  2. Print even numbers between 1 to 50.

  3. Print odd numbers between 1 to 50.

  4. Print the sum of numbers from 1 to N.

  5. Find the sum of even and odd numbers separately from 1 to N.

 Pattern  Questions


*

**

***

****

==========================

*****

****

***

**

*

===========================

    *

   **

  ***

 ****

*****

===============================

    *

   ***

  *****

 *******

*********
================================

    *

   ***

  *****

 *******

*********

 *******

  *****

   ***

    *

Job PortalJobs