For loop execution model in C Sharp (C#) - Time & Space Complexity
When we use a for loop, we want to know how the time it takes grows as we increase the number of times the loop runs.
We ask: How does the work inside the loop add up when the loop runs more times?
Analyze the time complexity of the following code snippet.
for (int i = 0; i < n; i++)
{
Console.WriteLine(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 gets bigger, the number of times the loop runs 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; if n doubles, work doubles.
Time Complexity: O(n)
This means the time to finish 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: Printing still takes time each loop, so total time grows with n, no matter how simple the action is.
Understanding how loops grow with input size helps you explain code efficiency clearly and confidently in real situations.
"What if we added a nested loop inside this for loop? How would the time complexity change?"
Practice
for loop in C#?Solution
Step 1: Understand the structure of a for loop
A for loop has three parts inside parentheses: initialization, condition, and increment/decrement.Step 2: Match parts to options
Initialization sets the start value, condition checks if the loop continues, and increment/decrement changes the variable each time.Final Answer:
Initialization, condition check, and increment/decrement -> Option BQuick Check:
for loop parts = initialization, condition, increment [OK]
- Confusing for loop parts with general program steps
- Thinking for loop has only two parts
- Mixing up condition and increment roles
for loop in C# that counts from 0 to 4?Solution
Step 1: Check syntax correctness
In C#, the for loop syntax requires parentheses around initialization, condition, and increment, and braces for the loop body.Step 2: Validate each option
for (int i = 0; i < 4; i++) { } only counts 0 to 3 (missing 4); for int i = 0; i < 5; i++ { } misses parentheses; for (int i = 0; i < 5; i++) { } is correct syntax counting 0 to 4; for (i = 0; i < 5; i++) { } misses type declaration for i.Final Answer:
for (int i = 0; i < 5; i++) { } -> Option AQuick Check:
Correct for loop syntax uses parentheses and semicolons [OK]
- Omitting parentheses around for loop parts
- Missing type declaration for loop variable
- Using wrong comparison operators
for (int i = 1; i <= 3; i++)
{
Console.Write(i + " ");
}Solution
Step 1: Analyze loop range
The loop starts at i=1 and runs while i is less than or equal to 3, so it runs for i=1, 2, 3.Step 2: Determine output per iteration
Each iteration prints the current i followed by a space, so output is "1 ", "2 ", "3 ".Final Answer:
1 2 3 -> Option AQuick Check:
Loop prints 1 to 3 with spaces [OK]
- Assuming loop starts at 0 instead of 1
- Including an extra number beyond the condition
- Printing in reverse order
for (int i = 0; i < 5; i++)
Console.WriteLine(i);
Console.WriteLine("Done");Solution
Step 1: Check loop body structure
Without braces, only the first statement after for is inside the loop; the second statement runs once after the loop.Step 2: Understand effect on output
"Done" prints once after loop finishes, which might be unexpected if intended inside loop.Final Answer:
Missing braces causes only first line to loop -> Option DQuick Check:
Use braces to include multiple statements in loop [OK]
- Assuming all lines are inside loop without braces
- Thinking semicolon is needed after for header
- Confusing variable declaration errors
Solution
Step 1: Understand the goal
We want to print even numbers from 2 to 10 inclusive, so loop variable should start at 2 and go up to 10.Step 2: Check each option
for (int i = 2; i <= 10; i += 2) increments by 2 starting at 2, so it prints 2,4,6,8,10. for (int i = 1; i <= 10; i++) if (i % 2 == 0) has a condition inside loop but header increments by 1; it works but is less efficient. for (int i = 0; i < 10; i += 2) starts at 0 and stops before 10, missing 10. for (int i = 2; i < 10; i++) if (i % 2 == 0) increments by 1 and stops before 10, missing 10.Final Answer:
for (int i = 2; i <= 10; i += 2) -> Option CQuick Check:
Increment by 2 from 2 to 10 for even numbers [OK]
- Using i++ and filtering inside loop instead of incrementing by 2
- Starting loop at 0 or 1 instead of 2
- Stopping loop before reaching 10
Ready to prove it?
Pass the test and this pattern moves unknown
