Free tutorials & notes in Hindi & English · Clean code examples 🧰 Free PDF & Image Tools →
🟡 Control Flow  ·  Lesson 12

Loops in Java (for, while, do-while)

In this lesson: Learn Java loops with syntax, flowcharts, dry run and programs like tables and patterns — with output. In Hindi & English.

What is a loop

A loop repeats a block of code many times without you rewriting it. Java has three main loops: for (when you know the count), while (repeat until a condition changes), and do-while (run at least once). Loops are the heart of nearly every real program.

💡 for = known count. while = check first, then run. do-while = run once, then check.

The for loop

for (int i = 1; i <= 5; i++) {
    System.out.println("Count: " + i);
}
Count: 1
Count: 2
Count: 3
Count: 4
Count: 5

A for loop has three parts: start (int i = 1), condition (i <= 5), and update (i++). It is ideal when you know exactly how many times to repeat.

Dry run of a for loop

Dry Run — for (i = 1; i <= 5; i++)
ii <= 5?Printthen i++
1yesCount: 12
2yesCount: 23
3yesCount: 34
4yesCount: 45
5yesCount: 56
6nocondition false → loop ends
💡 Understand it this way

Each turn: check the condition, run the body if true, then do the update (i++). When the condition becomes false (i = 6), the loop stops.

The while loop

int i = 1;
while (i <= 3) {
    System.out.println("i = " + i);
    i++;                       // must update, or infinite loop
}
i = 1
i = 2
i = 3

A while loop checks the condition first, then runs. Use it when you do not know the exact count. You must update the variable inside, or it never ends.

The do-while loop

int i = 10;
do {
    System.out.println("Runs once: " + i);
    i++;
} while (i <= 3);      // condition is false, but body ran once
Runs once: 10

A do-while runs the body first, then checks the condition — so it always runs at least once, even when the condition is false from the start.

break and continue

for (int i = 1; i <= 5; i++) {
    if (i == 3) continue;   // skip 3
    if (i == 5) break;      // stop at 5
    System.out.println(i);
}
1
2
4

continue skips the rest of this turn (so 3 is not printed), and break exits the loop entirely (so it stops before 5). They give fine control inside loops.

Common mistakes

  • Forgetting to update the variable. A while without i++ loops forever.
  • Off-by-one errors. i < 5 runs 4 times; i <= 5 runs 5 — pick carefully.
  • Semicolon after for. for(...); makes an empty loop — do not add ;.
  • Using do-while when it must not run once. do-while always runs at least once.

Summary

  • Loops repeat code without rewriting it.
  • for is best when you know the count.
  • while checks first; do-while runs once then checks.
  • break exits a loop; continue skips to the next turn.
  • Always update the loop variable to avoid infinite loops.

Next, make decisions with if-else in Java, or draw shapes in Floyd's triangle.

Frequently Asked Questions

What is a loop in Java?
A loop repeats a block of code multiple times, so you do not have to write it again and again. Java has three main loops: for, while and do-while, each suited to a different situation.
When should I use a for loop vs a while loop?
Use a for loop when you know how many times to repeat (like 1 to 10). Use a while loop when you repeat until a condition changes and you do not know the count in advance.
What is the difference between while and do-while?
A while loop checks the condition before running, so it may run zero times. A do-while loop runs the body once first, then checks, so it always runs at least once.
What do break and continue do?
break exits the loop immediately. continue skips the rest of the current turn and jumps to the next one. They give you finer control over how a loop behaves.
What causes an infinite loop?
An infinite loop happens when the condition never becomes false — usually because you forgot to update the loop variable. Always make sure something inside the loop moves it toward ending.
← Back to Java Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 Live Code Editor

This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.
Next topicArrays in Java Your progress: 12 / 61 (20%) · Lesson 13 / 61 Continue →

How was this lesson?

Your feedback helps us make the content better.

Write a review or suggestion (optional)

After approval, your name and city may appear publicly with your review. Please do not write your email or phone number.

▶ CodingEasily on YouTube

Learn with video lessons in easy Hindi

Featured video: RDBMS Part 3 – Class XI Commerce IT

▶ Subscribe – it’s free