Why loops are needed in Java - Performance Analysis
Loops help us repeat actions many times without writing the same code again and again.
We want to see how the time to run code changes when we use loops.
Analyze the time complexity of the following code snippet.
for (int i = 0; i < n; i++) {
System.out.println(i);
}
This code prints numbers from 0 up to n-1 using a loop.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Printing a number inside the loop.
- How many times: Exactly n times, once for each number from 0 to n-1.
As n grows, the number of print actions grows the same way.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 prints |
| 100 | 100 prints |
| 1000 | 1000 prints |
Pattern observation: The work grows directly with n; double n means double the prints.
Time Complexity: O(n)
This means the time to run the code grows in a straight line as the input size grows.
[X] Wrong: "The loop runs only once no matter the input size."
[OK] Correct: The loop runs once for each number up to n, so more input means more repeats.
Understanding loops and their time cost helps you explain how programs handle repeated tasks efficiently.
"What if we added a nested loop inside this loop? How would the time complexity change?"
Practice
Why do we use loops in Java programming?
Solution
Step 1: Understand the purpose of loops
Loops allow repeating the same code multiple times without writing it again and again.Step 2: Compare with other options
Options A, B, and D describe other programming concepts, not loops.Final Answer:
To repeat a block of code multiple times without rewriting it -> Option AQuick Check:
Loops repeat code = C [OK]
- Confusing loops with variables
- Thinking loops create new data types
- Believing loops stop program execution
Which of the following is the correct syntax for a for loop in Java that runs 5 times?
for (int i = 0; i < 5; i++) {
System.out.println(i);
}Solution
Step 1: Check Java for loop syntax
The correct syntax uses: for (initialization; condition; update) { ... }Step 2: Validate each option
for (int i = 0; i < 5; i++) { ... } matches correct syntax. B and C use invalid keywords. D has wrong update (i-- decreases).Final Answer:
for (int i = 0; i < 5; i++) { ... } -> Option DQuick Check:
Java for loop syntax = A [OK]
- Using 'to' instead of semicolons
- Wrong loop variable update (i-- instead of i++)
- Using non-Java keywords like 'loop'
What will be the output of this Java code?
for (int i = 1; i <= 3; i++) {
System.out.print(i + " ");
}Solution
Step 1: Analyze loop range
The loop starts at i=1 and runs while i <= 3, so i takes values 1, 2, 3.Step 2: Determine printed output
Each iteration prints i followed by a space, so output is "1 2 3 "Final Answer:
1 2 3 -> Option BQuick Check:
Prints 1 to 3 with spaces = B [OK]
- Starting loop at 0 instead of 1
- Including extra number beyond 3
- Printing in reverse order
Find the error in this Java loop code:
for (int i = 0; i < 5; i--) {
System.out.println(i);
}Solution
Step 1: Check loop update expression
The loop decreases i (i--) but condition is i < 5, so i moves away from stopping condition.Step 2: Understand loop behavior
Since i starts at 0 and decreases, i will always be less than 5, causing infinite loop.Final Answer:
The loop variable 'i' is decreasing, causing an infinite loop -> Option CQuick Check:
Decreasing i with i < 5 causes infinite loop = A [OK]
- Confusing i-- with i++
- Ignoring loop condition logic
- Assuming print statement causes error
You want to print all even numbers from 2 to 10 using a loop. Which code snippet correctly uses a for loop to do this?
Solution
Step 1: Understand the goal
We want to print even numbers from 2 to 10 in ascending order.Step 2: Evaluate each option
for (int i = 2; i <= 10; i += 2) { System.out.print(i + " "); } starts at 2, increments by 2, prints 2,4,6,8,10 correctly.
for (int i = 1; i <= 10; i++) { if (i % 2 == 1) System.out.print(i + " "); } prints odd numbers 1 3 5 7 9 (incorrect).
for (int i = 2; i < 10; i++) { System.out.print(i * 2 + " "); } prints multiples of 2 but stops at i=9, so last number is 18 (incorrect).
for (int i = 10; i >= 2; i--) { if (i % 2 == 0) System.out.print(i + " "); } prints even numbers but in descending order (10 to 2).Step 3: Choose best answer
for (int i = 2; i <= 10; i += 2) { System.out.print(i + " "); } is simplest and matches requirement exactly.Final Answer:
for (int i = 2; i <= 10; i += 2) { System.out.print(i + " "); } -> Option AQuick Check:
Increment by 2 from 2 to 10 = D [OK]
- Using wrong loop bounds
- Printing odd numbers by mistake
- Confusing ascending with descending order
Ready to prove it?
Pass the test and this pattern moves unknown
