Concept Flow - If statement
Start
Evaluate condition
Execute if-block
Continue
Skip if-block
Continue
The program checks a condition. If true, it runs the code inside the if-block; if false, it skips it and continues.
#include <iostream> using namespace std; int main() { int x = 5; if (x > 3) { cout << "x is greater than 3" << endl; } cout << "Done" << endl; return 0; }
| Step | Condition (x > 3) | Result | Branch Taken | Output |
|---|---|---|---|---|
| 1 | 5 > 3 | True | Execute if-block | x is greater than 3 |
| 2 | N/A | N/A | Continue after if | Done |
| Variable | Start | After Step 1 | After Step 2 |
|---|---|---|---|
| x | 5 | 5 | 5 |
If statement syntax:
if (condition) {
// code runs if condition true
}
Checks condition once.
Runs block only if true.
Skips block if false.
Then continues program.if statement do in C++?if statementif statement checks a condition and runs code only when that condition is true.return or exit.if statement in C++?if syntaxint x = 10;
if (x > 5) {
cout << "Hello";
} else {
cout << "Bye";
}x > 5x is 10, which is greater than 5, the condition is true.if block runs, printing "Hello".int a = 3;
if a > 0 {
cout << "Positive";
}if statement syntaxif statement must be inside parentheses.if a > 0 { without parentheses, which is invalid syntax.if statement to do this?int num = 7; // Your code here
num % 2 == 0.num % 2 == 0. if (num / 2 == 0) { cout << "Even"; } else { cout << "Odd"; } uses division instead of modulo, which is wrong. 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.Pass the test and this pattern moves unknown