📖 Scenario: Imagine you are creating a simple program that decides if someone can enter a movie theater based on their age.
🎯 Goal: You will build a program that uses conditional logic to check if a person is old enough to watch a movie.
📋 What You'll Learn
Create an integer variable called age with the value 18
Create an integer variable called minimum_age with the value 13
Use an if statement to check if age is greater than or equal to minimum_age
Print "Access granted" if the condition is true
Print "Access denied" if the condition is false
💡 Why This Matters
🌍 Real World
Conditional logic is used in many real-life programs like checking if someone can enter a place, if a password is correct, or if a number is positive or negative.
💼 Career
Understanding conditional logic is essential for all programming jobs because it allows software to make decisions and respond differently to different situations.
Progress0 / 4 steps
1
Create the age variable
Create an integer variable called age and set it to 18.
C++
Hint
Use int to create a whole number variable.
2
Set the minimum age
Create an integer variable called minimum_age and set it to 13.
C++
Hint
This variable will hold the minimum age allowed to watch the movie.
3
Add the conditional check
Use an if statement to check if age is greater than or equal to minimum_age.
C++
Hint
The condition inside if uses >= to compare the two values.
4
Print the result
Inside the if block, print "Access granted". Add an else block that prints "Access denied".
C++
Hint
Use std::cout to print text to the screen.
Practice
(1/5)
1. Why do we use conditional logic like if statements in C++ programs?
easy
A. To store multiple values in a list
B. To repeat the same code many times
C. To make decisions and run different code based on conditions
D. To define new data types
Solution
Step 1: Understand the purpose of conditional logic
Conditional logic allows a program to choose between different actions depending on whether a condition is true or false.
Step 2: Compare with other options
Repeating code is done with loops, storing values uses arrays or vectors, and defining data types uses classes or structs, not conditional logic.
Final Answer:
To make decisions and run different code based on conditions -> Option C
Quick Check:
Conditional logic = decision making [OK]
Hint: Conditional logic = choosing what to do next [OK]
Common Mistakes:
Confusing conditional logic with loops
Thinking conditional logic stores data
Mixing conditional logic with data type definitions
2. Which of the following is the correct syntax for an if statement in C++?
easy
A. if condition { /* code */ }
B. if (condition) { /* code */ }
C. if condition then { /* code */ }
D. if: (condition) { /* code */ }
Solution
Step 1: Recall C++ if statement syntax
In C++, the condition must be inside parentheses and the code block inside curly braces: if (condition) { /* code */ }
Step 2: Check each option
if condition { /* code */ } misses parentheses, C uses 'then' which is not C++ syntax, D uses a colon which is invalid.
Final Answer:
if (condition) { /* code */ } -> Option B
Quick Check:
Correct if syntax = if (condition) { } [OK]
Hint: If condition always in parentheses in C++ [OK]
Common Mistakes:
Omitting parentheses around condition
Using 'then' keyword incorrectly
Adding colons after if
3. What will be the output of this C++ code?
int x = 10;
if (x > 5) {
std::cout << "Big";
} else {
std::cout << "Small";
}
medium
A. Small
B. 10
C. No output
D. Big
Solution
Step 1: Evaluate the condition x > 5
Since x is 10, and 10 > 5 is true, the if block runs.
Step 2: Determine which output is printed
The code prints "Big" because the if condition is true, so the else block is skipped.
Final Answer:
Big -> Option D
Quick Check:
10 > 5 is true, prints "Big" [OK]
Hint: Check if condition true or false to pick output [OK]
Common Mistakes:
Confusing which block runs when condition is true
Expecting variable value printed instead of string
Ignoring else block
4. Find the error in this C++ code snippet:
int num = 7;
if num > 5 {
std::cout << "Yes";
} else {
std::cout << "No";
}
medium
A. Missing parentheses around condition in if statement
B. Missing semicolon after if statement
C. Incorrect use of else keyword
D. Variable num is not declared
Solution
Step 1: Check if statement syntax
The condition in an if statement must be inside parentheses in C++. Here, it's missing.
Step 2: Verify other parts
Semicolon after if is not needed, else keyword is correct, and num is declared properly.
Final Answer:
Missing parentheses around condition in if statement -> Option A
Quick Check:
If condition needs parentheses [OK]
Hint: If condition always inside ( ) in C++ [OK]
Common Mistakes:
Forgetting parentheses around condition
Adding unnecessary semicolon after if
Misunderstanding else syntax
5. You want to write a program that prints "Adult" if age is 18 or more, and "Minor" otherwise. Which code correctly uses conditional logic to do this?
hard
A. if (age >= 18) { std::cout << "Adult"; } else { std::cout << "Minor"; }
B. if age >= 18 then std::cout << "Adult"; else std::cout << "Minor";
C. if (age > 18) std::cout << "Adult"; else std::cout << "Minor";
D. if (age = 18) { std::cout << "Adult"; } else { std::cout << "Minor"; }
Solution
Step 1: Check condition correctness
The condition should be age >= 18 to include 18 as adult. if (age >= 18) { std::cout << "Adult"; } else { std::cout << "Minor"; } uses this correctly.
Step 2: Verify syntax and logic
if age >= 18 then std::cout << "Adult"; else std::cout << "Minor"; uses 'then' which is invalid in C++. if (age > 18) std::cout << "Adult"; else std::cout << "Minor"; uses > 18, excluding 18 itself. if (age = 18) { std::cout << "Adult"; } else std::cout << "Minor"; } uses assignment (=) instead of comparison (==).
Final Answer:
if (age >= 18) { std::cout << "Adult"; } else { std::cout << "Minor"; } -> Option A
Quick Check:
Use >= and correct syntax for if-else [OK]
Hint: Use >= for 18 or more, and proper if-else syntax [OK]
Common Mistakes:
Using assignment (=) instead of comparison (==)
Using > instead of >= to include 18
Adding 'then' keyword incorrectly
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.