Loops
For Loops
Structure:
Initialization: This occurs once before the loop starts, typically for setting up the loop counter (like
int i = 1). This establishes a starting point for the iteration.Condition: This is evaluated before each iteration. If
true, the loop body executes; iffalse, the loop terminates. For example,i <= 5checks if the counter is still within the specified limits.Update: After each iteration, this part executes, modifying the loop counter (like
i++which increments the counter). This is crucial for avoiding infinite loops.
Example:
for (int i = 1; i <= 5; i++) {
System.out.println(i); // Output: 1, 2, 3, 4, 5
}
In this example:
The loop initializes with
istarting at 1.It checks if
iis less than or equal to 5 during each iteration.After printing
i, it incrementsiby 1 until the condition is no longer true. This results in outputs from 1 to 5.
While Loops
Execution:
It checks the condition before executing the loop body. If the condition is
falseinitially, the loop will not run at all.
Example:
int i = 1;
while (i <= 5) {
System.out.println(i); // Output: 1, 2, 3, 4, 5
i++;
}
Here:
We start
iat 1.The loop checks if
iis less than or equal to 5, executes the print statement, and then incrementsiby 1.This continues until
iexceeds 5, hence outputting 1 through 5.
Do-While Loops
Characteristics:
This loop guarantees at least one execution of the code block, which is beneficial for scenarios like user prompts where initial interaction is necessary.
Example:
int i = 1;
do {
System.out.println(i); // Output: 1, 2, 3, 4, 5
i++;
} while (i <= 5);
In this example:
The
doblock runs at least once, irrespective ofi's initial value.After printing
i, we check the condition and may repeat the loop, illustrating how it ensures execution before checking the condition.
Nested Loops
Definition: A loop inside another loop, allowing for more complex iteration patterns, such as processing multi-dimensional data.
Execution Control:
The outer loop controls the total number of iterations, while the inner loop processes all its iterations for each cycle of the outer loop.
Example:
for (int i = 1; i <= 3; i++) { // Outer loop (3 rows)
for (int j = 1; j <= 4; j++) { // Inner loop (4 columns)
System.out.print(j + " ");
}
System.out.println(); // Move to the next line after inner loop
}
// Output:
// 1 2 3 4
// 1 2 3 4
// 1 2 3 4
Here:
The outer loop runs 3 times (for
ifrom 1 to 3).For each
i, the inner loop completes 4 iterations (forjfrom 1 to 4), printing values and creating a row of output.After the inner loop completes,
System.out.println()moves the output to the next line.
Infinite Loops
Definition: A loop that runs forever, which can cause issues if not intentionally designed.
Concept Check: It’s crucial to include a condition in the loop that changes with each iteration. For example,
int i = 1;
while (i % 2 != 0) {
System.out.println(i); // This runs indefinitely because i never changes
}
In this scenario:
The loop continues running since the condition is never altered. Hence, it will print
1indefinitely, leading to an infinite loop. This is a common pitfall in loop design that must be carefully managed.