Decision Making in Dart

Decision Making in Dart

Decision making in Dart is like making choices in everyday life.

Just as we decide what to do based on certain conditions, Dart allows us to write code that make decisions based on specific criteria.

By using simple conditional statements like if, else if and else , we can write code that adapts dynamically, making our applications more flexible, robust and user-friendly.

  1. If Statement:

    The if statement is used to execute a block of code if a condition is true. Here's how it works:

     if (condition) {
         // Code to execute if the condition is true
     }
    

    2. If-else Statement:

    The if-else statement expands upon the if statement, allowing us to execute one block of code if the condition is true and another block if the condition is false.

     if (condition) {
       // Code to execute if the condition is true
     } else {
       // Code to execute if the condition is false
     }
    

    3. Else if Ladder:

    When we have multiple conditions to evaluate, the else if ladder comes to the rescue. It allows us to check multiple conditions sequentially and execute the corresponding block of code for the first true condition encountered.

     if (condition1) {
       // Code to execute if condition1 is true
     } else if (condition2) {
       // Code to execute if condition2 is true
     } else {
       // Code to execute if none of the conditions are true
     }
    

Switch Case Statement:

The switch statement in Dart provides an alternative way to handle multiple branches of execution based on the value of an expression. It allows you to write more concise code compared to using multiple if-else if statements, especially when you have several conditions to check.

  switch( expression )  
  {  
      case value-1:{  
                //statement(s)  
              Block-1;  
                     }  
             break;  
      case value-2:{            
               //statement(s)  
              Block-2;  
                     }  
              break;  
      case value-N:{            
               //statement(s)  
              Block-N; 
                     }  
               break;  
      default:    {  
              //statement(s);  
       }  
  }