📖 Scenario: Imagine you are organizing a small event and need to count the number of guests arriving. You want to stop counting once 5 guests have arrived to avoid overcrowding.
🎯 Goal: You will write a simple C++ program that uses a loop to count guests arriving. You will learn how to control the loop to stop counting after 5 guests.
📋 What You'll Learn
Create an integer variable called guestCount and set it to 0
Create an integer variable called maxGuests and set it to 5
Use a while loop that runs while guestCount is less than maxGuests
Inside the loop, increase guestCount by 1 each time
Print the message "Guest number: X" where X is the current guest count
After the loop, print "Counting stopped after 5 guests."
💡 Why This Matters
🌍 Real World
Loop control helps in real life when you want to repeat tasks but stop after a certain point, like counting people, processing orders, or checking items.
💼 Career
Understanding loop control is essential for programming jobs because it helps write efficient and safe code that does not run forever or miss important steps.
Progress0 / 4 steps
1
Create the guest count variable
Create an integer variable called guestCount and set it to 0.
C++
Hint
Use int guestCount = 0; to start counting guests from zero.
2
Set the maximum number of guests
Create an integer variable called maxGuests and set it to 5.
C++
Hint
Use int maxGuests = 5; to set the limit for counting guests.
3
Write the loop to count guests
Write a while loop that runs while guestCount is less than maxGuests. Inside the loop, increase guestCount by 1 and print "Guest number: " followed by the current guestCount.
C++
Hint
Use while (guestCount < maxGuests) and inside increase guestCount by 1. Use std::cout to print the guest number.
4
Print the final message after counting
After the loop, print the message "Counting stopped after 5 guests." using std::cout.
C++
Hint
Use std::cout << "Counting stopped after 5 guests." << std::endl; after the loop.
Practice
(1/5)
1. Why is loop control important in C++ programming?
easy
A. To automatically fix syntax errors in loops
B. To make the program run faster by skipping all loops
C. To avoid writing any loops in the code
D. To manage when and how loops start, continue, or stop
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 D
Quick Check:
Loop control = Manage loop flow [OK]
Hint: Loop control manages loop flow and stopping [OK]
Common Mistakes:
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
2. Which of the following is the correct syntax to immediately exit a loop in C++?
easy
A. exit;
B. break;
C. stop;
D. continue;
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; and stop; are invalid here.
Final Answer:
break; -> Option B
Quick Check:
Exit loop = break [OK]
Hint: Use break to exit loops immediately [OK]
Common Mistakes:
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
3. What will be the output of the following C++ code?
for(int i = 1; i <= 5; i++) {
if(i == 3) continue;
std::cout << i << " ";
}
medium
A. 1 2 4 5
B. 1 2 3 4 5
C. 3 4 5
D. 1 2
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 A
Quick Check:
continue skips 3, output = 1 2 4 5 [OK]
Hint: continue skips current iteration output [OK]
Common Mistakes:
Including 3 in output despite continue
Confusing continue with break
Forgetting loop runs from 1 to 5
4. Identify the error in this C++ loop control usage:
for(int i = 0; i < 5; i++) {
if(i == 2)
continue
std::cout << i << " ";
}
medium
A. Using continue inside a for loop is not allowed
B. Incorrect loop condition
C. Missing semicolon after continue statement
D. std::cout syntax is wrong
Solution
Step 1: Check syntax of continue statement
The continue statement 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 C
Quick Check:
continue needs semicolon [OK]
Hint: Always put semicolon after continue [OK]
Common Mistakes:
Omitting semicolon after continue
Thinking continue is invalid in for loops
Misreading loop condition as error
5. You want to print numbers from 1 to 10 but skip multiples of 3 using loop control. Which code snippet correctly achieves this?
hard
A. for(int i=1; i<=10; i++) { if(i % 3 == 0) continue; std::cout << i << " "; }
B. for(int i=1; i<=10; i++) { if(i % 3 == 0) exit(0); std::cout << i << " "; }
C. for(int i=1; i<=10; i++) { if(i % 3 == 0) return; std::cout << i << " "; }
D. for(int i=1; i<=10; i++) { if(i % 3 == 0) break; std::cout << i << " "; }
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.