Break Statement Example Program in Dart


This program will print the numbers from 0 to 4 using a for loop. The loop starts by initializing a counter variable i to 0. Then, it checks if the value of i is less than 10. If the condition is true, the loop executes its block of code which includes an if statement.

The if statement checks if the value of i is equal to 5. If the condition is true, the break statement is executed, which terminates the loop immediately and jumps to the statement immediately following the loop.

Since the value of i is never equal to 5 except for the fifth iteration, the loop will execute the print statement for the values of i from 0 to 4. After i becomes 5, the loop terminates, and the program ends.

Source Code

void main()
{
  for(var i=0;i<10;i++)
  {
    if(i==5)
    break;
    print(i);
  }
}
To download raw file Click Here

Output

0
1
2
3
4