0
0
C++programming~5 mins

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

Choose your learning style9 modes available
Recall & Review
beginner
What is the purpose of an if statement in C++?
An if statement lets the program decide to run some code only if a condition is true. It's like choosing a path based on a yes/no question.
Click to reveal answer
beginner
Write the basic syntax of an if statement in C++.
The basic syntax is:<br>
if (condition) {<br>  // code to run if condition is true<br>}
Click to reveal answer
beginner
What happens if the condition in an if statement is false?
If the condition is false, the code inside the if block is skipped and the program continues with the next part.
Click to reveal answer
beginner
How do you add an alternative action if the if condition is false?
You use an else block after the if. It runs when the if condition is false.<br>
if (condition) {<br>  // if true<br>} else {<br>  // if false<br>}
Click to reveal answer
intermediate
Can you check multiple conditions in C++? How?
Yes, by using else if blocks between if and else. It checks conditions one by one until one is true.<br>
if (cond1) {<br>} else if (cond2) {<br>} else {<br>}
Click to reveal answer
What does this code print?<br>
int x = 5;<br>if (x > 3) {<br>  std::cout << "Yes";<br>} else {<br>  std::cout << "No";<br>}
AError
BYes
CNothing
DNo
Which keyword is used to provide an alternative when an if condition is false?
Aelse
Belif
Cotherwise
Dswitch
What will happen if the if condition is false and there is no else block?
AThe code inside <code>if</code> is skipped
BThe program will crash
CThe code inside <code>if</code> runs anyway
DThe program asks for input
How do you check multiple conditions in sequence?
AUsing <code>for</code> loops
BUsing multiple <code>if</code> statements without <code>else</code>
CUsing <code>switch</code> only
DUsing <code>else if</code> blocks
Which of these is a valid if condition in C++?
Aif x > 5 then { }
Bif x > 5 { }
Cif (x > 5) { }
Dif (x > 5) then { }
Explain how an if statement controls the flow of a program.
Think about making choices based on yes/no questions.
You got /4 concepts.
    Describe the difference between if, else if, and else in C++.
    Imagine checking multiple doors until one opens.
    You got /4 concepts.