Lesson 8 – Loops (while, for, do–while)

Loops allow you to repeat actions. They are one of the most important tools in programming.

Java has 3 main types of loops:


1. The while Loop

A while loop runs as long as its condition is true.

Example 1 – Count from 1 to 5

public class Main {
    public static void main(String[] args) {
        int i = 1;

        while (i <= 5) {
            System.out.println(i);
            i++;  // increase i each time
        }
    }
}
        

Example 2 – Print even numbers from 2 to 10

public class Main {
    public static void main(String[] args) {
        int number = 2;

        while (number <= 10) {
            System.out.println(number);
            number += 2;
        }
    }
}
        

Example 3 – Countdown

public class Main {
    public static void main(String[] args) {
        int countdown = 5;

        while (countdown > 0) {
            System.out.println("Countdown: " + countdown);
            countdown--;
        }

        System.out.println("Blast off!");
    }
}
        

2. The for Loop

Use a for loop when you know how many times to repeat.

Example 4 – Count from 1 to 10

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

Example 5 – Print numbers backwards

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

Example 6 – Print squares of numbers

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

3. Looping Through an Array

Example 7 – Print all elements of an array

public class Main {
    public static void main(String[] args) {
        String[] fruits = {"Apple", "Banana", "Mango", "Orange"};

        for (int i = 0; i < fruits.length; i++) {
            System.out.println(fruits[i]);
        }
    }
}
        

Example 8 – Enhanced for loop (for-each)

public class Main {
    public static void main(String[] args) {
        int[] numbers = {10, 20, 30, 40};

        for (int n : numbers) {
            System.out.println(n);
        }
    }
}
        

4. The do–while Loop

A do–while loop runs at least once even if the condition is false.

Example 9 – Basic do-while

public class Main {
    public static void main(String[] args) {
        int x = 1;

        do {
            System.out.println("Number: " + x);
            x++;
        } while (x <= 5);
    }
}
        

Example 10 – Menu that repeats

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int option;

        do {
            System.out.println("1. Say Hello");
            System.out.println("2. Say Bye");
            System.out.println("3. Exit");
            System.out.print("Choose: ");
            option = input.nextInt();

            if (option == 1) {
                System.out.println("Hello!");
            } else if (option == 2) {
                System.out.println("Bye!");
            }

        } while (option != 3);

        System.out.println("Program ended.");
    }
}
        

5. Break and Continue

break stops the loop immediately.

continue skips one loop step but continues.

Example 11 – Using break

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

Example 12 – Using continue

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

6. Real-Life Example – Sum of 10 Numbers

Example 13 – Full Program

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int sum = 0;

        for (int i = 1; i <= 10; i++) {
            System.out.print("Enter number " + i + ": ");
            int n = input.nextInt();
            sum += n;
        }

        System.out.println("Total sum = " + sum);
    }
}
        

Summary

Back to Top