Tutorials Logic, IN info@tutorialslogic.com

Loops in Java for, while, do while

Choosing for, while, and do while

A loop form should match its control rule: counters fit for, condition-driven work fits while, post-tested work fits do-while, and traversal fits enhanced for.

A Java loop is correct only when initialization, continuation, update, and termination describe one consistent rule. Trace those four parts before changing syntax.

Loop revision should always include one normal case, one empty input case, and one boundary case. That practice catches the most common mistakes: skipped first items, skipped last items, and loops that never stop.

Test the first, last, empty, and single-item cases to expose off-by-one conditions and updates that fail to make progress.

Use a for loop when the number of repetitions is known or controlled by a counter. Use while when repetition depends on a condition that may change outside a simple counter, such as reading input until a valid value appears.

Use do while when the body must run at least once before the condition is checked. This is useful for menu prompts, retry flows, and input validation screens.

  • for is best for counting and array traversal.
  • while is best for condition-based repetition.
  • do while is best when one execution is required.
  • Every loop needs a clear stopping condition.

Repeating work safely with Java loops

Loops repeat a block of code while a condition or sequence allows it. for loops are common when the number of repetitions is known, while loops are useful when repetition depends on a condition, and do-while loops run at least once. Enhanced for loops are best when reading every item without needing the index.

The danger in loops is not the syntax; it is the stopping rule. A wrong condition can skip the first item, process one item too many, or never stop. When debugging a loop, trace the initial value, condition, body, and update step. This shows exactly why a loop repeats or exits.

  • Use for when count or index is central to the task.
  • Use while when the loop depends on a changing condition.
  • Use enhanced for when only item values are needed.
  • Check the first iteration, last iteration, and zero-iteration case.

Loops in Java for, while, do while Example

Loops in Java for, while, do while Example
public class Demo {
    public static void main(String[] args) {
        System.out.println("Practice Loops in Java for, while, do while");
    }
}

for, while, and do while in One Program

for, while, and do while in One Program
public class LoopTypes {
    public static void main(String[] args) {
        for (int i = 1; i <= 3; i++) {
            System.out.println("for count: " + i);
        }

        int attempts = 0;
        while (attempts < 2) {
            attempts++;
            System.out.println("while attempt: " + attempts);
        }

        int menuChoice = 0;
        do {
            menuChoice++;
            System.out.println("show menu once");
        } while (menuChoice < 1);
    }
}

Loop through marks and calculate total

Loop through marks and calculate total
int[] marks = {70, 80, 90};
int total = 0;

for (int mark : marks) {
    total += mark;
}

System.out.println("Total: " + total);
Before you move on

Loops in Java for, while, do while Mastery Check

5 checks
  • Use a for loop when the number of repetitions is known or controlled by a counter.
  • Before running a loop, identify its changing state, the last valid iteration, and the condition that stops execution; this exposes infinite loops and off-by-one errors.
  • Use while when repetition depends on a condition that may change outside a simple counter, such as reading input until a valid value appears.
  • Use do while when the body must run at least once before the condition is checked.
  • This is useful for menu prompts, retry flows, and input validation screens.

Loops in Java for, while, do while Questions Learners Ask

The boundary often uses <= where < was intended. Test the first and last index explicitly.

Use it when the body must run once before the condition is checked.

The condition never becomes false, often because the control variable is not updated on every path.

Browse Free Tutorials

Explore 500+ free tutorials across 20+ languages and frameworks.