Table of Contents
# If Statement
if(boolean_expression){
// statement(s) will execute if the boolean expression is true.
}
Example
void main() {
var num=15;
if (num>0) {
print("number is positive");
}
}
=> output
number is positive
# If Else Statement
if(boolean_expression){
// statement(s) will execute if the Boolean expression is true.
} else {
// statement(s) will execute if the Boolean expression is false.
}
Example
void main() {
var num = 21;
if (num % 2==0) {
print("Even");
} else {
print("Odd");
}
}
=> output
Odd
# If Else Ladder
if (boolean_expression1) {
//statements if the expression1 evaluates to true
}
else if (boolean_expression2) {
//statements if the expression2 evaluates to true
}
else {
//statements if both expression1 and expression2 result to false
}
Example
void main() {
var num = 20;
if(num > 0) {
print("${num} is positive");
}
else if(num < 0) {
print("${num} is negative");
} else {
print("${num} is neither positive nor negative");
}
}
=> output
20 is positive
# Switch Case Statement
switch(variable_expression) {
case constant_expr1: {
// statements;
}
break;
case constant_expr2: {
//statements;
}
break;
default: {
//statements;
}
break;
}
Example
void main() {
var grade = "A";
switch(grade) {
case "A": { print("Excellent"); }
break;
case "B": { print("Good"); }
break;
case "C": { print("Fair"); }
break;
case "D": { print("Poor"); }
break;
default: { print("Invalid choice"); }
break;
}
}
=> output
Excellent