Control Flow Statements are used to control the flow of execution of the program. Control Flow Statements are used to alter the flow of the program based on certain conditions.

Types of Control Flow Statements

There are three types of Control Flow Statements in Dart:

  1. Decision Making Statements
  2. Looping Statements
  3. Jump Statements

Familiar with the topic? Play Quiz

Decision Making Statements

Decision Making Statements are used to make decisions based on certain conditions. There are three types of Decision Making Statements in Dart:

  1. if Statement
  2. if-else Statement
  3. if-else-if Ladder

if Statement

if Statement is used to execute a block of code if a certain condition is true. The syntax of if Statement is:

  if(condition){
    //statements to be executed if the condition is true
}
  

Example

  void main() {
   
 bool isAdmin = true; 
  
  if(isAdmin) { 
  
  print("Access Granted"); 
    
    }
}
  

Output

  Access Granted
  

code Try Yourself

if-else Statement

if-else Statement is used to execute a block of code if a certain condition is true and another block of code if the condition is false.

The program flow of if-else statements can be demostrated with the following chart.

graph LR;
A[Start] --> B{Check Condition};
B --> |True| C{True Branch};
B --> |False| D{False Branch};
C --> E[End];
D --> E;

The syntax of if-else Statement is:

  if(condition){
    //statements to be executed if the condition is true
}
else{
    //statements to be executed if the condition is false
}
  

Example

  void main() {
   
 bool isAdmin = false; 
  
  if(isAdmin) { 
  
  print("Access Granted"); 
      
    }
  else { 
  
  print("Access Denied"); 
  }
}
  

Output

  Access Denied
  

code Try Yourself

if-else-if Ladder

if-else-if Ladder is used to execute a block of code if a certain condition is true and another block of code if the condition is false. There can be multiple else-if blocks in an if-else-if Ladder.

Let’s understand it with the flow chart diagram:

graph TD;
A[Start] --> B{Check Condition 1};
B --> |True| C{True Branch 1};
B --> |False| D{Check Condition 2};
D --> |True| E{True Branch 2};
D --> |False| F{Else Branch};
C --> G[End];
E --> G;
F --> G;

The syntax of if-else-if Ladder is:

  if(condition1){
    //statements to be executed if the condition1 is true
}
else if(condition2){
    //statements to be executed if the condition2 is true
}
else if(condition3){
    //statements to be executed if the condition3 is true
}
else{
    //statements to be executed if all the conditions are false
}
  

Example

  void main() {
  String name = "Ben";

  if (name == "Ben") {
    print("Hi, $name. How are you? ");
  } else if (name == "Jane") {
    print("Hey $name. We missed you");
  } else {
    print("Hey $name. We didn't check on you");
  }
}
  

Output

  Hi, Ben. How are you?
  

code Try Yourself

switch case in Dart

In some cases, using the if-else-if ladder will be hectic and makes the code longer resulting into difficultly to understand, in such cases we use switch case statements.

The switch case statement is used when we have multiple conditions and we need to perform different action based on the condition.

Flowchart diagram of switch case statements

graph TD;
A[Start] --> B{Input Value};
B --> |Case 1| C{Case 1 Actions};
B --> |Case 2| D{Case 2 Actions};
B --> |Case 3| E{Case 3 Actions};
B --> |Default| F{Default Case Actions};
C --> G[End];
D --> G;
E --> G;
F --> G;

Every cases in switch cases must have either return or break keywords which prevents the execution of left cases. If no cases are matched the default case gets executed.

Syntax

  
switch (expression) {
  case value1:
    // Statements to execute when the
    // expression value1 is equal to the
    // value of the switch expression.
    break;

  case value2:
    // Statements to execute when the
    // expression value2 is equal to the
    // value of the switch expression.
    break;

  case valueN:
    // Statements to execute when the
    // expression valueN is equal to the
    // value of the switch expression.
    break;

  default:
    // Statements to execute when none of
    // the values match the value of the
    // switch expression.
    break;
}
  

Example

  
void main() {
  var grade = 'A';

  switch (grade) {
    case 'A':
      print('Excellent grade of A');
      break;
    case 'B':
      print('Very good!');
      break;
    case 'C':
      print('Good enough. But work hard');
      break;
    case 'F':
      print('You have failed');
      break;
    default:
      print('Invalid Grade');
  }
}
  

Output

  
Excellent grade of A
  

