Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

dart
communitycreator

What are loop control statements in Dart?

Maria Elijah

Dart offers two loop control statements:

  1. Break statement
  2. continue statement

Break statement

The break statement is intended to interrupt the loop’s control flow. When a break statement is used in a loop, the program exits the loop.

Syntax:
break;
// Using break statement in a while loop
void main()
{
    int i = 1;
  
    while (i <= 5) {
        print(" $i. You are within the while loop");
        i++;
  
        if (i == 3) {
            break; //breaks out of the loop
        }
    }
    print("Break statement executed. You are out of the while loop");
}
// Using break statement in a for loop

void main()
{
    for (int count= 1; count <= 5; ++count) {
        if (count == 3)
            break;
  
        print(" $count. You are within the for loop");
    }
  
    print("Break statement executed. You are out of the for loop");
}

Continue statement

The continue statement is used to continue a loop. Unlike the break statementstops the flow of control, the continue statement keeps the flow of control going. When a continue statement appears in a loop, it does not end the loop; instead, it jumps to the next iteration.

Syntax:
continue;
// Using continue in a for loop

void main() {
   int count = 0;

   for(int i = 0; i <= 10; i++) { 
      if (i % 2 == 0) { 
         continue; //jumps to the next iteration
      } 
      count++; 
   } 
   print(" The odd numbers between 0 and 10 is: ${count}"); 
}  

RELATED TAGS

dart
communitycreator
RELATED COURSES

View all Courses

Keep Exploring