📖 Scenario: You are creating a simple program that categorizes a person's age group based on their age. This is useful in many real-life situations like assigning age-appropriate activities or content.
🎯 Goal: Build a C++ program that uses an else-if ladder to print the correct age group for a given age.
📋 What You'll Learn
Create an integer variable called age with the value 25
Create an integer variable called ageGroup to store the category code
Use an else-if ladder to assign ageGroup based on age: 1 for child (0-12), 2 for teenager (13-19), 3 for adult (20-59), 4 for senior (60+)
Print the ageGroup variable
💡 Why This Matters
🌍 Real World
Age group classification is used in schools, healthcare, marketing, and entertainment to provide age-appropriate services and content.
💼 Career
Understanding conditional statements like else-if ladders is fundamental for software developers to control program flow based on different conditions.
Progress0 / 4 steps
1
Create the age variable
Create an integer variable called age and set it to 25.
C++
Hint
Use int to declare an integer variable named age and assign it the value 25.
2
Create the ageGroup variable
Create an integer variable called ageGroup and initialize it to 0.
C++
Hint
Declare ageGroup as an integer and set it to zero to start.
3
Use an else-if ladder to assign ageGroup
Write an if-else if ladder to assign ageGroup based on age: set ageGroup = 1 if age is between 0 and 12 inclusive, ageGroup = 2 if between 13 and 19 inclusive, ageGroup = 3 if between 20 and 59 inclusive, and ageGroup = 4 if 60 or above.
C++
Hint
Use multiple else if statements to check the age ranges and assign the correct ageGroup value.
4
Print the ageGroup
Write a std::cout statement to print the value of ageGroup.
C++
Hint
Use std::cout << ageGroup << std::endl; to print the value of ageGroup.
Practice
(1/5)
1.
What does an else-if ladder do in C++?
easy
A. Skips all conditions and runs the else block always
B. Runs all conditions regardless of their truth
C. Only runs the last condition in the ladder
D. Checks multiple conditions one by one and runs the first true block
Solution
Step 1: Understand the purpose of else-if ladder
An else-if ladder is used to check multiple conditions in order, one after another.
Step 2: Identify how conditions are evaluated
It runs the code block for the first condition that is true and skips the rest.
Final Answer:
Checks multiple conditions one by one and runs the first true block -> Option D
Quick Check:
Else-if ladder = first true condition runs [OK]
Hint: Remember: else-if stops at first true condition [OK]
Common Mistakes:
Thinking all conditions run
Believing else always runs
Confusing else-if with separate ifs
2.
Which of the following is the correct syntax for an else-if ladder in C++?
int x = 10;
if (x < 5) {
// code A
} _____ (x < 15) {
// code B
} else {
// code C
}
easy
A. else if
B. elseif
C. else_if
D. else if()
Solution
Step 1: Recall correct else-if syntax in C++
The correct keyword is else if with a space, no underscores or parentheses after else.
Step 2: Check each option
Options A, B, and C are invalid syntax in C++.
Final Answer:
else if -> Option A
Quick Check:
Correct syntax = else if [OK]
Hint: Use 'else if' with space, no underscores or parentheses [OK]
Common Mistakes:
Writing 'elseif' as one word
Using underscores like 'else_if'
Adding parentheses after else
3.
What will be the output of this C++ code?
int num = 7;
if (num > 10) {
std::cout << "Greater than 10";
} else if (num > 5) {
std::cout << "Greater than 5";
} else {
std::cout << "5 or less";
}
medium
A. Greater than 5
B. 5 or less
C. Greater than 10
D. No output
Solution
Step 1: Evaluate first condition (num > 10)
7 is not greater than 10, so this block is skipped.
Step 2: Evaluate second condition (num > 5)
7 is greater than 5, so this block runs and prints "Greater than 5".
Final Answer:
Greater than 5 -> Option A
Quick Check:
7 > 5 = true, prints "Greater than 5" [OK]
Hint: Check conditions top to bottom, first true runs [OK]
Common Mistakes:
Choosing first condition output incorrectly
Ignoring else-if order
Assuming else runs always
4.
Find the error in this else-if ladder code snippet: