A loop is used to execute a block of code repeatedly until a specified condition is met.
| Loop Type | Condition Check | Executes At Least Once? | When to Use |
|---------------|------------------|---------------------------|-----------------------------------------|
| `for` | Before loop body | Only if condition is true | When number of iterations is known |
| `while` | Before loop body | Only if condition is true | When number of iterations is unknown |
| `do-while` | After loop body | Always executes once | When block must run **at least once** |
| Feature | `for` loop | `while` loop | `do-while` loop |
|----------------------|-------------------------------------|----------------------------------------|----------------------------------------|
| Condition Check | Before executing the block | Before executing the block | After executing the block |
| Guaranteed Execution | No | No | Yes (executes at least once) |
| Use Case | Known number of iterations | Unknown number of iterations | Run block once, then check condition |
Yes, We can create infinite loops with any of the 3 loop types. Use break to exit the loop.
while (true) {
if (Condition)
break;
}The loop executes once, even if the condition is false.
break: Terminates the loop entirely.
for (int i = 1; i <= 5; i++) { // break example
if (i == 3)
break;
System.out.println(i);
}continue: Skips the current iteration and jumps to the next.
for (int i = 1; i <= 5; i++) { // continue example
if (i == 3)
continue;
System.out.println(i);
}for (int i = 1; i <= 5; i++) {
if (i == 3) continue;
System.out.print(i + " ");
}Yes, nested loops are common in multi-dimensional structures like 2D arrays.
for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= 2; j++) {
System.out.print("* ");
}
System.out.println();
}int i = 1;
while (i <= 3) {
System.out.print(i + " ");
i++;
}int i = 10;
do {
System.out.print(i + " ");
i++;
} while (i < 5);for (int i = 1; i <= 5; i++) {
if (i == 4) break;
System.out.print(i + " ");
}int i = 1;
while (true) {
if (i == 3) break;
System.out.print(i + " ");
i++;
}for (int i = 1; i <= 2; i++) {
for (int j = 1; j <= 3; j++) {
System.out.print(i + "," + j + " ");
}
System.out.println();
}Use do-while when the block must run at least once, like menu-based programs or input validations.
while (true) {
System.out.println("This will never stop unless we break");
}Use the break statement inside a condition.
int i = 1;
while (true) {
if (i == 5)
break;
System.out.println(i);
i++;
}It causes an infinite loop because the condition never becomes false.
int i = 1;
while (i <= 5) {
System.out.println(i); // Infinite loop, i never changes
// i++; // Missing update!
}A new variable is created in every iteration, wasting memory.
for (int i = 1; i <= 5; i++) {
int x = 10; // Created again and again
System.out.println(x + i);
}Create variable outside:
int x = 10;
for (int i = 1; i <= 5; i++) {
System.out.println(x + i);
}Yes. break helps exit the loop before the condition becomes false.
int i = 1;
while (i <= 10) {
if (i == 5)
break;
System.out.println(i);
i++;
}int i = 10;
do {
System.out.println(i);
i--;
} while (i >= 1);int x = 5;
while (x > 0) {
System.out.println(x);
x--;
}int i = 10;
do {
System.out.println(i);
i++;
} while (i < 5);int i = 1;
while (true) {
if (i == 4)
break;
System.out.println("Value is: " + i);
i++;
}int i = 0;
while (i < 3) {
i++;
if (i == 2)
continue; // Skips printing when i = 2
System.out.println("i = " + i);
}
| Mistake | Example | Fix |
|-------------------------------------------|------------------------------------------ |---------------------------------------------|
| Not updating the loop variable | while(i < 10) { ... } (but i++ missing) | Always update the condition |
| Semicolon after FOR or WHILE block | for(i=0;i<5;i++); | Adds an empty loop body |
| Forgetting braces for multiple statements | Only one line runs if `{}` are omitted | Always use `{}` for clarity |
| Loop | When to Use | Common Use Case |
| ---------- | ---------------------------------------- | ------------------------- |
| FOR | When the number of iterations is known | Iterating arrays |
| WHILE | When the number of iterations is unknown | User input until valid |
| D0-WHILE | When the loop must run at least once | User menus or retry input |