Why loop control is required - Performance Analysis
Loops repeat actions many times, so how long a program takes depends a lot on them.
We want to see why controlling loops matters for how fast code runs.
Analyze the time complexity of the following code snippet.
for (int i = 0; i < n; i++) {
printf("%d\n", i);
}
This code prints numbers from 0 up to n-1 using a loop controlled by i.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The loop runs the print statement repeatedly.
- How many times: Exactly n times, controlled by the loop variable i.
As n grows, the number of times the loop runs grows the same way.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 print actions |
| 100 | 100 print actions |
| 1000 | 1000 print actions |
Pattern observation: The work grows directly with n, so doubling n doubles the work.
Time Complexity: O(n)
This means the time to finish grows in a straight line with the size of n.
[X] Wrong: "The loop will run forever if I don't control it."
[OK] Correct: Without proper control, the loop might never stop, causing the program to hang or crash.
Understanding loop control helps you write code that runs efficiently and avoids endless work, a key skill in programming.
"What if the loop condition changed from i < n to i < n*n? How would the time complexity change?"
Practice
Solution
Step 1: Understand loop behavior
Loops repeat code multiple times, so controlling when to stop or skip is important to avoid infinite loops or unwanted actions.Step 2: Identify the role of loop control
Loop control statements likebreakandcontinuehelp manage loop execution by stopping or skipping iterations.Final Answer:
To manage when a loop should stop or skip iterations -> Option AQuick Check:
Loop control = Manage loop flow [OK]
- Confusing loop control with variable declaration
- Thinking loop control prints output
- Assuming loop control defines functions
Solution
Step 1: Recall loop control keywords
In C,break;is used to exit a loop immediately.Step 2: Differentiate from other keywords
continue;skips to next iteration,exit;is not a valid statement, andstop;does not exist.Final Answer:
break; -> Option CQuick Check:
Stop loop = break [OK]
- Using 'stop;' which is invalid in C
- Confusing 'continue;' with stopping the loop
- Trying to use 'exit;' as loop control
for(int i = 1; i <= 5; i++) {
if(i == 3) {
continue;
}
printf("%d ", i);
}Solution
Step 1: Understand the loop and condition
The loop runs from 1 to 5. Wheni == 3,continue;skips printing 3.Step 2: Trace output values
Numbers 1, 2 are printed normally. At 3, printing is skipped. Then 4 and 5 are printed.Final Answer:
1 2 4 5 -> Option BQuick Check:
Skip 3 with continue = 1 2 4 5 [OK]
- Including 3 in output despite continue
- Confusing continue with break
- Expecting no output at all
int i = 0;
while(i < 5) {
if(i == 3)
break
printf("%d ", i);
i++;
}Solution
Step 1: Check syntax of break statement
Thebreakstatement must end with a semicolon (;), but it is missing here.Step 2: Verify other parts
Variableiis initialized,breakis valid inside while, andprintfsyntax is correct.Final Answer:
Missing semicolon after break statement -> Option DQuick Check:
break needs semicolon [OK]
- Forgetting semicolon after break
- Thinking break can't be inside while
- Assuming variable i is uninitialized
Solution
Step 1: Understand the goal
We want to skip printing numbers that are multiples of 3 but continue the loop for others.Step 2: Choose correct loop control
continue;skips the current iteration, so placing it when number is multiple of 3 skips printing those numbers.Final Answer:
Use continue; when number is multiple of 3 to skip printing -> Option AQuick Check:
Skip multiples of 3 = continue [OK]
- Using break to stop entire loop instead of skipping
- Placing continue outside the loop
- Trying to break after printing multiples
Ready to prove it?
Pass the test and this pattern moves unknown
