Looping Statements: for, while, do-while

Estimated reading: 1 minute 17 views

Looping statements in Java are fundamental tools for executing a block of code repeatedly, based on a specified condition. They reduce redundancy and improve code efficiency, enabling developers to handle repetitive tasks dynamically. The main types of loops in Java are for, while, and do-while.

What Are Loops?

A loop is a control structure that executes a set of statements multiple times, either for a fixed number of iterations or while a condition remains true. Loops consist of the following parts:

  • Initialization: Sets up a loop control variable.
  • Condition: Determines whether the loop should continue executing.
  • Update: Modifies the loop control variable after each iteration.

1. for Loop

The for loop is ideal for situations where the number of iterations is known in advance. It combines initialization, condition checking, and update in a single line.

Syntax:

				
					for (initialization; condition; update) {
    // Code to execute
}

				
			
  • Initialization: Typically sets the loop counter to a starting value.
  • Condition: Evaluated before each iteration. The loop continues while this condition is true.
  • Update: Adjusts the loop counter after each iteration.

Example:

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

Output:

Iteration: 1
Iteration: 2
Iteration: 3
Iteration: 4
Iteration: 5
				
			

2. while Loop

The while loop is used when the number of iterations is unknown, but a condition needs to be repeatedly checked. The loop runs as long as the condition is true.

Syntax:

				
					while (condition) {
    // Code to execute
}

				
			
  • The condition is evaluated at the start of each iteration.
  • If the condition is false initially, the loop does not execute.

Example:

				
					int count = 1;
while (count <= 5) {
    System.out.println("Count: " + count);
    count++;
}

Output:
Count: 1
Count: 2
Count: 3
Count: 4
Count: 5
				
			

3. do-while Loop

The do-while loop is similar to the while loop but guarantees at least one iteration of the loop, as the condition is checked after executing the block of code.

Syntax:

				
					do {
    // Code to execute
} while (condition);

				
			
  • The block executes first, and then the condition is checked.
  • If the condition is true, the loop repeats. If false, the loop exits.

Example:

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

Output:
Number: 1
Number: 2
Number: 3
Number: 4
Number: 5

				
			

Comparison of for, while, and do-while Loops

Featurefor Loopwhile Loopdo-while Loop
Use CaseKnown number of iterations.Unknown number of iterations.Executes at least once, even if the condition is initially false.
Condition CheckAt the start of each iteration.At the start of each iteration.After each iteration.
StructureCombines initialization, condition, and update in one line.Initialization and update handled separately.Similar to while but ensures one execution.

Nested Loops

You can place one loop inside another to handle multidimensional data or complex iterations.

Example:

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

Output:

i = 1, j = 1
i = 1, j = 2
i = 1, j = 3
i = 2, j = 1
i = 2, j = 2
i = 2, j = 3
i = 3, j = 1
i = 3, j = 2
i = 3, j = 3

				
			

Complete Example Program

				
					public class LoopingExamples {
    public static void main(String[] args) {
        System.out.println("Using for loop:");
        for (int i = 1; i <= 3; i++) {
            System.out.println("Iteration: " + i);
        }

        System.out.println("\nUsing while loop:");
        int count = 1;
        while (count <= 3) {
            System.out.println("Count: " + count);
            count++;
        }

        System.out.println("\nUsing do-while loop:");
        int number = 1;
        do {
            System.out.println("Number: " + number);
            number++;
        } while (number <= 3);
    }
}
				
			

Output:

				
					Using for loop:
Iteration: 1
Iteration: 2
Iteration: 3

Using while loop:
Count: 1
Count: 2
Count: 3

Using do-while loop:
Number: 1
Number: 2
Number: 3

				
			

Key Points

  • Use a for loop when the number of iterations is known beforehand.
  • Use a while loop when the condition must be checked before each iteration.
  • Use a do-while loop when at least one iteration of the code block is required, regardless of the condition.

Understanding these looping constructs helps write concise, efficient, and readable Java programs.

Leave a Comment

Share this Doc

Looping Statements: for, while, do-while

Or copy link

CONTENTS