What if your program could think and choose like you do in everyday decisions?
Why If–else statement in C++? - Purpose & Use Cases
Imagine you are sorting mail by hand. You have to decide if each letter goes to the "urgent" pile or the "normal" pile. Doing this for hundreds of letters is tiring and easy to mess up.
Manually checking each letter one by one is slow and you might put some letters in the wrong pile. It's hard to keep track and easy to forget the rules.
An if-else statement lets the computer quickly check each letter and decide where it belongs. It follows the rules perfectly every time without getting tired.
if (score > 50) { grade = "Pass"; } // else do nothing
if (score > 50) { grade = "Pass"; } else { grade = "Fail"; }
It enables your program to make choices and respond differently depending on the situation.
When you log into a website, the system checks if your password is correct. If it is, you get access; if not, you see an error message. This decision is made using if-else statements.
If-else helps the computer decide between two paths.
It makes programs smart and responsive.
It saves you from doing repetitive, error-prone checks manually.
Practice
if-else statement do in C++?Solution
Step 1: Understand the purpose of if-else
An if-else statement lets the program decide which block of code to run based on a condition.Step 2: Compare with other options
Repeating code is done by loops, functions define reusable code, and variables store data, so these are not correct.Final Answer:
It chooses between two paths based on a condition. -> Option CQuick Check:
If-else = choose path [OK]
- Confusing if-else with loops
- Thinking if-else defines functions
- Mixing variable declaration with control flow
Solution
Step 1: Check C++ if-else syntax rules
C++ requires parentheses around the condition and braces around blocks for multiple statements.Step 2: Analyze each option
if (x > 0) { cout << "Positive"; } else { cout << "Non-positive"; } uses correct parentheses and braces. Options A and B miss parentheses or use incorrect keywords. if (x > 0) cout << "Positive" else cout << "Non-positive"; misses semicolon after else block.Final Answer:
if (x > 0) { cout << "Positive"; } else { cout << "Non-positive"; } -> Option AQuick Check:
Correct syntax uses parentheses and braces [OK]
- Omitting parentheses around condition
- Missing braces for multiple statements
- Using 'then' keyword (not in C++)
int x = 5;
if (x < 3) {
cout << "Less";
} else {
cout << "More or equal";
}Solution
Step 1: Evaluate the condition x < 3
Variable x is 5, so 5 < 3 is false.Step 2: Determine which block runs
Since condition is false, the else block runs, printing "More or equal".Final Answer:
More or equal -> Option BQuick Check:
Condition false -> else block output [OK]
- Assuming if-block runs when condition is false
- Confusing output strings
- Ignoring else block
int n = 10;
if n > 5 {
cout << "Big";
} else {
cout << "Small";
}Solution
Step 1: Check if statement syntax
C++ requires parentheses around the condition in if statements.Step 2: Identify the error
The code uses if n > 5 without parentheses, which is invalid syntax.Final Answer:
Missing parentheses around condition -> Option AQuick Check:
if condition needs parentheses [OK]
- Omitting parentheses in if condition
- Forgetting semicolons (though not the main error here)
- Misreading else keyword
Solution
Step 1: Understand divisibility by 2
A number is divisible by 2 if remainder when divided by 2 is zero, i.e., num % 2 == 0.Step 2: Check each option's condition
if (num % 2 == 0) { cout << "Even"; } else { cout << "Odd"; } correctly uses num % 2 == 0. if (num / 2 == 0) { cout << "Even"; } else { cout << "Odd"; } uses division instead of modulo. if (num % 2 = 0) { cout << "Even"; } else { cout << "Odd"; } uses assignment (=) instead of comparison (==). if (num % 2 != 0) { cout << "Even"; } else { cout << "Odd"; } reverses the logic.Final Answer:
if (num % 2 == 0) { cout << "Even"; } else { cout << "Odd"; } -> Option DQuick Check:
Use modulo == 0 for even check [OK]
- Using assignment (=) instead of comparison (==)
- Using division (/) instead of modulo (%)
- Reversing if and else blocks
Ready to prove it?
Pass the test and this pattern moves unknown
