Loops help repeat actions many times without writing the same code again and again.
Loop execution flow in Java
for (initialization; condition; update) { // code to repeat } while (condition) { // code to repeat } do { // code to repeat } while (condition);
The for loop repeats code while a condition is true, updating a variable each time.
The while loop repeats code as long as the condition is true, checking before each run.
for (int i = 1; i <= 5; i++) { System.out.println(i); }
int i = 1; while (i <= 5) { System.out.println(i); i++; }
int i = 1; do { System.out.println(i); i++; } while (i <= 5);
This program shows how three types of loops run and print counts from 1 to 3.
public class LoopFlow { public static void main(String[] args) { System.out.println("For loop output:"); for (int i = 1; i <= 3; i++) { System.out.println("Count: " + i); } System.out.println("While loop output:"); int j = 1; while (j <= 3) { System.out.println("Count: " + j); j++; } System.out.println("Do-while loop output:"); int k = 1; do { System.out.println("Count: " + k); k++; } while (k <= 3); } }
Loops run the code inside their block repeatedly until the condition is false.
For loops are great when you know how many times to repeat.
While loops check the condition before running the code; do-while loops run the code once before checking.
Loops repeat actions to save writing the same code many times.
For, while, and do-while are common loop types with small differences in how they check conditions.
Understanding loop flow helps control how many times your code runs.
Practice
for loop in Java primarily do?Solution
Step 1: Understand the purpose of a for loop and compare options
A for loop is designed to repeat a block of code multiple times based on a condition. Only "Repeats a block of code a specific number of times" correctly describes it.Final Answer:
Repeats a block of code a specific number of times -> Option CQuick Check:
for loop = repeat code [OK]
- Thinking for loops run only once
- Confusing loops with variable declarations
- Believing loops stop the program
while loop in Java?Solution
Step 1: Recall Java while loop syntax and check options
The correct syntax requires parentheses around the condition and braces for the code block. Only "while (condition) { // code }" matches:while (condition) { // code }.Final Answer:
while (condition) { // code } -> Option DQuick Check:
while loop syntax = parentheses + braces [OK]
- Omitting parentheses around condition
- Adding semicolon after while condition
- Using colon instead of parentheses
int count = 0;
while (count < 3) {
System.out.print(count + " ");
count++;
}Solution
Step 1: Analyze the loop condition and trace iterations
The loop runs while count is less than 3, starting from 0. It prints 0, then increments to 1; prints 1, increments to 2; prints 2, increments to 3; then stops because 3 is not less than 3.Final Answer:
0 1 2 -> Option AQuick Check:
Prints 0,1,2 then stops [OK]
- Including 3 in output
- Starting count from 1
- Printing in reverse order
int i = 0;
while (i < 5) {
System.out.println(i);
}Solution
Step 1: Check loop control variable and consequences
The variable i is initialized but never changed inside the loop. Without incrementing i, the condition i < 5 is always true, causing an infinite loop.Final Answer:
Missing increment of i inside the loop -> Option AQuick Check:
Loop needs variable update to stop [OK]
- Forgetting to increment loop variable
- Misplacing semicolons
- Declaring variable inside loop unnecessarily
int sum = 0;
for (int i = 1; i <= 5; i++) {
if (i == 3) continue;
sum += i;
}
System.out.println(sum);What is the output and why?
Solution
Step 1: Trace the loop and effect of continue
The loop runs from 1 to 5. When i equals 3, continue skips the addition for that iteration. Sum = 1 + 2 + 4 + 5 = 12 because 3 is skipped.Final Answer:
12 because number 3 is skipped in addition -> Option BQuick Check:
continue skips 3, sum = 12 [OK]
- Adding 3 despite continue
- Thinking continue causes error
- Summing only numbers less than 3
Ready to prove it?
Pass the test and this pattern moves unknown