code Try Yourself

Looping Statements

Looping Statements are used to execute a block of code repeatedly. There are three types of Looping Statements in Dart:

  1. for Loop
  2. while Loop
  3. do-while Loop

for Loop in Dart

For Loop is used to execute a block of code repeatedly for a fixed number of times until the given condition is matched.The syntax of for Loop is:

  
for(initialization; condition; updation){
    //statements to be executed repeatedly
}
  

We have discussed about for loop in more details in For Loop Section

while Loop in Dart

while Loop is used to execute a block of code repeatedly for an unknown number of times. The syntax of while Loop is:

  
while(condition){
    //statements to be executed repeatedly
}
  

We have discussed about while loop in more details in While Loop Section

do-while Loop in Dart

do-while Loop is used to execute a block of code repeatedly for an unknown number of times. In this kind of loop, the loop gets executed at least one even if the first condition itself is false. The syntax of do-while Loop is:

  
do{
    //statements to be executed repeatedly
}while(condition);
  

We have discussed about do-while loop in more details in Do while Loop Section

Jump Statements

Jump Statements are used to alter the flow of the program. There are three types of Jump Statements in Dart:

  1. break Statement, discussed here
  2. continue Statement, discussed here
  3. return Statement

break Statement

break Statement is used to terminate the loop or switch statement. The syntax of break Statement is:

  break;
  

continue Statement

continue Statement is used to skip the current iteration of the loop. The syntax of continue Statement is:

  continue;
  

return Statement

return Statement is used to return a value from a function. The syntax of return Statement is:

  return value;
  

Frequently Asked Questions ( FAQs )

What are Control Flow Statements in Dart? Control Flow Statements are used to control the flow of execution of the program. They alter the flow of the program based on certain conditions.
What are the main types of Control Flow Statements in Dart? There are three main types of Control Flow Statements in Dart: Decision Making Statements, Looping Statements, and Jump Statements.
What are Decision Making Statements and how are they used in Dart? Decision Making Statements in Dart are used to make decisions based on certain conditions. The three types are: if Statement, if-else Statement, and if-else-if Ladder.
Explain the syntax and usage of the ‘if’ statement in Dart.

The ‘if’ statement in Dart is used to execute a block of code if a certain condition is true. The syntax is:

 if (condition) {
   /* code */ 
   }
How does the ‘if-else’ statement work in Dart?

The ‘if-else’ statement in Dart is used to execute one block of code if a certain condition is true and another block if the condition is false. The syntax is:

if (condition) {
   /* code */ 
   } else { 
    /* code */ 
    }
What is an ‘if-else-if Ladder’ and how is it used in Dart?

An ‘if-else-if Ladder’ in Dart is used to execute a block of code if a certain condition is true and another block if the condition is false. It can have multiple else-if blocks. The syntax is:

if (condition1) {
   /* code */ 
   } else if (condition2) {
     /* code */ 
     } else { 
      /* code */ 
      } 
What are Looping Statements in Dart? Looping Statements in Dart are used to execute a block of code repeatedly. The three types are: for Loop, while Loop, and do-while Loop.
Explain the syntax and usage of the ‘for’ loop in Dart.

The ‘for’ loop in Dart is used to execute a block of code repeatedly for a fixed number of times. The syntax is:

for (initialization; condition; updation) {
   /* code */ 
   }
How does the ‘while’ loop work in Dart?

The while loop in Dart is used to execute a block of code repeatedly for an unknown number of times. The syntax is:

while (condition) {
   /* code */ 
   } 
Explain the syntax and usage of the ‘do-while’ loop in Dart.

The do-while loop in Dart is used to execute a block of code repeatedly for an unknown number of times. The syntax is:

do { 
  /* code */ 
  } while (condition);
What are Jump Statements in Dart? Jump Statements in Dart are used to alter the flow of the program. The three types are: break Statement, continue Statement, and return Statement.
How does the ‘break’ statement work in Dart? The break statement in Dart is used to terminate the loop or switch statement. The syntax is: break;.
Explain the usage of the ‘continue’ statement in Dart. The continue statement in Dart is used to skip the current iteration of the loop. The syntax is: continue;.
What is the ‘return’ statement and how is it used in Dart? The return statement in Dart is used to return a value from a function. The syntax is: return value;.

Quiz

You can test your understanding with the questions we prepared, it will take less than 2 minutes.