Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
while-Loops | Loops
Introduction to Dart

while-Loopswhile-Loops

A while loop is a programming construct that allows you to execute a specific block of code as long as a certain condition is true.

The syntax for a while loop in Dart is as follows:

  • First, before entering the loop, the condition (which is enclosed in parentheses) is checked. If this condition is true, then the code inside the loop is executed;
  • After executing the code inside the loop, the condition is checked again. If the condition is still true, the loop is executed again. This process continues until the condition becomes false;
  • Once the condition becomes false, the execution of the loop stops, and the program moves on to the code after the loop.

Note

It is important to remember that while loops can be infinite if the condition never becomes false. Remember, it is essential to check the condition carefully.

Example

dart

main.dart

The while loop will repeat as long as the value of the variable counter is less than 5. The loop will repeat five times since the variable counter is initialized to 0. Each time the loop repeats, the value of the variable counter will be incremented by 1.

question-icon

What does the syntax of a while loop look like?

Click or drag`n`drop items and fill in the blanks

Everything was clear?

Section 5. Chapter 2
course content

Course Content

Introduction to Dart

while-Loopswhile-Loops

A while loop is a programming construct that allows you to execute a specific block of code as long as a certain condition is true.

The syntax for a while loop in Dart is as follows:

  • First, before entering the loop, the condition (which is enclosed in parentheses) is checked. If this condition is true, then the code inside the loop is executed;
  • After executing the code inside the loop, the condition is checked again. If the condition is still true, the loop is executed again. This process continues until the condition becomes false;
  • Once the condition becomes false, the execution of the loop stops, and the program moves on to the code after the loop.

Note

It is important to remember that while loops can be infinite if the condition never becomes false. Remember, it is essential to check the condition carefully.

Example

dart

main.dart

The while loop will repeat as long as the value of the variable counter is less than 5. The loop will repeat five times since the variable counter is initialized to 0. Each time the loop repeats, the value of the variable counter will be incremented by 1.

question-icon

What does the syntax of a while loop look like?

Click or drag`n`drop items and fill in the blanks

Everything was clear?

Section 5. Chapter 2
some-alt