Continue statement in Dart Programming

Dart ProgrammingServer Side ProgrammingProgramming

The continue statement is used when we want to skip over the current iteration of any loop. When the compiler sees a continue statement then the rest of the statements after the continue are skipped and the control is transferred back to the first statement in the loop for the next iteration.

It is used in almost every programming language and we normally encounter continue statements inside the conditional blocks of code.

Syntax

continue;

Example

Let's consider an example where we make use of a continue statement inside the while loop.

Consider the example shown below −

 Live Demo

void main(){
   var num = 10;
   while(num >= 3){
      num--;
      if(num == 6){
         print("Number became 6, so skipping.");
         continue;
      }
      print("The num is: ${num}");
   }
}

In the above code, we have a variable named num and we are using a while loop to iterate until the number is greater than or equal to 3. Then, we maintain a conditional check using the if statement that if we encounter a condition where the num becomes equal to 6, we continue.

Output

The num is: 9
The num is: 8
The num is: 7
Number became 6, so skipping.
The num is: 5
The num is: 4
The num is: 3
The num is: 2

Example

Let's consider another example where we make use of the continue statement.

Consider the example shown below −

 Live Demo

void main(){
   var name = "apple";
   var fruits = ["mango","banana","litchi","apple","kiwi"];
   for(var fruit in fruits){
      if(fruit == name){
         print("Don't need an apple!");
         continue;
      }
      print("current fruit : ${fruit}");
   }
}

Output

current fruit : mango
current fruit : banana
current fruit : litchi
Don't need an apple!
current fruit : kiwi
raja
Published on 21-May-2021 12:41:35
Advertisements