Why loop control is required in C++ - Performance Analysis
Loops repeat actions in code, but without control, they can run too long or forever.
We want to see how controlling loops affects how long a program runs.
Analyze the time complexity of the following code snippet.
int i = 0;
while (i < 5) {
std::cout << i << std::endl;
i++;
}
This code prints numbers from 0 to 4 by increasing i each time until it reaches 5.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The while loop runs repeatedly.
- How many times: It runs 5 times, controlled by the variable i.
Explain the growth pattern intuitively.
| Input Size (n) | Approx. Operations |
|---|---|
| 5 | 5 times |
| 10 | 10 times |
| 100 | 100 times |
Pattern observation: The number of loop runs grows directly with the input number controlling it.
Time Complexity: O(n)
This means the time to finish grows in a straight line as the loop count increases.
[X] Wrong: "The loop will always stop on its own without control."
[OK] Correct: Without a condition that changes, the loop can run forever, making the program stuck.
Understanding loop control shows you can write code that runs efficiently and safely without getting stuck.
"What if we forgot to increase i inside the loop? How would the time complexity change?"
Practice
Solution
Step 1: Understand loop control purpose
Loop control helps decide when a loop should continue or stop running.Step 2: Identify correct role of loop control
It manages the flow inside loops, like stopping early or skipping steps.Final Answer:
To manage when and how loops start, continue, or stop -> Option DQuick Check:
Loop control = Manage loop flow [OK]
- Thinking loop control speeds up code by skipping all loops
- Believing loop control removes the need for loops
- Assuming loop control fixes syntax errors automatically
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;andstop;are invalid here.Final Answer:
break; -> Option BQuick Check:
Exit loop = break [OK]
- Using continue to exit the loop (it only skips iteration)
- Writing exit or stop which are not valid loop controls
- Missing the semicolon after break
for(int i = 1; i <= 5; i++) {
if(i == 3) continue;
std::cout << i << " ";
}Solution
Step 1: Analyze the loop and condition
The loop runs i from 1 to 5. When i equals 3,continue;skips printing.Step 2: Determine printed values
Numbers 1, 2, 4, and 5 are printed with spaces. Number 3 is skipped.Final Answer:
1 2 4 5 -> Option AQuick Check:
continue skips 3, output = 1 2 4 5 [OK]
- Including 3 in output despite continue
- Confusing continue with break
- Forgetting loop runs from 1 to 5
for(int i = 0; i < 5; i++) {
if(i == 2)
continue
std::cout << i << " ";
}Solution
Step 1: Check syntax of continue statement
Thecontinuestatement must end with a semicolon.Step 2: Verify other parts
The loop condition and std::cout syntax are correct; continue is allowed in for loops.Final Answer:
Missing semicolon after continue statement -> Option CQuick Check:
continue needs semicolon [OK]
- Omitting semicolon after continue
- Thinking continue is invalid in for loops
- Misreading loop condition as error
Solution
Step 1: Understand the goal
We want to skip printing numbers divisible by 3 but continue the loop.Step 2: Analyze each option
for(int i=1; i<=10; i++) { if(i % 3 == 0) break; std::cout << i << " "; } stops loop at first multiple of 3 (break). for(int i=1; i<=10; i++) { if(i % 3 == 0) continue; std::cout << i << " "; } skips multiples of 3 (continue). The snippets using exit(0) and return exit the function or program, not desired.Final Answer:
for(int i=1; i<=10; i++) { if(i % 3 == 0) continue; std::cout << i << " "; } -> Option AQuick Check:
Use continue to skip multiples of 3 [OK]
- Using break which stops the whole loop
- Using return or exit which ends function or program
- Not checking modulo condition correctly
Ready to prove it?
Pass the test and this pattern moves unknown
