If Else Conditional Statement In Dart
Code snippet for how to If Else Conditional Statement In Dart with sample and detail explanation
The If/Else conditional statement is an essential part of any programming language, including Dart. It allows the program to make decisions based on certain conditions. In this article, we will delve into understanding how If/Else conditional statements work in Dart.
Code snippet of If Else Conditional Statement In Dart
Below is a simple code snippet that uses the If/Else conditional statement in Dart.
void main() {
var grade = 85;
if (grade >= 90) {
print('A');
} else if (grade >= 80) {
print('B');
} else if (grade >= 70) {
print('C');
} else {
print('D');
}
}
Code Explanation for If Else Conditional Statement In Dart
Let’s break down the code step by step for a better understanding.
-
First, we create a variable called
grade
and assign it a value of85
. -
The
if
statement is used to establish a condition to be met. In this case, the condition isgrade >= 90
. If this condition is met, the program will print ‘A’ and skip the rest of the conditions. -
If the first condition isn’t met, the program will move on to the
else if
statement. Here, the condition is ‘grade >= 80’. If the grade is equal to or higher than 80 but less than 90, the program will print ‘B’. -
The program continues going through the
else if
andelse
statements until it finds a condition that is met. If none of the conditions are met, it will execute the code within theelse
statement. In our case, if the grade is less than 70, the program will print ‘D’.
The If/Else conditional statement in Dart is a powerful tool that allows for complex decision-making processes within your code. By understanding how to properly use it, you can create more dynamic and responsive applications.