A program normally executes one statement, then the next statement, then the next.
That works until a requirement says something like:
- Print numbers from
1to100. - Process every employee in an array.
- Keep asking for input until the user enters a valid value.
- Search for a product until it is found.
- Repeat an operation for every row and column.
- Skip invalid records but continue processing valid ones.
- Stop processing as soon as a required value is found.
You could write the same statement repeatedly:
System.out.println(1);
System.out.println(2);
System.out.println(3);
System.out.println(4);
System.out.println(5);But imagine doing this for 10,000 values.
That would be repetitive, difficult to maintain, and completely impractical.
So programming languages provide a way to say:
"Execute this block repeatedly according to a rule."
That mechanism is called a loop.
This chapter covers Java loops, loop-control statements, loop patterns, array and collection iteration, and the mistakes developers commonly make while working with iteration.
1. Why Loops Are Required#
Before learning loop syntax, understand the problem loops solve.
Suppose we want to print numbers from 1 through 5.
Without a loop:
public class Main {
public static void main(String[] args) {
System.out.println(1);
System.out.println(2);
System.out.println(3);
System.out.println(4);
System.out.println(5);
}
}This technically works.
But now the requirement changes:
Print numbers from1through1,000,000.
Writing one million println() statements is clearly not reasonable.
What actually changes between those statements?
Only the value:
1
2
3
4
5
...The operation stays the same:
System.out.println(...)So what we really want is:
Start with 1
↓
Print it
↓
Increase it
↓
Still within the limit?
↓ Yes
Repeat
↓ No
StopThat is the basic idea behind iteration.
What is iteration?#
Iteration means performing one repetition of a loop.
Consider:
for (int i = 1; i <= 3; i++) {
System.out.println(i);
}The loop body executes three times.
Therefore:
- first execution = first iteration
- second execution = second iteration
- third execution = third iteration
Output:
1
2
3What does a loop normally need?#
Most loops involve three ideas:
Starting State
↓
Condition
↓
Execute Work
↓
Change State
↓
Check Condition AgainFor example:
i = 1
↓
Is i <= 5?
↓
Print i
↓
i++
↓
Check againThe variable controlling this repetition is often called a loop control variable.
2. Before Learning Individual Loops: The Core Mental Model#
Different Java loops look different syntactically, but internally they revolve around one question:
Should the loop execute another iteration?
Consider:
int i = 1;
while (i <= 3) {
System.out.println(i);
i++;
}Execution:
i = 1
↓
i <= 3 ? → true
↓
print 1
↓
i becomes 2
↓
i <= 3 ? → true
↓
print 2
↓
i becomes 3
↓
i <= 3 ? → true
↓
print 3
↓
i becomes 4
↓
i <= 3 ? → false
↓
Loop terminatesOutput:
1
2
3There are several ways Java expresses this repetition:
| Loop | Best mental model |
|---|---|
for | Repeat with an explicit counter/progression |
while | Repeat while a condition remains true |
do-while | Execute once, then decide whether to repeat |
enhanced for | Process each element of an array or Iterable |
We will now understand why each one exists instead of merely memorizing syntax.
3. The for Loop#
Suppose we know in advance that something should happen five times.
We need:
- a starting value
- a condition
- an update after every iteration
We could write all of these separately with while, but because this pattern is extremely common, Java gives us a compact structure that keeps these three pieces together.
for Loop Syntax#
for (initialization; condition; update) {
// statements
}Example:
for (int i = 1; i <= 5; i++) {
System.out.println(i);
}Output:
1
2
3
4
5Let's understand the three important parts.
int i = 1This is the initialization.
It establishes the starting state.
i <= 5This is the condition.
The loop continues while this expression evaluates to true.
i++This is the update expression.
It executes after each normal iteration.
Exact Execution Order#
One of the most important interview and debugging concepts is the execution order.
For:
for (int i = 1; i <= 3; i++) {
System.out.println(i);
}Java conceptually performs:
1. int i = 1
↓
2. Check i <= 3
↓ true
3. Execute loop body
↓
4. Execute i++
↓
5. Check i <= 3 again
↓
6. RepeatInitialization happens only once.
Condition may execute multiple times.
Update normally executes after every completed iteration.
Detailed trace#
Initialization:
i = 1
Condition:
1 <= 3 → true
Body:
print 1
Update:
i = 2
Condition:
2 <= 3 → true
Body:
print 2
Update:
i = 3
Condition:
3 <= 3 → true
Body:
print 3
Update:
i = 4
Condition:
4 <= 3 → false
STOPNotice something important:
The condition was checked four times, even though the body executed only three times.
The final condition check is what determines that execution should stop.
4. Understanding i++#
A new expression appeared:
i++;Before continuing, understand it.
i++ increments i by 1.
For this standalone usage:
i++;it effectively means:
i = i + 1;Example:
int i = 5;
i++;
System.out.println(i);Output:
6For loop control, you'll commonly see:
i++or:
i--i-- decreases the variable by 1.
5. Counting Backward#
Loops don't have to move forward.
Requirement:
5to1.
for (int i = 5; i >= 1; i--) {
System.out.println(i);
}Output:
5
4
3
2
1Mental model:
Start 5
↓
Is value >= 1?
↓
Print
↓
Decrease by 1
↓
Repeat6. Changing the Step Size#
The update expression does not have to change the variable by exactly 1.
Requirement:
Print even numbers from2through10.
for (int i = 2; i <= 10; i += 2) {
System.out.println(i);
}Output:
2
4
6
8
10Here:
i += 2;means:
i = i + 2;Another example:
for (int i = 10; i >= 0; i -= 2) {
System.out.println(i);
}Output:
10
8
6
4
2
07. More Than One Variable in a for Loop#
A for statement may initialize and update multiple compatible variables.
for (int left = 0, right = 10; left < right; left++, right--) {
System.out.println(left + " " + right);
}Possible output:
0 10
1 9
2 8
3 7
4 6Important distinction:
Java does not generally have a C-style comma operator.
But the initialization and update portions of a for statement allow comma-separated expressions/declarators according to Java's for syntax.
8. Scope of a for Loop Variable#
Consider:
for (int i = 0; i < 3; i++) {
System.out.println(i);
}The variable i declared inside the for initialization belongs to the scope of that loop.
This will not compile:
public class Main {
public static void main(String[] args) {
for (int i = 0; i < 3; i++) {
System.out.println(i);
}
System.out.println(i);
}
}i is no longer available outside the loop.
If you genuinely need it afterward:
int i;
for (i = 0; i < 3; i++) {
System.out.println(i);
}
System.out.println("Final i = " + i);Output:
0
1
2
Final i = 3Prefer the smallest reasonable variable scope. Don't move loop variables outside merely because you can.
9. Missing Parts in a for Loop#
Java allows all three major for components to be omitted.
Example:
int i = 1;
for (; i <= 5; i++) {
System.out.println(i);
}Initialization occurred before the loop.
This is valid.
You could even write:
int i = 1;
for (; i <= 5;) {
System.out.println(i);
i++;
}Still valid.
And:
for (;;) {
System.out.println("Running");
}is also valid.
But this last example has no termination condition.
It is an infinite loop, which we will discuss later.
10. When Should You Choose for?#
A practical rule:
Do I have an obvious counter, range, or progression?
↓
Yes
↓
Use forExamples:
1through100- array indexes
- fixed retry attempts
- countdowns
- stepping through positions
- repeating something
ntimes
11. The while Loop#
Now consider another problem.
Suppose you don't know exactly how many attempts will be required.
For example:
Keep processing while the system has pending work.
You may not know whether there will be:
- 1 iteration
- 5 iterations
- 500 iterations
- zero iterations
The key idea is not "repeat 10 times."
The key idea is:
Repeat while some condition remains true.
That is where while fits naturally.
Syntax#
while (condition) {
// statements
}Example:
int i = 1;
while (i <= 5) {
System.out.println(i);
i++;
}Output:
1
2
3
4
512. while Execution Flow#
Condition
↓
true? ── No ──→ Stop
│
Yes
↓
Execute body
↓
Return to conditionNotice the important point:
while checks its condition before entering the body.This means the body may execute zero times.
Example:
int number = 10;
while (number < 5) {
System.out.println(number);
}Output:
No outputBecause:
10 < 5 → falseThe body is never entered.
13. while and State Changes#
Consider:
int i = 1;
while (i <= 3) {
System.out.println(i);
}Before reading further, predict what happens.
i starts as 1.
Condition:
i <= 3is true.
But does anything change i?
No.
Therefore Java keeps seeing:
1 <= 3 → trueagain and again.
The loop does not terminate naturally.
Correct version:
int i = 1;
while (i <= 3) {
System.out.println(i);
i++;
}One essential rule should now be clear:
If termination depends on changing state, make sure something inside the loop actually changes that state.
14. Practical while Example#
Imagine processing jobs while work remains.
int pendingJobs = 3;
while (pendingJobs > 0) {
System.out.println("Processing job...");
pendingJobs--;
}
System.out.println("All jobs processed.");Output:
Processing job...
Processing job...
Processing job...
All jobs processed.In production, pendingJobs might come from application state rather than a hardcoded integer, but the conceptual pattern is the same.
15. When Should You Choose while?#
Use while naturally when continuation is driven primarily by a condition rather than a clean counter.
Examples:
While connection remains active
While input is invalid
While queue is not empty
While data is available
While operation has not completed
While balance remains positiveDecision rule:
Known counter/range?
→ for
Condition-driven repetition?
→ while16. The do-while Loop#
There is one limitation in while.
Its condition is tested before execution.
Therefore its body may execute zero times.
But some requirements say:
Perform the action first. Then decide whether it should happen again.
Consider a menu.
The program should display the menu at least once.
Only after that do we ask whether the user wants to continue.
For such cases Java provides do-while.
Syntax#
do {
// statements
} while (condition);Notice the semicolon:
while (condition);It is required in the do-while syntax.
Example:
int i = 1;
do {
System.out.println(i);
i++;
} while (i <= 5);Output:
1
2
3
4
517. Why do-while Is Different#
Consider:
int number = 10;
do {
System.out.println(number);
} while (number < 5);Output:
10Why?
Execution order:
Execute body first
↓
Print 10
↓
Check 10 < 5
↓
false
↓
StopCompare that with:
int number = 10;
while (number < 5) {
System.out.println(number);
}Output:
No outputThis produces the fundamental difference:
while | do-while |
|---|---|
| Condition checked first | Body executed first |
| Zero or more executions | One or more executions |
| Entry-controlled | Exit-controlled |
| Good when work may not be needed | Good when first execution is mandatory |
18. Typical do-while Use Cases#
Historically common examples include:
- console menus
- retry prompts
- input validation
- repeating a user interaction
- command-line applications
Example:
int choice = 1;
do {
System.out.println("Menu displayed");
choice++;
} while (choice <= 3);In production applications, the exact UI mechanism may be different, but the language behavior remains important.
19. Choosing Between for, while, and do-while#
Now we can build a useful decision model.
Need repetition
│
├── Known range/count/progression?
│ └── for
│
├── Condition determines whether execution begins?
│ └── while
│
└── Must execute at least once?
└── do-whileComparison#
| Dimension | for | while | do-while |
|---|---|---|---|
| Initial condition check | Yes | Yes | No |
| Minimum body executions | 0 | 0 | 1 |
| Counter-friendly | Excellent | Possible | Possible |
| Condition-driven work | Possible | Excellent | Good |
| Update location | Usually header | Usually body | Usually body |
| Typical readability | High for counting | High for unknown iterations | High for execute-once-first requirements |
These loops are not different in computational power for most ordinary tasks. Often one can be rewritten using another.
The correct choice is usually about expressing intent clearly.
20. Enhanced for Loop#
Suppose we have an array:
int[] numbers = {10, 20, 30, 40};A traditional indexed loop works:
for (int i = 0; i < numbers.length; i++) {
System.out.println(numbers[i]);
}But imagine our requirement is simply:
Give me every value.
We don't actually care about i.
The index exists only because the traditional loop requires us to reach each array position.
Java therefore provides a shorter form for element-by-element traversal.
This is commonly called the:
- enhanced
forloop - for-each loop
Syntax#
for (Type variable : arrayOrIterable) {
// use variable
}Example:
int[] numbers = {10, 20, 30, 40};
for (int number : numbers) {
System.out.println(number);
}Output:
10
20
30
40Read it naturally as:
For eachnumberinnumbers, execute the body.
21. Enhanced for with Objects#
String[] names = {"Amit", "Riya", "Neha"};
for (String name : names) {
System.out.println(name);
}Output:
Amit
Riya
NehaThe variable:
namereceives each array element, one at a time.
22. Important Limitation of Enhanced for#
Suppose you need the element's index:
Position 0
Position 1
Position 2The enhanced loop does not directly provide the index.
Use an indexed loop:
String[] names = {"Amit", "Riya", "Neha"};
for (int i = 0; i < names.length; i++) {
System.out.println(i + " -> " + names[i]);
}Output:
0 -> Amit
1 -> Riya
2 -> NehaDecision rule:
Need only each element?
→ enhanced for
Need position/index?
→ traditional indexed for23. Reassigning the Enhanced-Loop Variable#
This is an important trap.
Consider:
int[] numbers = {1, 2, 3};
for (int number : numbers) {
number = number * 10;
}
for (int number : numbers) {
System.out.println(number);
}What do you expect?
Output:
1
2
3Why didn't the array become:
10
20
30Because the enhanced-loop variable:
numbercontains the current primitive value.
Changing that local variable doesn't rewrite the array slot.
To modify array elements, use their indexes:
int[] numbers = {1, 2, 3};
for (int i = 0; i < numbers.length; i++) {
numbers[i] = numbers[i] * 10;
}Now the array contains:
10
20
30For object references, the nuance is different: reassigning the local reference does not replace the collection/array element, but mutating the referenced object's state can affect the same object.
24. Internal Model of Enhanced for#
This is useful for interviews.
For arrays:
for (int number : numbers) {
System.out.println(number);
}Java handles array traversal using array positions internally according to the language's enhanced-for translation model.
Conceptually:
Array
↓
Element 0
↓
Element 1
↓
Element 2
↓
...For an Iterable, such as most collection types:
for (String name : names) {
System.out.println(name);
}the enhanced-for mechanism is based on an Iterator.
Conceptual model:
Iterable
↓
iterator()
↓
hasNext()
↓
next()
↓
Loop body
↓
hasNext()
↓
...We'll return to Iterator when we discuss collection iteration.
25. Nested Loops#
So far every loop has contained ordinary statements.
But Java allows a loop inside another loop.
Before introducing the name, imagine this requirement:
For every row, process every column.
If there are:
3 rows
4 columns per rowthen processing looks like:
Row 1 → Column 1, 2, 3, 4
Row 2 → Column 1, 2, 3, 4
Row 3 → Column 1, 2, 3, 4That naturally requires one repetition to exist inside another.
This is called loop nesting.
Example#
for (int row = 1; row <= 3; row++) {
for (int column = 1; column <= 4; column++) {
System.out.println("Row " + row + ", Column " + column);
}
}The outer loop controls rows.
The inner loop controls columns.
26. Nested Loop Execution#
For:
for (int i = 1; i <= 2; i++) {
for (int j = 1; j <= 3; j++) {
System.out.println(i + " " + j);
}
}Output:
1 1
1 2
1 3
2 1
2 2
2 3Execution:
Outer i = 1
↓
Inner j = 1
Inner j = 2
Inner j = 3
↓
Outer i = 2
↓
Inner j = 1
Inner j = 2
Inner j = 3The inner loop finishes all of its iterations for each outer-loop iteration.
27. Multiplication Table with Nested Loops#
for (int row = 1; row <= 3; row++) {
for (int column = 1; column <= 3; column++) {
System.out.print((row * column) + "\t");
}
System.out.println();
}Output:
1 2 3
2 4 6
3 6 928. Time Complexity Intuition#
A new concept matters here.
Suppose the outer loop executes n times.
For each outer iteration, the inner loop also executes n times.
Approximately:
n × n = n²operations occur.
This is commonly described as:
O(n²)You don't need a complete algorithm-analysis course here. The important idea is:
Nested loops can multiply the amount of work.
But don't automatically assume every nested loop is O(n²).
Example:
for (int i = 0; i < n; i++) {
for (int j = 0; j < 5; j++) {
// work
}
}The inner loop always runs only five times.
Work grows approximately as:
5nwhich is linear with respect to n.
So analyze actual iteration counts instead of counting braces.
29. Infinite Loops#
Most loops are expected to terminate.
But what happens if their continuation condition never becomes false?
The loop keeps executing.
That is an infinite loop.
Example:
while (true) {
System.out.println("Running");
}This does not naturally terminate.
Another form:
for (;;) {
System.out.println("Running");
}Also infinite.
And this accidental form:
int i = 1;
while (i <= 5) {
System.out.println(i);
}is also effectively infinite because i never changes.
30. Are Infinite Loops Always Wrong?#
No.
This distinction matters.
An infinite loop can be:
- intentional
- accidental
Intentional loops are used in systems such as:
- long-running worker processes
- event-processing loops
- servers
- message consumers
- game loops
- embedded systems
A simplified worker pattern:
while (true) {
// wait for and process work
}Production implementations need proper lifecycle control, interruption, error handling, shutdown behavior, and resource management. A naked while (true) is not automatically a complete production design.
The problem isn't "infinite loop = wrong."
The problem is:
Does the loop have a valid lifecycle and a controlled termination strategy when termination is required?
31. break#
Imagine searching an array.
int[] numbers = {10, 20, 30, 40, 50};We want to find 30.
Once 30 has been found, should we continue inspecting 40 and 50?
No.
The useful work is finished.
Java therefore gives us a statement that exits the nearest applicable loop immediately.
That statement is break.
Example#
int[] numbers = {10, 20, 30, 40, 50};
for (int number : numbers) {
if (number == 30) {
System.out.println("Found");
break;
}
System.out.println("Checked " + number);
}Output:
Checked 10
Checked 20
FoundValues after 30 are not processed.
32. break Execution Model#
Loop iteration
↓
Condition for break?
│
No
↓
Continue body
↓
Next iteration
If Yes:
↓
break
↓
Exit loop immediately
↓
Continue after loopExample:
for (int i = 1; i <= 10; i++) {
if (i == 4) {
break;
}
System.out.println(i);
}
System.out.println("Finished");Output:
1
2
3
FinishedWhen i == 4, break executes before println(i).
33. break in while#
break isn't limited to for.
int i = 1;
while (true) {
if (i > 3) {
break;
}
System.out.println(i);
i++;
}Output:
1
2
3Here the loop condition itself is always true, but a controlled break terminates execution.
This can be legitimate when the exit rule is clearer inside the body.
34. continue#
Now consider a different requirement.
Suppose we want to process numbers 1 through 5, except number 3.
When we reach 3, we don't want to terminate the entire loop.
We only want to skip the current iteration.
That requires different behavior.
continue does exactly that.
Example#
for (int i = 1; i <= 5; i++) {
if (i == 3) {
continue;
}
System.out.println(i);
}Output:
1
2
4
5The loop did not terminate.
Only the remainder of the i == 3 iteration was skipped.
35. break vs continue#
This distinction must be crystal clear.
break
→ exit the loop
continue
→ skip the rest of the current iteration
→ attempt the next iterationComparison:
| Dimension | break | continue |
|---|---|---|
| Terminates loop | Yes | No |
| Skips current remainder | Yes | Yes |
| Future iterations occur | No | Normally yes |
| Typical use | Search completed | Ignore one item |
| Example | Stop after match found | Skip invalid record |
36. A Common continue Bug in while#
Look carefully:
int i = 0;
while (i < 5) {
if (i == 2) {
continue;
}
System.out.println(i);
i++;
}What happens?
Trace:
i = 0 → print → i = 1
i = 1 → print → i = 2
i = 2 → continue
i still = 2
i = 2 → continue
i still = 2
...Infinite loop.
The update was placed after the possible continue.
One correction:
int i = 0;
while (i < 5) {
if (i == 2) {
i++;
continue;
}
System.out.println(i);
i++;
}A cleaner structure may avoid the duplicated increment:
int i = 0;
while (i < 5) {
if (i != 2) {
System.out.println(i);
}
i++;
}This demonstrates why control-flow statements must be considered together with loop-state updates.
37. Labeled break#
Nested loops introduce another problem.
Consider:
for (int row = 0; row < 3; row++) {
for (int column = 0; column < 3; column++) {
if (row == 1 && column == 1) {
break;
}
System.out.println(row + "," + column);
}
}The break exits only the nearest enclosing loop.
It exits the inner loop, but the outer loop continues.
Sometimes we need:
Exit a particular outer structure immediately.
Java provides labels.
Syntax#
labelName:
for (...) {
...
}Then:
break labelName;Example:
outer:
for (int row = 0; row < 3; row++) {
for (int column = 0; column < 3; column++) {
if (row == 1 && column == 1) {
break outer;
}
System.out.println(row + "," + column);
}
}
System.out.println("Finished");Output:
0,0
0,1
0,2
1,0
Finishedbreak outer; exits the statement marked with outer.
In loop-oriented code, labels are usually encountered for nested-loop control.
38. Labeled continue#
Sometimes we don't want to terminate the outer loop.
Instead, we want to stop the current inner work and move to the next iteration of an outer loop.
That is where labeled continue can help.
outer:
for (int row = 1; row <= 3; row++) {
for (int column = 1; column <= 3; column++) {
if (column == 2) {
continue outer;
}
System.out.println(row + "," + column);
}
}Output:
1,1
2,1
3,1When:
column == 2Java performs:
continue outer;So the remainder of the current outer-loop iteration is abandoned and the next outer iteration begins.
39. Important Label Rule#
break label; and continue label; are related but not identical.
A labeled break can exit its labeled statement.
A labeled continue must target an appropriate enclosing loop/iteration statement because "continue" means move to another iteration.
This is valid:
outer:
for (int i = 0; i < 5; i++) {
continue outer;
}Labels are useful, but heavy use can make control flow harder to understand.
In many production designs, extracting nested logic into well-named methods can improve readability.
So don't conclude:
"Labels are bad."
Instead:
Use them when they express nested-loop control more clearly than the available alternatives.
40. Loop Control Variables#
We've repeatedly used variables such as:
i
j
row
column
attemptThese often determine:
- current position
- number of completed iterations
- whether a boundary has been reached
- how execution progresses
Good loop-control variable properties#
A loop-control variable should usually have:
- an understandable initial value
- a clear condition
- a predictable update
- a reachable termination state
Example:
for (int attempt = 1; attempt <= 3; attempt++) {
System.out.println("Attempt " + attempt);
}This is easy to reason about.
41. Meaningful Names vs i#
Is i bad?
No.
For a tiny local index loop:
for (int i = 0; i < numbers.length; i++) {
System.out.println(numbers[i]);
}i is conventional and easy to understand.
But deeply nested logic benefits from better names:
Less readable:
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[i].length; j++) {
System.out.println(matrix[i][j]);
}
}Still acceptable because i and j are common matrix indexes.
For domain logic, however:
for (int employeeIndex = 0; employeeIndex < employees.length; employeeIndex++) {
// ...
}may communicate intent better.
Choose names based on complexity and scope.
42. Off-by-One Errors#
One of the most common loop mistakes involves boundaries.
Suppose an array has five elements:
int[] numbers = {10, 20, 30, 40, 50};Indexes are:
0
1
2
3
4Not:
1
2
3
4
5The correct loop:
for (int i = 0; i < numbers.length; i++) {
System.out.println(numbers[i]);
}A dangerous version:
for (int i = 0; i <= numbers.length; i++) {
System.out.println(numbers[i]);
}Why?
When:
i == numbers.lengththe code tries to access index 5.
But valid indexes end at 4.
Result:
ArrayIndexOutOfBoundsExceptionThe classic array pattern is:
i < array.lengthnot:
i <= array.length43. Loop Patterns#
Once syntax becomes comfortable, most real loop problems start looking like recurring patterns.
Recognizing these patterns is more valuable than memorizing dozens of isolated examples.
Pattern 1 — Counter#
Requirement:
Count how many values match a rule.
int[] numbers = {3, 8, 10, 5, 12};
int evenCount = 0;
for (int number : numbers) {
if (number % 2 == 0) {
evenCount++;
}
}
System.out.println(evenCount);Output:
3Mental model:
Start count at 0
↓
Inspect each item
↓
Match?
→ count++44. Pattern 2 — Accumulator#
Requirement:
Calculate the total.
int[] prices = {100, 200, 300};
int total = 0;
for (int price : prices) {
total += price;
}
System.out.println(total);Output:
600total is an accumulator.
It carries the partial result from one iteration to the next.
45. Pattern 3 — Search#
Requirement:
Determine whether a value exists.
int[] numbers = {10, 20, 30, 40};
int target = 30;
boolean found = false;
for (int number : numbers) {
if (number == target) {
found = true;
break;
}
}
System.out.println(found);Output:
trueWhy use break?
Once the target is found, further searching is unnecessary.
46. Pattern 4 — Find First Match#
int[] numbers = {5, 12, 7, 20};
int firstEven = -1;
for (int number : numbers) {
if (number % 2 == 0) {
firstEven = number;
break;
}
}
System.out.println(firstEven);Output:
12The sentinel value -1 means "not found" in this particular domain.
But be careful:
If -1 can be a valid data value, it becomes ambiguous.
In richer domain code, another representation may be better.
47. Pattern 5 — Minimum#
int[] numbers = {8, 3, 12, 2, 9};
int minimum = numbers[0];
for (int i = 1; i < numbers.length; i++) {
if (numbers[i] < minimum) {
minimum = numbers[i];
}
}
System.out.println(minimum);Output:
2Why start from:
numbers[0]instead of 0?
Suppose:
{-8, -3, -15}If minimum started as 0, it could still work for this negative example, but using an actual element creates a general data-derived baseline.
However, there is an important edge case:
numbers[0]is invalid for an empty array.
So production code must define how empty input should be handled.
48. Pattern 6 — Maximum#
int[] numbers = {8, 3, 12, 2, 9};
int maximum = numbers[0];
for (int i = 1; i < numbers.length; i++) {
if (numbers[i] > maximum) {
maximum = numbers[i];
}
}
System.out.println(maximum);Output:
1249. Pattern 7 — Filter/Skip#
Requirement:
Process only positive values.
int[] numbers = {5, -3, 8, -1, 10};
for (int number : numbers) {
if (number < 0) {
continue;
}
System.out.println(number);
}Output:
5
8
10An alternative:
for (int number : numbers) {
if (number >= 0) {
System.out.println(number);
}
}Neither is universally better.
If skipping invalid items early reduces deep nesting, continue can improve readability.
50. Pattern 8 — Flag-Controlled Loop#
boolean running = true;
int count = 0;
while (running) {
count++;
if (count == 3) {
running = false;
}
}
System.out.println(count);Output:
3This pattern is useful when the loop's lifecycle is controlled by state.
51. Pattern 9 — Sentinel-Controlled Loop#
A sentinel is a special value indicating that processing should stop.
Conceptually:
Read value
↓
Is value sentinel?
→ Yes: stop
→ No: process and continueSimplified example:
int[] input = {10, 20, 30, -1, 40};
for (int value : input) {
if (value == -1) {
break;
}
System.out.println(value);
}Output:
10
20
30Here -1 acts as the sentinel.
52. Pattern 10 — Bounded Retry#
Real applications should often avoid uncontrolled retries.
Example:
boolean success = false;
for (int attempt = 1; attempt <= 3 && !success; attempt++) {
System.out.println("Attempt " + attempt);
if (attempt == 2) {
success = true;
}
}Output:
Attempt 1
Attempt 2Production retries are more complicated: delays, backoff, idempotency, transient failures, logging, metrics, and failure classification may matter.
But the fundamental bounded-loop pattern is worth understanding.
53. Pattern 11 — Reverse Traversal#
int[] numbers = {10, 20, 30, 40};
for (int i = numbers.length - 1; i >= 0; i--) {
System.out.println(numbers[i]);
}Output:
40
30
20
10Initial index:
numbers.length - 1because the final valid array position is always one less than the length.
54. Pattern 12 — Two Ends Moving Toward Each Other#
Suppose we want to compare values from both ends.
int[] numbers = {1, 2, 3, 2, 1};
boolean same = true;
for (int left = 0, right = numbers.length - 1;
left < right;
left++, right--) {
if (numbers[left] != numbers[right]) {
same = false;
break;
}
}
System.out.println(same);Output:
trueThis demonstrates how one loop can maintain multiple control variables.
55. Iterating Arrays#
Arrays are one of the most common places beginners first use loops.
Suppose:
int[] scores = {80, 90, 75, 95};There are two common traversal approaches.
Indexed loop#
for (int i = 0; i < scores.length; i++) {
System.out.println(scores[i]);
}Enhanced loop#
for (int score : scores) {
System.out.println(score);
}Which one should you choose?
Need element only?
→ enhanced for
Need index?
→ indexed for
Need to replace array positions?
→ indexed for
Need custom stepping/reverse traversal?
→ indexed for56. Updating an Array#
Requirement:
Increase every score by 5.Use indexes because array positions must be replaced.
int[] scores = {80, 90, 75};
for (int i = 0; i < scores.length; i++) {
scores[i] += 5;
}
for (int score : scores) {
System.out.println(score);
}Output:
85
95
8057. Two-Dimensional Arrays#
A two-dimensional array naturally introduces nested loops.
int[][] matrix = {
{1, 2, 3},
{4, 5, 6}
};Indexed traversal:
for (int row = 0; row < matrix.length; row++) {
for (int column = 0; column < matrix[row].length; column++) {
System.out.print(matrix[row][column] + " ");
}
System.out.println();
}Output:
1 2 3
4 5 6Notice:
matrix[row].lengthrather than assuming every row has the same size.
Java supports arrays whose rows have different lengths.
Example:
int[][] data = {
{1, 2},
{3, 4, 5},
{6}
};This is sometimes called a jagged or ragged array.
Correct traversal:
for (int row = 0; row < data.length; row++) {
for (int column = 0; column < data[row].length; column++) {
System.out.print(data[row][column] + " ");
}
System.out.println();
}58. Iterating Collections#
Arrays have fixed-size language-level structure.
Java also provides the Collections Framework with types such as:
ListSetMap
We only need enough collection knowledge here to understand iteration.
59. Iterating a List#
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) {
List<String> names = new ArrayList<>();
names.add("Amit");
names.add("Riya");
names.add("Neha");
for (String name : names) {
System.out.println(name);
}
}
}Output:
Amit
Riya
NehaThe enhanced loop works because List types are iterable.
60. Iterating with an Iterator#
A new concept appears here.
An Iterator is an object used to move through elements sequentially.
Typical operations include:
hasNext()which asks:
Is another element available?
and:
next()which returns the next element.
Example:
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class Main {
public static void main(String[] args) {
List<String> names = new ArrayList<>();
names.add("Amit");
names.add("Riya");
names.add("Neha");
Iterator<String> iterator = names.iterator();
while (iterator.hasNext()) {
String name = iterator.next();
System.out.println(name);
}
}
}Output:
Amit
Riya
NehaConceptually:
List
↓
iterator()
↓
hasNext()?
↓ Yes
next()
↓
Process element
↓
Repeat61. Why Would We Use Iterator Directly?#
One important reason is controlled removal during iteration.
Risky:
List<String> names = new ArrayList<>();
names.add("Amit");
names.add("Riya");
names.add("Neha");
for (String name : names) {
if (name.equals("Riya")) {
names.remove(name);
}
}Structural modification of many ordinary collections while using their fail-fast iteration mechanism can cause ConcurrentModificationException.
A standard iterator-removal approach:
Iterator<String> iterator = names.iterator();
while (iterator.hasNext()) {
String name = iterator.next();
if (name.equals("Riya")) {
iterator.remove();
}
}Important precision:
ConcurrentModificationException is a fail-fast behavior used by many standard collection iterators. It should not be treated as a synchronization guarantee, and not every collection has identical iteration behavior.
62. Iterating a Set#
import java.util.HashSet;
import java.util.Set;
public class Main {
public static void main(String[] args) {
Set<String> technologies = new HashSet<>();
technologies.add("Java");
technologies.add("Spring");
technologies.add("Angular");
for (String technology : technologies) {
System.out.println(technology);
}
}
}Do not rely on HashSet iteration order unless the chosen collection type/API contract provides the ordering semantics you require.
63. Iterating a Map#
A Map stores key-value mappings.
Example:
import java.util.HashMap;
import java.util.Map;
public class Main {
public static void main(String[] args) {
Map<Integer, String> employees = new HashMap<>();
employees.put(101, "Amit");
employees.put(102, "Riya");
employees.put(103, "Neha");
for (Map.Entry<Integer, String> entry : employees.entrySet()) {
System.out.println(entry.getKey() + " -> " + entry.getValue());
}
}
}Using:
entrySet()is a natural choice when both key and value are needed.
If only keys are required:
for (Integer id : employees.keySet()) {
System.out.println(id);
}If only values are required:
for (String name : employees.values()) {
System.out.println(name);
}64. Why entrySet() Is Often Preferred for Key + Value#
This:
for (Integer key : map.keySet()) {
String value = map.get(key);
}works for many maps.
But if you need both pieces, this expresses intent directly:
for (Map.Entry<Integer, String> entry : map.entrySet()) {
Integer key = entry.getKey();
String value = entry.getValue();
}The exact performance difference depends on the map implementation, but entrySet() also avoids redundant conceptual lookup and clearly communicates "iterate mappings."
65. Traditional Loop vs Enhanced Loop vs Iterator#
| Requirement | Preferred starting point |
|---|---|
| Need numeric index | Traditional for |
| Reverse traversal | Traditional for |
| Custom stepping | Traditional for |
| Only process every element | Enhanced for |
| Need iterator-specific removal | Iterator |
| Condition-driven external state | while |
| Execute before first condition test | do-while |
Don't select syntax based only on what is shortest.
Select the structure that best communicates the intended control flow.
66. Common Loop Mistakes#
Loops are simple syntactically, but they produce many real bugs because a small mistake can repeat thousands or millions of times.
We will examine the important ones carefully.
Mistake 1 — Wrong Boundary#
Risky#
int[] values = {10, 20, 30};
for (int i = 0; i <= values.length; i++) {
System.out.println(values[i]);
}Why developers make it#
They think:
"I need to reach the length."
But array indexes stop at:
length - 1Consequence#
ArrayIndexOutOfBoundsException.
Preferred#
for (int i = 0; i < values.length; i++) {
System.out.println(values[i]);
}Debugging clue#
Look at:
- initial index
- comparison operator
- final valid index
Interview connection#
Classic off-by-one question.
67. Mistake 2 — Missing Update#
Buggy#
int i = 1;
while (i <= 5) {
System.out.println(i);
}Why risky#
i remains 1.
Consequence#
Infinite execution until externally stopped or the environment fails/intervenes.
Correct#
int i = 1;
while (i <= 5) {
System.out.println(i);
i++;
}68. Mistake 3 — Updating in the Wrong Direction#
Buggy#
for (int i = 1; i <= 5; i--) {
System.out.println(i);
}i begins at 1.
Condition:
i <= 5is true.
Then:
i--makes values:
0
-1
-2
...Those values remain <= 5.
The intended termination boundary is never approached.
Correct:
for (int i = 1; i <= 5; i++) {
System.out.println(i);
}69. Mistake 4 — Accidental Semicolon After Loop Header#
Look at:
for (int i = 0; i < 3; i++); {
System.out.println("Hello");
}The semicolon immediately after for (...) is an empty loop body.
The following block is separate.
Therefore "Hello" prints only once after the loop finishes.
Indented deceptively:
for (int i = 0; i < 3; i++);
{
System.out.println("Hello");
}Preferred:
for (int i = 0; i < 3; i++) {
System.out.println("Hello");
}70. Mistake 5 — do-while Semicolon Confusion#
Unlike ordinary while syntax:
while (condition) {
}a do-while requires:
do {
} while (condition);The final semicolon is part of the syntax.
So don't mechanically apply the "semicolon after loop is always wrong" rule.
Context matters.
71. Mistake 6 — Modifying the Enhanced-For Primitive Variable#
Risky assumption:
int[] numbers = {1, 2, 3};
for (int number : numbers) {
number *= 2;
}Some learners expect the array to become:
2, 4, 6It does not.
Use indexes when replacing array values:
for (int i = 0; i < numbers.length; i++) {
numbers[i] *= 2;
}72. Mistake 7 — Structural Modification During Collection Iteration#
Risky:
for (String item : items) {
if (item.isEmpty()) {
items.remove(item);
}
}For many ordinary collections, this can interfere with fail-fast iteration.
Possible better approaches include:
Iterator<String> iterator = items.iterator();
while (iterator.hasNext()) {
if (iterator.next().isEmpty()) {
iterator.remove();
}
}or an appropriate collection API such as:
items.removeIf(String::isEmpty);removeIf was added in Java 8 via the Collection API.
Don't use an alternative API merely because it is shorter; choose based on the actual requirement and mutation semantics.
73. Mistake 8 — Assuming All Collections Support Index-Based Access Efficiently#
This code is natural for ArrayList:
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}But suppose list is a LinkedList.
Repeated indexed get(i) operations can require traversal through the list, potentially making a seemingly simple loop much more expensive.
For sequential traversal:
for (String value : list) {
System.out.println(value);
}is usually a better general expression.
Production lesson:
Understand the data structure you're iterating, not just the loop syntax.
74. Mistake 9 — Expensive Work Repeated in Loop Conditions#
Consider conceptually:
while (calculateExpensiveState()) {
process();
}This may be correct if the state genuinely must be recalculated every iteration.
But if the value doesn't need repeated calculation, unnecessary work can become significant.
Never hoist a condition blindly, though.
If its changing value determines loop correctness, evaluating it each iteration is required.
Optimization must preserve semantics.
75. Mistake 10 — Integer Overflow in Loop Control#
Consider:
for (int i = 0; i >= 0; i++) {
}It may look infinite.
But Java int eventually overflows from:
2147483647to:
-2147483648At that point:
i >= 0becomes false.
This would take a large number of iterations, but the conceptual lesson matters:
Primitive numeric types have finite ranges.
Another dangerous boundary pattern:
for (int i = start; i <= Integer.MAX_VALUE; i++) {
}When i reaches Integer.MAX_VALUE, incrementing overflows to Integer.MIN_VALUE.
Depending on the condition, behavior may surprise you.
76. Mistake 11 — Floating-Point Loop Counters#
Consider:
for (double value = 0.0; value != 1.0; value += 0.1) {
System.out.println(value);
}Binary floating-point representation means decimal values such as 0.1 are not always represented exactly.
Therefore equality-based termination can be unreliable.
For fixed iteration counts, prefer an integer counter:
for (int i = 0; i <= 10; i++) {
double value = i / 10.0;
System.out.println(value);
}General production rule:
Avoid depending on exact floating-point equality for loop termination unless you fully understand the representation and requirement.
77. Mistake 12 — Wrong Nested-Loop Variable#
Bug:
for (int i = 0; i < 3; i++) {
for (int j = 0; i < 3; j++) {
System.out.println(i + "," + j);
}
}Look carefully.
Inner condition:
i < 3instead of:
j < 3Since i doesn't change inside the inner loop, the inner loop may become effectively infinite until some other failure such as integer overflow behavior influences it.
Correct:
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
System.out.println(i + "," + j);
}
}This is why i/j mistakes are common in nested logic.
78. Mistake 13 — Misunderstanding break Scope#
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (j == 1) {
break;
}
}
}A plain break exits the inner loop.
It does not automatically terminate every surrounding loop.
Use a label or restructure the logic if outer termination is required.
79. Mistake 14 — Misunderstanding continue#
Incorrect mental model:
continue = stop loopCorrect:
continue = stop current iteration's remaining work
and proceed according to loop continuation rulesFor a for loop, the update expression is still involved when continuing to the next iteration.
For while, you must ensure required state changes haven't been accidentally skipped.
80. Mistake 15 — Deep Nesting#
Technically valid:
for (...) {
if (...) {
for (...) {
if (...) {
for (...) {
// ...
}
}
}
}
}The problem is not compilation.
It is maintainability.
Deep control flow can make it difficult to understand:
- which condition applies
- what
breakexits - what
continueskips - where state changes
- why execution terminates
Possible improvements:
- guard clauses
- helper methods
- better data structures
- algorithm redesign
- early exits
The best solution depends on the requirement.
81. Edge Cases and Traps#
Empty array#
int[] values = {};
for (int value : values) {
System.out.println(value);
}The body executes zero times.
Perfectly valid.
But this is not valid:
int minimum = values[0];because there is no first element.
82. Null Array#
int[] values = null;
for (int value : values) {
System.out.println(value);
}This throws:
NullPointerExceptionbecause there is no array object to iterate.
Production code should define whether null is:
- impossible by contract
- invalid input
- equivalent to empty
- something that must be rejected
Do not randomly add null checks without understanding the API contract.
83. Empty Collection#
List<String> names = new ArrayList<>();
for (String name : names) {
System.out.println(name);
}No iterations occur.
This is normal.
84. Loop over Mutable State#
Consider:
int limit = 5;
for (int i = 0; i < limit; i++) {
limit--;
}Both i and limit change.
Tracing this mentally becomes harder.
The loop may terminate earlier than expected.
Whenever the boundary itself changes inside a loop, ask:
Is that behavior intentional?
85. Loop Condition with Side Effects#
Possible:
while (index++ < 5) {
// ...
}But mixing state modification into complex conditions can reduce readability and increase off-by-one risk.
Often clearer:
while (index < 5) {
index++;
}This is not a universal ban on side effects in expressions. The point is to prefer control flow that other developers can reason about reliably.
86. Internal Working: What the JVM Actually Sees#
At source-code level, Java offers:
for
while
do-while
enhanced forBut compiled bytecode ultimately uses conditional and unconditional branch instructions to control execution.
Conceptually:
Condition check
↓
Conditional jump
↓
Body
↓
Jump backThe Java compiler converts high-level loop syntax into lower-level control flow.
Later, the JVM's runtime execution engine and JIT compiler may optimize hot code.
This is why two different loop syntaxes can produce similar low-level behavior.
You should choose high-level syntax mainly for:
- correctness
- intent
- maintainability
- appropriate access pattern
rather than assuming one syntax is always fundamentally faster.
87. Enhanced for Translation Concept#
For an array, the language's model is conceptually similar to maintaining:
array reference
index
array lengthFor an Iterable, it is conceptually similar to:
Iterator<Type> iterator = collection.iterator();
while (iterator.hasNext()) {
Type value = iterator.next();
}This matters because it explains:
- why enhanced-for supports arrays
- why it supports
Iterable - why index is not exposed
- why collection iterator behavior affects enhanced-for iteration
88. Production Performance Perspective#
Loops themselves are extremely common and not inherently problematic.
Performance problems usually come from what happens inside them or from the number of iterations.
Suppose:
for (Customer customer : customers) {
loadOrdersFromDatabase(customer);
}If there are 10,000 customers and every iteration performs a separate database query, the real performance problem may be the repeated I/O.
The loop is simply exposing a broader design issue.
Potential concerns:
Loop
↓
How many iterations?
↓
What work per iteration?
↓
CPU?
Memory allocation?
Database call?
Network call?
Disk I/O?
Lock?
Nested scan?Always analyze total work.
89. Avoid Premature Micro-Optimization#
Do not automatically rewrite:
for (String item : items)into some obscure loop because you heard that one form might theoretically be faster.
Modern JVMs perform sophisticated runtime optimizations.
Choose the clear correct algorithm first.
Optimize when:
- profiling identifies a bottleneck
- the complexity is obviously problematic
- data-volume characteristics justify it
- latency/throughput requirements demand it
90. Nested Loops and Algorithmic Cost#
Suppose:
for (Customer customer : customers) {
for (Order order : orders) {
if (order.getCustomerId() == customer.getId()) {
// ...
}
}
}If there are:
100,000 customers
100,000 orderscomparing every order against every customer can be extremely expensive.
A better data structure, lookup table, query strategy, or algorithm may reduce the work.
Production lesson:
Often the best loop optimization is not a different loop syntax. It is a better algorithm or data structure.
91. Readability and Maintainability#
Compare:
for (int i = 0; i < employees.size(); i++) {
System.out.println(employees.get(i).getName());
}with:
for (Employee employee : employees) {
System.out.println(employee.getName());
}If index is irrelevant, the enhanced form better communicates:
Process every employee.
But if the index matters:
for (int i = 0; i < employees.size(); i++) {
System.out.println((i + 1) + ". " + employees.get(i).getName());
}the indexed version expresses the requirement naturally.
92. break vs Flag#
Two possible approaches:
Flag#
boolean found = false;
for (int value : values) {
if (value == target) {
found = true;
}
}Early exit#
boolean found = false;
for (int value : values) {
if (value == target) {
found = true;
break;
}
}If we need only existence, early exit avoids unnecessary later comparisons.
But don't force break if all items must be processed for another reason.
The requirement determines the correct pattern.
93. continue vs Nested if#
Approach A:
for (Employee employee : employees) {
if (employee.isActive()) {
process(employee);
}
}Approach B:
for (Employee employee : employees) {
if (!employee.isActive()) {
continue;
}
process(employee);
}Both can be good.
When processing logic becomes large, early continue can reduce nesting.
When the body is tiny, the direct if can be simpler.
This is a readability decision, not a universal correctness rule.
94. Loop Testing Strategy#
Loops need boundary-oriented tests.
Suppose a method processes:
for (int i = 0; i < values.length; i++) {
}Useful tests include:
- empty array
- one element
- several elements
- first element matches
- last element matches
- no element matches
- duplicates
- negative values where relevant
- minimum/maximum domain values
- null if contract allows or rejects null
For nested loops:
- zero rows
- one row
- uneven row sizes
- empty inner row
- large dimensions
Testing boundaries catches many loop defects.
95. Practical Example — Employee Salary Total#
Requirement:
Calculate the total salary of all active employees.
Learning model:
class Employee {
String name;
double salary;
boolean active;
Employee(String name, double salary, boolean active) {
this.name = name;
this.salary = salary;
this.active = active;
}
}Process:
Employee[] employees = {
new Employee("Amit", 50000, true),
new Employee("Riya", 60000, false),
new Employee("Neha", 55000, true)
};
double total = 0;
for (Employee employee : employees) {
if (!employee.active) {
continue;
}
total += employee.salary;
}
System.out.println(total);Output:
105000.0Concepts combined:
Enhanced for
+
Condition
+
continue
+
AccumulatorThis is how real programming knowledge works: concepts combine rather than remaining isolated chapters.
96. Practical Example — Search an Employee#
Employee foundEmployee = null;
String requiredName = "Neha";
for (Employee employee : employees) {
if (employee.name.equals(requiredName)) {
foundEmployee = employee;
break;
}
}
if (foundEmployee != null) {
System.out.println("Employee found");
}Concepts:
- traversal
- comparison
- search pattern
break- state/result variable
For large searchable datasets, repeated linear searches may not be the best production strategy. A Map, database index, or other lookup structure may be more suitable.
Again:
Loop syntax is only one part of algorithm design.
97. Practical Example — Validate All Values#
Requirement:
Determine whether every score is between0and100.
int[] scores = {80, 90, 75, 110};
boolean valid = true;
for (int score : scores) {
if (score < 0 || score > 100) {
valid = false;
break;
}
}
System.out.println(valid);Output:
falseWhy break?
The requirement asks:
Are all values valid?
The first invalid value is sufficient to prove the final answer is false.
98. Practical Example — First Duplicate Using Nested Loops#
Learning example:
int[] values = {4, 8, 2, 8, 5};
boolean duplicateFound = false;
outer:
for (int i = 0; i < values.length; i++) {
for (int j = i + 1; j < values.length; j++) {
if (values[i] == values[j]) {
System.out.println("Duplicate: " + values[i]);
duplicateFound = true;
break outer;
}
}
}
if (!duplicateFound) {
System.out.println("No duplicates");
}Output:
Duplicate: 8This is useful for learning nested loops and labeled break.
However, the nested scan is approximately quadratic for large inputs.
A production solution may use a Set to detect duplicates more efficiently.
Learning example and production design are not always identical.
99. Comparison: Indexed for vs Enhanced for#
| Dimension | Indexed for | Enhanced for |
|---|---|---|
| Gives index | Yes | No |
| Reverse traversal | Easy | Not directly |
| Custom step | Yes | No |
| Replacing array slots | Yes | Not through loop variable |
| Readability for simple traversal | Good | Usually excellent |
| Arrays | Yes | Yes |
Iterable collections | Via APIs/index where supported | Yes |
| Iterator semantics | Not necessarily | Used for Iterable |
| Best use | Position-sensitive traversal | Element-focused traversal |
Decision#
Need position/control?
→ indexed for
Need each item only?
→ enhanced for100. Comparison: break vs continue vs return#
A third statement is often confused with them: return.
Suppose we're inside a method.
void process() {
for (...) {
...
}
}continue#
Leave current iteration remainder
→ continue loopbreak#
Leave loop
→ continue method after loopreturn#
Leave method entirelyExample:
public static void demo() {
for (int i = 1; i <= 5; i++) {
if (i == 3) {
return;
}
System.out.println(i);
}
System.out.println("After loop");
}Output:
1
2After loop never executes because return exits the method.
101. Quick Decision Rules#
Repeat a known number of times?
→ for
Repeat while a condition remains true?
→ while
Must run once before testing?
→ do-while
Process every array/Iterable element?
→ enhanced for
Need array/list index?
→ indexed for
Need to exit loop immediately?
→ break
Need to skip one iteration?
→ continue
Need to exit a particular outer nested loop?
→ labeled break
Need to move directly to next iteration of outer loop?
→ labeled continue
Need iterator-controlled removal?
→ Iterator102. COMPLETE REVISION#
One-Line Definitions#
Loop: A control structure that repeatedly executes statements according to a continuation rule.
Iteration: One execution cycle of a loop.
Loop control variable: State used to control or track loop progression.
for: Loop suited to explicit initialization, condition, and progression.
while: Loop that repeats while its condition is true.
do-while: Loop that executes its body before checking whether to repeat.
Enhanced for: Element-focused traversal over arrays and Iterable objects.
Nested loop: A loop placed inside another loop.
Infinite loop: A loop with no naturally reached terminating condition.
break: Immediately exits the targeted loop or labeled statement.
continue: Skips the remaining current iteration and proceeds toward another iteration.
Labeled break: Exits a particular labeled enclosing statement.
Labeled continue: Continues a particular labeled enclosing iteration statement.
103. Syntax Revision#
for#
for (initialization; condition; update) {
// body
}while#
while (condition) {
// body
}do-while#
do {
// body
} while (condition);Enhanced for#
for (Type item : source) {
// body
}break#
break;continue#
continue;Label#
outer:
for (...) {
...
}Labeled break#
break outer;Labeled continue#
continue outer;104. If You Remember Only 10 Things#
- A loop repeats code according to a control rule.
foris usually clearest for known ranges or progression.whilechecks before executing and may run zero times.do-whileexecutes once before its first condition check.- Enhanced
foris ideal when you need elements but not indexes. - Array indexes run from
0tolength - 1. breakterminates the loop;continueskips only the current remainder.- Missing or incorrect state updates commonly create infinite loops.
- Nested loops can multiply computational work.
- Production performance depends more on algorithm, data structure, and work inside the loop than on superficial loop syntax.
105. Memory Hooks#
FOR
→ "From here to there"
WHILE
→ "As long as this is true"
DO-WHILE
→ "Do it once, then ask"
FOR-EACH
→ "Give me every element"
BREAK
→ "I'm done with this loop"
CONTINUE
→ "Skip this one"
LABEL
→ "Control that specific outer structure"106. Final Knowledge Map#
Loops and Iteration
│
├── Why Repetition Exists
│ ├── Repeated operations
│ ├── Iteration
│ └── Loop state
│
├── Core Loops
│ ├── for
│ │ ├── initialization
│ │ ├── condition
│ │ ├── update
│ │ ├── forward traversal
│ │ ├── reverse traversal
│ │ ├── custom step
│ │ └── multiple variables
│ │
│ ├── while
│ │ ├── pre-condition
│ │ ├── condition-driven execution
│ │ └── zero-or-more iterations
│ │
│ ├── do-while
│ │ ├── post-condition
│ │ └── one-or-more iterations
│ │
│ └── enhanced for
│ ├── arrays
│ ├── Iterable
│ ├── element traversal
│ └── index limitations
│
├── Complex Iteration
│ ├── nested loops
│ ├── multidimensional arrays
│ └── algorithmic cost
│
├── Loop Control
│ ├── break
│ ├── continue
│ ├── labeled break
│ └── labeled continue
│
├── Loop Patterns
│ ├── counter
│ ├── accumulator
│ ├── search
│ ├── first match
│ ├── min/max
│ ├── filter/skip
│ ├── flag
│ ├── sentinel
│ ├── bounded retry
│ ├── reverse traversal
│ └── two-ended traversal
│
├── Arrays
│ ├── indexes
│ ├── boundaries
│ ├── updates
│ └── multidimensional traversal
│
├── Collections
│ ├── enhanced for
│ ├── Iterator
│ ├── List
│ ├── Set
│ └── Map
│
├── Common Bugs
│ ├── off-by-one
│ ├── missing update
│ ├── wrong update direction
│ ├── accidental semicolon
│ ├── wrong nested variable
│ ├── unsafe structural modification
│ ├── continue/update interaction
│ ├── overflow
│ └── floating-point termination
│
└── Production Thinking
├── readability
├── algorithm complexity
├── collection characteristics
├── I/O inside loops
├── lifecycle/termination
├── mutation safety
└── boundary testing