For loop syntax in Java - Time & Space Complexity
We want to see how the time a for loop takes changes as we increase the number of times it runs.
How does the work grow when the loop runs more times?
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, running the loop n times.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The loop runs and prints a number each time.
- How many times: Exactly n times, where n is the input size.
As n grows, the number of print operations 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, so doubling n doubles the work.
Time Complexity: O(n)
This means the time grows in a straight line with the number of loop runs.
[X] Wrong: "The loop runs faster because it just prints numbers quickly."
[OK] Correct: The speed of printing doesn't change how many times the loop runs; the number of steps still grows with n.
Understanding how loops grow with input size helps you explain how your code will behave with bigger data, a key skill in programming.
"What if we added a nested loop inside this for loop? How would the time complexity change?"
Practice
What is the main purpose of a for loop in Java?
Solution
Step 1: Understand the role of a for loop
A for loop is used to repeat code multiple times when the number of repetitions is known.Step 2: Compare with other options
Options A, B, and C describe other programming concepts, not loops.Final Answer:
To repeat a block of code a specific number of times -> Option CQuick Check:
For loop = repeat code fixed times [OK]
- Confusing for loop with if statement
- Thinking for loop stores data
- Mixing for loop with class definition
Which of the following is the correct syntax for a basic for loop in Java?
for (int i = 0; i < 5; i++) {
System.out.println(i);
}Solution
Step 1: Check the for loop syntax structure
The correct syntax requires parentheses around initialization, condition, and increment, and braces to enclose the loop body.Step 2: Validate each option
for (int i = 0; i < 5; i++) { System.out.println(i); } has correct syntax with parentheses and braces. for int i = 0; i < 5; i++ { System.out.println(i); } misses parentheses. for (int i = 0; i < 5; i++) System.out.println(i) misses semicolon after println(i). for (int i = 0; i < 5; i++) System.out.println(i); } has an extra closing brace.Final Answer:
for (int i = 0; i < 5; i++) { System.out.println(i); } -> Option AQuick Check:
Correct for loop syntax = for (int i = 0; i < 5; i++) { System.out.println(i); } [OK]
- Omitting parentheses around loop control
- Missing braces for multiple statements
- Extra or missing braces
What will be the output of the following Java code?
for (int i = 1; i <= 3; i++) {
System.out.print(i + " ");
}Solution
Step 1: Analyze the loop range
The loop starts at i=1 and runs while i is less than or equal to 3, so i takes values 1, 2, and 3.Step 2: Determine the output
Each iteration prints the current 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 = 1 2 3 [OK]
- Starting loop at 0 instead of 1
- Including extra number beyond 3
- Printing in reverse order
Identify the error in the following for loop code snippet:
for (int i = 0; i < 5; i++)
System.out.println(i);
}Solution
Step 1: Check loop syntax and braces
The for loop has no opening brace but has a closing brace, which is unmatched and causes a syntax error.Step 2: Verify other parts
Semicolons and variable declaration are correct, and the loop condition syntax is valid.Final Answer:
Extra closing brace } -> Option AQuick Check:
Unmatched brace causes syntax error = Extra closing brace}without matching opening brace [OK]
- Leaving unmatched braces
- Forgetting semicolons in loop header
- Not declaring loop variable
How can you write a for loop in Java that prints only even numbers from 2 to 10 inclusive?
Solution
Step 1: Understand the requirement
We want to print even numbers from 2 to 10 inclusive, so numbers: 2, 4, 6, 8, 10.Step 2: Analyze each option
for (int i = 2; i <= 10; i += 2) { System.out.print(i + " "); } starts at 2 and increments by 2 up to 10, printing even numbers correctly. for (int i = 1; i < 10; i++) { if (i % 2 == 0) System.out.print(i + " "); } uses a condition inside the loop but loops only up to 9, missing 10. for (int i = 2; i < 10; i++) { System.out.print(i * 2 + " "); } prints multiples of 2 starting at 4 (2*2), but loop ends before 10 and prints wrong numbers. for (int i = 0; i <= 10; i += 2) { System.out.print(i + " "); } starts at 0, which is not in the required range.Final Answer:
for (int i = 2; i <= 10; i += 2) { System.out.print(i + " "); } -> Option DQuick Check:
Increment by 2 from 2 to 10 = for (int i = 2; i <= 10; i += 2) { System.out.print(i + " "); } [OK]
- Starting loop at 0 instead of 2
- Using wrong loop condition
- Multiplying inside print instead of increment
Ready to prove it?
Pass the test and this pattern moves unknown
