📖 Scenario: You are creating a simple program to check if a person is eligible for a special discount based on their age and membership status.
🎯 Goal: Build a program that uses logical operators to decide if a person qualifies for the discount.
📋 What You'll Learn
Create variables for age and membership status
Create a variable for minimum age to qualify
Use logical operators to check if the person is both old enough and a member
Print the eligibility result
💡 Why This Matters
🌍 Real World
Checking eligibility for discounts or offers based on multiple conditions is common in stores, websites, and apps.
💼 Career
Understanding logical operators is essential for writing conditions in programming, which is a key skill for any software developer.
Progress0 / 4 steps
1
Create variables for age and membership status
Create an int variable called age and set it to 25. Create a bool variable called isMember and set it to true.
C++
Hint
Use int for numbers and bool for true/false values.
2
Create a variable for minimum age to qualify
Create an int variable called minAge and set it to 18.
C++
Hint
This variable will hold the minimum age required for the discount.
3
Use logical operators to check eligibility
Create a bool variable called isEligible that is true only if age is greater than or equal to minAgeandisMember is true. Use the logical AND operator &&.
C++
Hint
Use parentheses to group conditions clearly.
4
Print the eligibility result
Use std::cout to print "Eligible for discount: " followed by the value of isEligible. Include #include <iostream> and use std::boolalpha to print true or false instead of 1 or 0.
C++
Hint
Remember to include #include <iostream> and use std::boolalpha to print true or false.
Practice
(1/5)
1. Which logical operator in C++ means "both conditions must be true"?
easy
A. &&
B. ||
C. !
D. &
Solution
Step 1: Understand the meaning of logical AND
The operator that requires both conditions to be true is called logical AND.
Step 2: Identify the C++ symbol for logical AND
In C++, logical AND is represented by &&.
Final Answer:
&& -> Option A
Quick Check:
Both true = true means && [OK]
Hint: && means both true; || means one true [OK]
Common Mistakes:
Confusing & (bitwise AND) with && (logical AND)
Thinking || means both true
Mixing ! with AND or OR
2. Which of the following is the correct syntax to negate a boolean variable flag in C++?
easy
A. !flag = flag;
B. flag = flag!;
C. flag != flag;
D. flag = !flag;
Solution
Step 1: Recall the negation operator syntax
The negation operator ! is placed before the variable to flip its value.
Step 2: Check correct assignment syntax
To update flag to its negation, write flag = !flag; which is valid C++ syntax.
Final Answer:
flag = !flag; -> Option D
Quick Check:
Negate with ! before variable [OK]
Hint: Put ! before variable to negate [OK]
Common Mistakes:
Placing ! after variable (invalid syntax)
Using != which means not equal
Trying to assign to !flag (not a variable)
3. What is the output of this C++ code?
bool a = true; bool b = false; if (a || b) { std::cout << "Yes"; } else { std::cout << "No"; }
medium
A. Yes
B. No
C. true
D. false
Solution
Step 1: Evaluate the logical OR condition
a is true, b is false, so a || b is true because at least one is true.
Step 2: Determine which branch runs
Since the condition is true, the if block runs and prints "Yes".
Final Answer:
Yes -> Option A
Quick Check:
true || false = true [OK]
Hint: || is true if any side is true [OK]
Common Mistakes:
Thinking both must be true for ||
Confusing output with boolean literals
Ignoring that std::cout prints strings
4. Find the error in this C++ code snippet:
bool x = true; bool y = false; if (x & y) { std::cout << "True"; } else { std::cout << "False"; }
medium
A. Incorrect use of std::cout
B. Missing semicolon after bool declarations
C. Using & instead of && in if condition
D. Variables x and y not initialized
Solution
Step 1: Identify the operator used in condition
The code uses & which is bitwise AND, not logical AND.
Step 2: Explain why && is needed
For boolean logic in conditions, && should be used to combine true/false values.
Final Answer:
Using & instead of && in if condition -> Option C
Quick Check:
Logical AND needs && not & [OK]
Hint: Use && for logic, & is bitwise [OK]
Common Mistakes:
Confusing bitwise & with logical &&
Assuming & works like && in conditions
Ignoring operator precedence differences
5. Given int a = 5; and int b = 10;, which condition correctly checks if a is less than 10 AND b is greater than 5?
hard
A. if (a < 10 || b > 5)
B. if (a < 10 && b > 5)
C. if (!(a < 10) && !(b > 5))
D. if (a > 10 && b < 5)
Solution
Step 1: Understand the condition requirements
We want both: a less than 10 AND b greater than 5.
Step 2: Match condition with logical AND
Logical AND (&&) combines both conditions to be true simultaneously.
Final Answer:
if (a < 10 && b > 5) -> Option B
Quick Check:
Both true means && [OK]
Hint: Use && to require both conditions true [OK]
Common Mistakes:
Using || which means either condition true
Negating conditions incorrectly
Swapping comparison operators
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.