Decision Making and Loops

The decision-making is a feature that allows you to evaluate a condition before the instructions are executed. The Dart language supports the following types of decision-making statements:

  • If statement
  • If-else statement
  • Switch statement

The below diagram explains it more clearly.

What is Dart Programming

Example

void main() {   
   var num = 12;   
   if (num % 2 = = 0) {   
      print("Number is Even.");   
   } else {   
      print("Number is Odd.");   
   }   
}  

Loops are used to execute a block of code repeatedly until a specified condition becomes true. Dart language supports the following types of loop statements:

  • for
  • for..in
  • while
  • do..while

The below diagram explains it more clearly.

What is Dart Programming

Example

void main() {   
   var name = ["Peter", "Rinky Ponting", "Abhishek"];   
     
   for (var prop in name) {   
      print(prop);   
   }   
}  

Comments

Comments are the lines of non-executable code. They are one of the main aspects of all programming languages. The purpose of this is to provide information about the project, variable, or an operation. There are three types of comments in Dart programming:

  • Make format comments: It is a single line comment (//)
  • Block Comments: It is a multi-line comment (/*…*/)
  • Doc Comments: It is a document comment that used for member and types (///)

Continue and Break

Dart has also used the continue and break keyword in the loop, and elsewhere it required. The continue statement allows you to skip the remaining code inside the loop and immediately jump to the next iteration of the loop. We can understand it from the following example.

Example

void main() {   
  for(int i=1;i<=10;i++){    
    if(i==5){    
      print("Hello");  
      continue; //it will skip the rest statement        
    }    
    print(i);    
  }   
}  

The break statement allows you to terminate or stops the current flow of a program and continues execution after the body of the loop. The following example gives a detailed explanation.

Example

void main() {   
  for(int i=1;i<=10;i++){    
    if(i==5){    
      print("Hello");  
      break;//it will terminate the rest statement        
    }    
    print(i);    
  }   
}  


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *