Recall & Review
beginner
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) {<br> // code if condition is true<br>} else {<br> // code if condition is false<br>}
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?
✗ 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++?
✗ 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?
✗ 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++?
✗ Incorrect
The correct syntax requires parentheses around the condition.
Can an if statement exist without an else block?
✗ 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.