Skip to main content
Java Basics
CHAPTER 08 Beginner

Loops in Java

Updated: May 17, 2026
5 min read

# CHAPTER 8

Loops in Java

1. Introduction

Loops let you execute a block of code repeatedly. Instead of writing System.out.println() 100 times, you write one loop. Loops are the workhorse of programming — processing lists, iterating through databases, and running game engines.

2. Learning Objectives

  • Use for, while, and do-while loops.
  • Control flow with break and continue.
  • Write nested loops.
  • Build a multiplication table generator.

3. The for Loop

Best when you know exactly how many times to iterate.
java
1234
for (int i = 1; i <= 5; i++) {
    System.out.println("Count: " + i);
}
// Output: Count: 1, Count: 2, Count: 3, Count: 4, Count: 5

Structure: for (initialization; condition; update) { body }

4. The while Loop

Best when you don't know how many iterations in advance.
java
12345
int count = 1;
while (count <= 5) {
    System.out.println("Count: " + count);
    count++;
}

5. The do-while Loop

Guarantees the body executes at least once.
java
123456
int num = 10;
do {
    System.out.println("Number: " + num);
    num++;
} while (num <= 5);
// Prints "Number: 10" even though 10 > 5

6. Enhanced for-each Loop

Simplified syntax for iterating over arrays and collections.
java
1234
int[] scores = {90, 85, 78, 92, 88};
for (int score : scores) {
    System.out.println("Score: " + score);
}

7. break and continue

  • break: Immediately exits the entire loop.
  • continue: Skips the current iteration and jumps to the next one.
java
1234567891011
for (int i = 1; i <= 10; i++) {
    if (i == 5) break;       // Stop at 5
    System.out.print(i + " ");
}
// Output: 1 2 3 4

for (int i = 1; i <= 10; i++) {
    if (i % 2 == 0) continue; // Skip even numbers
    System.out.print(i + " ");
}
// Output: 1 3 5 7 9

8. Nested Loops

java
123456
for (int i = 1; i <= 3; i++) {
    for (int j = 1; j <= 3; j++) {
        System.out.print("(" + i + "," + j + ") ");
    }
    System.out.println();
}

9. Mini Project: Multiplication Table Generator

java
123456789101112131415
import java.util.Scanner;

public class MultiplicationTable {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter a number: ");
        int num = sc.nextInt();

        System.out.println("\n=== Multiplication Table for " + num + " ===");
        for (int i = 1; i <= 12; i++) {
            System.out.printf("%d x %2d = %3d%n", num, i, num * i);
        }
        sc.close();
    }
}

10. Infinite Loop

java
123
while (true) {
    // Runs forever until break or program termination
}

11. Common Mistakes

  • Off-by-one errors: Using < vs <= incorrectly.
  • Infinite loops: Forgetting to update the loop variable in a while loop.
  • Modifying the loop variable inside the body: Can cause unexpected behavior.

12. MCQ Quiz with Answers

Question 1

Which loop always executes at least once?

Question 2

What does break do?

Question 3

What does continue do?

Question 4

How many times does for(int i=0; i<5; i++) iterate?

Question 5

What is the for-each loop used for?

Question 6

What causes an infinite loop?

Question 7

Nested loops are:

Question 8

for(;;) creates:

Question 9

What is the output of for(int i=0; i<3; i++) { System.out.print(i); }?

Question 10

Which loop checks condition AFTER execution?

13. Interview Questions

  • Q: What is the difference between while and do-while?
  • Q: How do you avoid infinite loops?
  • Q: When should you use for vs while?
  • Q: Can break exit nested loops? (Answer: only the innermost one, use labeled break for outer)

14. Summary

Java provides for, while, do-while, and enhanced for-each loops. break exits loops, continue skips iterations. Nested loops create multi-dimensional patterns. Always ensure loop termination conditions to avoid infinite loops.

15. Next Chapter Recommendation

In Chapter 9: Arrays in Java, we'll learn how to store and manipulate collections of data efficiently.

Finish this Chapter

Save your progress on your learning path and prepare for coding interview challenges.

Discussion

Join the discussion

Log in or create a free account to participate.

Sort: ·