Conversation


Your input fuels progress! Share your tips or experiences on prioritizing mental wellness at work. Let's inspire change together!

Join the discussion and share your insights now!

Comments 0


Decision-making statement in Dart Programming

selection statement in dart


Decision-making statements help to identify which statement/block of statements should be executed to support an expression at runtime. The choice-making statements are called conditional statements or Selection statements. In the Dart program, Single or multiple expressions (or conditions) can exist, which evaluate to boolean true and false. These results of an expression/condition help to decide on the block of the statement(s) to be executed if the given condition is true or false.


Types of Decision-making Statements

Different types of decision-making statements are as follows:

  • if
  • if-else
  • if-else-if
  • switch case

a. if Statement:

if statement allows a block of code to be executed only when a specified condition is true. The condition evaluates to either boolean value true or false and the decision is made based on this outcome.

Example:

void main(){
int num=5;
if(num>0){
    print("number is positive");
  }
}

b. if-else Statement:

if-block is executed when the given condition is true. If the given condition is false, the else block is executed. An else block cannot exist independently and is always associated with an if-block.

Example:

void main(){
int a=15;
if(a<30){
   print("15 is less than 30");
}else{ 
   print("30 is greater than 15");
 }
}

c. if-else-if Statement:

In Dart, the if-else-if statement provides the facility to check multiple conditions within expressions and execute the different statements. It is used when a decision has to be made from more than two possibilities.

Example:

void main(){
int b=10;
if(b<9){
    print("condition 1 is true");
}else if(b>=10){
    print("condition 2 is true");
}
else{
    print("All the conditions are false");
 }
}

d. switch case statement:

In Dart, a switch case statement is used to avoid a long chain of if-else statements. It is a simplified form of a nested if-else statement. The value of a given variable is compared against multiple cases and if a match is found, it will execute a block of statements associated with that particular case.

Example:

void main(){
var grade='B';
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;
 }
}

Upon matching the given declared variable, the statement inside the matched case will be printed, or if none of the cases match, it will execute the default statement. A break keyword also has to be used to break a line if the condition is true and the developer wishes to terminate subsequent processing within the switch block. Without the break keyword, the program continues to the next case-labeled statement.


Dart if-else statement decision making statement if statement if-else-if statement
Blogs