In this chapter, we will learn about the dart if…else statements and how they help in decision making, with the help of examples.
In programming, decision making is an important aspect. Suppose a friend scored 70 on their test, and now want to determine if he passed or failed. In such cases, decision making statements are useful.
Dart if Statement
The syntax of if statement is:
if(test_condition) {
// statements that are executed if test_condition is true
}
// next statement
The if statement evaluates the test_condition inside the parenthesis().
1. If test_condition results to true, then statements inside of if curly braces gets executed. And the program control moves to the next statement.
2. If test_condition results to false, then statements inside of if curly braces are ignored and the program control goes to the next statement.
Example of if Statement
Example 1:
void main() {
int score = 70;
if(score > 60) {
print("You passed the test.");
}
print("if Statement in Dart");
}
Output
You passed the test.
if Statement in Dart
Here, the score is 70, which is greater than 60. Hence the condition score > 60 is true. Therefore the statements inside the if statement “You passed the test.” is printed.
Example 2:
void main() {
int score = 50;
if(score > 60) {
print("You passed the test.");
}
print("if Statement in Dart");
}
Output
if Statement in Dart
Here the score is 50. The condition score > 60 results in false. The statement inside if “You passed the test” is not executed.
And we get the output as “if Statement in Dart”.
Dart if…else Statements
The syntax of dart if..else statements is:
if(test_condition) {
// statements that are executed if test_condition is true
} else {
// statements that are executed if test_condition is false
}
// next statement
The dart if statements evaluates the test_condition inside the parenthesis().
1. If test_condition results to true, then statements inside of if curly braces are executed. And the program control moves to the next statement.
2.. If test_condition results to false, then statements inside of else curly braces are executed. And the program control goes to the next statement.
Example of Dart if…else Statements
Example1 :
void main() {
int score = 70;
if(score > 60) {
print("You passed the test.");
} else {
print("You failed the test.");
}
print("if…else Statement in Dart");
}
Output
You passed the test.
if…else Statement in Dart
Here the score is 70, which is greater than 60. Hence the condition score > 60 is true. Therefore the statements inside the if statement “You passed the test.” is printed.
Example 2:
void main() {
int score = 50;
if(score > 60) {
print("You passed the test.");
} else {
print("You failed the test.");
}
print("if…else Statement in Dart");
}
Output
You failed the test.
if...else Statement in Dart
In the above example, 50 is not greater than 60. And the condition results in false. Hence the statement inside else “You failed the test .” is executed.
Dart if…else if Statement
The if…else statement helps to execute a block of code among two conditions. The if…else if…else statements helps if there are multiple conditions.
The syntax of the if…else if…else statement is:
if (test_condition1) {
// statements that are executed if test_condition1 is true
} else if (test_condition2){
// statements that are executed if test_condition2 is true
} else {
// statements that are executed if no above condition is true
}
If test_condition1 results in true, then statements inside of if curly braces are executed.
If test_condition1 results in false, then test_condition2 is evaluated.
When the test_condition2 is true, then statements inside of else if curly braces are executed..
When the condition2 is false, then statements inside of else curly braces are executed.
Example 1:
void main() {
int score = 90;
if(score >= 90) {
print("You aced the test.");
} else if (score > 60) {
print("You passed the test.");
} else {
print("You failed the test.");
}
print("if...else...if Statement in Dart");
}
Output
You aced the test.
if...else...if Statement in Dart
Here, the score is 90. The if condition score >= 90 results in true. And we get the output as “You aced the test.”
Example 2:
void main() {
int score = 70;
if(score >= 90) {
print("You aced the test.");
} else if (score > 60) {
print("You passed the test.");
} else {
print("You failed the test.");
}
print("if...else...if Statement in Dart");
}
Output
You passed the test.
if...else...if Statement in Dart
Here, ther score is 70. The first condition of if score >= 90 results in false.
Then the program flow moves to else if condition. The condition score > 60 results in true. Hence “You passed the test.” is printed.
Example 3:
void main() {
int score = 50;
if(score >= 90) {
print("You aced the test.");
} else if (score > 60) {
print("You passed the test.");
} else {
print("You failed the test.");
}
print("if...else...if Statement in Dart");
}
Output
You failed the test.
if...else...if Statement in Dart
Here, the score is 50. The first condition of if score >= 90 results in false.
Then the program flow moves to else if condition. The condition score > 60 results in false as well.
The program flow moves to else statement. Hence “You failed the test.” is printed.
Note: In else if statements, the first condition that results in true is executed. And the rest are ignored.
Nested if…else Statement
We can also use an if…else statement inside of an if…else statement. This is called a nested if…else statement.
Example:
void main() {
int score = 90;
if(score > 60) {
if(score >=90) {
print("You aced the test.");
} else {
print("You passed the test.");
}
}
else {
print("You failed the test.");
}
print("nested if...else Statement in Dart");
}
Output
You aced the test.
nested if...else Statement in Dart
You can also learn more about the comparison operators.