What is the purpose of an if–else statement in C++?
An if–else statement lets the program choose between two paths: one if a condition is true, and another if it is false.
Click to reveal answer
beginner
Write the basic syntax of an if–else statement in C++.
if (condition) { // code if condition is true } else { // code if condition is false }
Click to reveal answer
beginner
What happens if the condition in an if statement is false and there is no else block?
The program skips the if block and continues with the next code after it.
Click to reveal answer
intermediate
Can you have multiple else if blocks in C++? What is their use?
Yes, multiple else if blocks let you check several conditions one after another, running the code for the first true condition.
Click to reveal answer
beginner
Explain with a real-life example how if–else works.
Imagine deciding what to wear: if it is raining, wear a raincoat; else, wear a t-shirt. The if–else helps choose based on weather.
Click to reveal answer
What does the else block do in an if–else statement?
ARuns code when the if condition is true
BChecks another condition
CRuns code when the if condition is false
DEnds the program
✗ Incorrect
The else block runs only when the if condition is false.
Which keyword is used to check multiple conditions after an if in C++?
Aelif
Belseif
Celse
Delse if
✗ Incorrect
C++ uses 'else if' as two words to check multiple conditions.
What happens if the if condition is true and there is an else block?
ABoth if and else blocks run
BOnly the if block runs
COnly the else block runs
DNeither block runs
✗ Incorrect
Only the if block runs when the condition is true; else block is skipped.
Which of these is a correct if statement syntax in C++?
Aif (condition) { }
Bif condition then { }
Cif condition { }
Dif: (condition) { }
✗ Incorrect
The correct syntax requires parentheses around the condition.
Can an if statement exist without an else block?
AYes, else is optional
BNo, else is required
COnly if the condition is true
DOnly in loops
✗ Incorrect
The else block is optional; you can have just an if statement.
Describe how an if–else statement controls the flow of a program.
Think about how the program chooses what to do next based on a question.
You got /4 concepts.
Write a simple if–else statement in C++ that checks if a number is positive or not.
Use if (number > 0) and else blocks.
You got /4 concepts.
Practice
(1/5)
1. What does an if-else statement do in C++?
easy
A. It defines a function.
B. It repeats a block of code multiple times.
C. It chooses between two paths based on a condition.
D. It declares a variable.
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 C
Quick Check:
If-else = choose path [OK]
Hint: If condition true, run if-block; else run else-block [OK]
Common Mistakes:
Confusing if-else with loops
Thinking if-else defines functions
Mixing variable declaration with control flow
2. Which of the following is the correct syntax for an if-else statement in C++?
easy
A. if (x > 0) { cout << "Positive"; } else { cout << "Non-positive"; }
B. if x > 0 then cout << "Positive" else cout << "Non-positive"
C. if (x > 0) cout << "Positive" else cout << "Non-positive";
D. if x > 0 { cout << "Positive" } else { cout << "Non-positive" }
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 A
Quick Check:
Correct syntax uses parentheses and braces [OK]
Hint: Use parentheses for condition and braces for blocks [OK]
Common Mistakes:
Omitting parentheses around condition
Missing braces for multiple statements
Using 'then' keyword (not in C++)
3. What will be the output of this code?
int x = 5;
if (x < 3) {
cout << "Less";
} else {
cout << "More or equal";
}
medium
A. Less
B. More or equal
C. No output
D. Compilation error
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 B
Quick Check:
Condition false -> else block output [OK]
Hint: Check condition truth to pick if or else output [OK]
Common Mistakes:
Assuming if-block runs when condition is false
Confusing output strings
Ignoring else block
4. Find the error in this code snippet:
int n = 10;
if n > 5 {
cout << "Big";
} else {
cout << "Small";
}
medium
A. Missing parentheses around condition
B. Missing semicolon after cout statements
C. Incorrect else keyword
D. Variable n not declared
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 A
Quick Check:
if condition needs parentheses [OK]
Hint: Always put condition inside parentheses in if [OK]
Common Mistakes:
Omitting parentheses in if condition
Forgetting semicolons (though not the main error here)
Misreading else keyword
5. You want to write a program that prints "Even" if a number is divisible by 2, otherwise prints "Odd". Which code correctly uses if-else to do this?
hard
A. if (num % 2 = 0) { cout << "Even"; } else { cout << "Odd"; }
B. if (num / 2 == 0) { cout << "Even"; } else { cout << "Odd"; }
C. if (num % 2 != 0) { cout << "Even"; } else { cout << "Odd"; }
D. if (num % 2 == 0) { cout << "Even"; } else { cout << "Odd"; }
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.