Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Break and Continue | Loops
Introduction to Dart
course content

Contenido del Curso

Introduction to Dart

Introduction to Dart

1. First Acquaintance with Dart
2. Variables and Data Types
3. Conditional Statements
4. List and String
5. Loops

bookBreak and Continue

break

In Dart, break is a statement used to exit a loop. The break statement can be used in any loop, including while, for, and do-while loops.

The syntax for the break statement is as follows:

When the break statement is encountered in code, it terminates the execution of the loop in which it is located. This means that any other iterations of the loop will not be executed.

Example

The break statement is often used to exit a loop if a condition meets specific requirements. For example, the following code prints all numbers from 1 to 10, but it breaks the loop if it encounters the number 5:

dart

main

copy
1234567891011
void main() { int counter = 1; while (counter <= 10) { if (counter == 5) { break; } print(counter); counter++; } }

continue

Continue is a statement used to skip the current iteration of a loop. The continue statement can be used in any loop, including while, for, and do-while loops.

The syntax for the continue statement is as follows:

Example

The continue statement is often used to skip certain values or conditions

dart

main

copy
123456789
void main() { List<String> actions = ["go", "jump", "stop", "go", "go", "stop", "jump", "stop", "go", "go", "stop"]; for (String item in actions) { if (item == "stop"){ continue; } print(item); } }

As you can see, the continue statement allows us to control which list items will be output to the console.

Which statement stops the loop?

Which statement stops the loop?

Selecciona la respuesta correcta

¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 5. Capítulo 6
We're sorry to hear that something went wrong. What happened?
some-alt