If-Else Statement
The if-else statement allows you to run one block of code if the condition is true, and another block if the condition is false.
Syntax:
if (condition) {
// code if condition is true
} else {
// code if condition is false
}
Example:
void main() {
int score = 40;
if (score >= 50) {
print('You passed the exam!');
} else {
print('You failed the exam.');
}
}
Explanation:
- If
score >= 50, the first block is executed. - Otherwise, the
elseblock is executed.
Sample Input/Output
Input:
score = 75
Output:
You passed the exam!
Input:
score = 40
Output:
You failed the exam.