Introduction
In this blog, we will look into the Dart Decision Statements. We will go through all the decision statements supported by Dart. If you are new to Dart and are yet to set up your environment for the language, you can check out this article.
Decision Making Statements
Decision-making statements allow programmers to choose which statement should be executed under different circumstances. The most typical decision-making statements are as follows:
If Statement
It checks whether or not the given statement is true. The code enclosed within the if statement is executed only if the if statement evaluates to true. Otherwise, it's simply ignored.
Syntax:
if(condition){
//body of the if statement
}
Example:
void main() {
int num = 87;
// here the if statement checks if the number is more than 35
if (num > 35) {
// execute this print statement if the condition evaluates to true
print("Pass");
}
}
Output:
Pass
if-else Statement
If the condition inside the if statement evaluates to true, the piece of code enclosed within it is executed. Otherwise, the code contained within the else statement is run.
Syntax:
if(condition){
//body of the if statement
}
else{
//body of the else statement
}
Example:
void main() {
int num = 32;
// here the if statement checks if number are less than 35 or not
if (num < 35) {
// execute this print statement if the condition evaluates to true
print("Fail");
} else {
// if the condition evaluates to false, execute this print statement.
print("Pass");
}
}
Output:
Fail
else-if ladder
The function of an else if statement is similar to an if statement. It is used to check for other conditions after an if statement. If the condition evaluates to true, the code inside it is executed; otherwise, it checks for other else if conditions.
Syntax:
if(condition_1){
//body of the if statement
}
else if(condition_2){
//body of the else if condition
}
.
.
.
else {
//body of the else statement
}
Example:
void main() {
int num = 67;
// here the if statement checks if the number is less than 35
if (num < 35) {
// execute this print statement if the condition evaluates to true
print("Grade: F");
} else if (num < 45) {
// print Grade D if the number falls between the range [35,45)
print("Grade: D");
} else if (num < 65) {
// print Grade C if the number falls between the range [45,65)
print("Grade: C");
} else if (num < 75) {
// print Grade B if the number falls between the range [65,75)
print("Grade: B");
} else {
//else print the grade as A
print("Grade: A");
}
}
Output:
Grade: B
Switch Case Statement
It works like a long statement containing one if statement and multiple else if statements. The condition inside the switch statement is evaluated, and the value obtained is matched with all the cases. If it matches a case, the code inside that case statement is executed. Otherwise, the code of the default statement is executed.
Syntax:
switch(condition){
case constant_value1:{
//body of the case statement
}
break;
case constant_value2:{
//body of the case statement
}
break;
default: {
// body of the default statement
}
break;
}
Example:
void main() {
String operator = "/";
// switch statement to check for valid arithmetic operators
switch (operator) {
case "+":
{
print("Addition operator");
}
break;
case "-":
{
print("Subtraction Operator");
}
break;
case "*":
{
print("Multiplication Operator");
}
break;
case "/":
{
print("Divide Operator");
}
break;
case "//":
{
print("Division Operator");
}
break;
case "%":
{
print("Modulus Operator");
}
break;
default:
{
print("Not a valid arithmetic operator");
}
break;
}
}
Output:
Divide Operator




