📖 Scenario: Imagine you have a rocket that needs to count down before launch. We will write a simple program that counts down from a number to zero using a while loop.
🎯 Goal: Build a C++ program that uses a while loop to count down from 5 to 0 and print each number.
📋 What You'll Learn
Create an integer variable called count and set it to 5
Use a while loop that runs while count is greater than or equal to 0
Inside the loop, print the current value of count
Decrease count by 1 in each loop iteration
Print the countdown numbers on separate lines
💡 Why This Matters
🌍 Real World
Counting down is common in timers, games, and events where you wait before starting something important.
💼 Career
Understanding loops is essential for programming tasks like automation, data processing, and controlling program flow.
Progress0 / 4 steps
1
Set up the countdown variable
Create an integer variable called count and set it to 5.
C++
Hint
Use int count = 5; inside the main function.
2
Write the while loop condition
Add a while loop that runs while count is greater than or equal to 0.
C++
Hint
Use while (count >= 0) { } to start the loop.
3
Print the count and decrease it
Inside the while loop, print the current value of count using std::cout, then decrease count by 1 using count--.
C++
Hint
Use std::cout << count << std::endl; to print and count--; to decrease.
4
Run the program to see the countdown
Add a return 0; at the end of main and run the program to print the countdown from 5 to 0.
C++
Hint
Run the program and check the output shows numbers from 5 down to 0, each on its own line.
Practice
(1/5)
1.
What does a while loop do in C++?
easy
A. Executes code only once
B. Repeats code a fixed number of times
C. Stops the program immediately
D. Repeats code while a condition is true
Solution
Step 1: Understand the purpose of a while loop
A while loop runs the code inside it repeatedly as long as the condition is true.
Step 2: Compare options with definition
Only Repeats code while a condition is true correctly states this behavior.
Final Answer:
Repeats code while a condition is true -> Option D
Quick Check:
while loop = repeat while true [OK]
Hint: While loops run as long as condition is true [OK]
Common Mistakes:
Thinking while loops run a fixed number of times
Confusing while with if statement
Assuming while loops always run once
2.
Which of the following is the correct syntax for a while loop in C++?
???
{
// code
}
easy
A. while condition
B. while {condition}
C. while (condition)
D. while: (condition)
Solution
Step 1: Recall correct while loop syntax
The condition must be inside parentheses after the keyword while.
Step 2: Check each option
while (condition) uses while (condition), which is correct syntax in C++.
Final Answer:
while (condition) -> Option C
Quick Check:
while loop syntax = while (condition) [OK]
Hint: Condition in while must be inside parentheses [OK]
Common Mistakes:
Omitting parentheses around condition
Using curly braces instead of parentheses
Adding colon after while
3.
What is the output of this C++ code?
int i = 1;
while (i <= 3) {
std::cout << i << " ";
i++;
}
medium
A. 1 2 3 4
B. 1 2 3
C. 0 1 2
D. 3 2 1
Solution
Step 1: Analyze loop condition and increments
Variable i starts at 1 and increases by 1 each loop until i > 3.
Step 2: Trace output values
i = 1 prints "1 ", then 2 prints "2 ", then 3 prints "3 ". Loop stops after i becomes 4.
Final Answer:
1 2 3 -> Option B
Quick Check:
Prints 1 to 3 with spaces [OK]
Hint: Count from start to condition limit inclusive [OK]
Common Mistakes:
Printing extra number after loop ends
Starting count from 0 instead of 1
Printing numbers in reverse
4.
Find the error in this code snippet:
int count = 5;
while (count > 0)
std::cout << count << " ";
medium
A. Infinite loop because count is never changed
B. Variable count is not declared
C. Syntax error in while condition
D. Missing semicolon after cout statement
Solution
Step 1: Check loop body and variable changes
The loop prints count but does not change it inside the loop.
Step 2: Understand loop behavior
Since count stays 5, condition is always true, causing an infinite loop.
Final Answer:
Infinite loop because count is never changed -> Option A
Quick Check:
Loop variable must change to avoid infinite loop [OK]
Hint: Update loop variable inside while to avoid infinite loop [OK]
Common Mistakes:
Forgetting to change loop variable
Assuming loop ends automatically
Misreading syntax as error
5.
Write a while loop in C++ that prints all even numbers from 2 to 10 inclusive.
Which code snippet correctly does this?
???
hard
A. int n = 2;
while (n <= 10) {
std::cout << n << " ";
n += 2;
}
B. int n = 1;
while (n < 10) {
if (n % 2 == 0) std::cout << n << " ";
n++;
}
C. int n = 2;
while (n < 10) {
std::cout << n << " ";
n += 2;
}
D. int n = 0;
while (n <= 10) {
std::cout << n << " ";
n += 2;
}
Solution
Step 1: Understand the requirement
We need to print even numbers from 2 to 10 inclusive, so numbers: 2,4,6,8,10.
Step 2: Check each option
int n = 2;
while (n <= 10) {
std::cout << n << " ";
n += 2;
} starts at 2, increments by 2, and stops at 10 inclusive, printing correct numbers.
Final Answer:
2 4 6 8 10 -> Option A
Quick Check:
Start at 2, add 2 each time, stop at 10 [OK]
Hint: Start at 2, add 2, stop at 10 inclusive [OK]
Common Mistakes:
Stopping before 10 (using < instead of <=)
Starting at 1 and missing even numbers
Including 0 which is not in range
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.