0
0
C++programming~5 mins

If–else statement in C++ - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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?
ARuns code when the if condition is true
BChecks another condition
CRuns code when the if condition is false
DEnds the program
Which keyword is used to check multiple conditions after an if in C++?
Aelif
Belseif
Celse
Delse if
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
Which of these is a correct if statement syntax in C++?
Aif (condition) { }
Bif condition then { }
Cif condition { }
Dif: (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
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.