Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Do-While and For-In Loops | Loops and Iteration Patterns
Dart Control Flow

bookDo-While and For-In Loops

Understanding how to control the flow of your program is essential, and Dart provides different types of loops for different scenarios. The do-while loop is unique because it guarantees that its body will execute at least once, regardless of the condition. This is especially useful when you need to perform an action before checking a condition, such as prompting for user input. On the other hand, the for-in loop offers a concise way to iterate over elements in a collection, such as a list or set, making it ideal for processing each item without worrying about indexes or loop counters.

do_while_input.dart

do_while_input.dart

copy
1234567891011
import 'dart:io'; void main() { int? number; do { stdout.write('Enter a positive number: '); String? input = stdin.readLineSync(); number = int.tryParse(input ?? ''); } while (number == null || number <= 0); print('You entered: $number'); }

The structure of a do-while loop starts with the do keyword, followed by a block of code that always executes at least once. After the block, the while keyword and a condition follow. The loop repeats as long as the condition remains true. This pattern is particularly useful for input validation, as you saw in the previous example: you prompt the user, check the input, and repeat until a valid number is entered. The guarantee that the prompt appears at least once is what sets do-while apart from other loops.

for_in_list.dart

for_in_list.dart

copy
123456
void main() { List<String> fruits = ['apple', 'banana', 'cherry']; for (var fruit in fruits) { print(fruit); } }

The for-in loop shines when you want to process every element in a collection. Its straightforward syntax improves readability and eliminates the need for manual index management. As the previous list printing example shows, you simply declare a variable to represent each element and let Dart handle the iteration. This approach leads to cleaner, more maintainable code, especially when working with lists, sets, or other iterable objects.

question mark

Which scenario best demonstrates when to use a do-while loop instead of a for-in loop in Dart?

Select the correct answer

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 2. Kapittel 2

Spør AI

expand

Spør AI

ChatGPT

Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår

Suggested prompts:

Can you show me an example of a do-while loop in Dart?

What are some common use cases for the for-in loop?

How does a do-while loop differ from a regular while loop?

bookDo-While and For-In Loops

Sveip for å vise menyen

Understanding how to control the flow of your program is essential, and Dart provides different types of loops for different scenarios. The do-while loop is unique because it guarantees that its body will execute at least once, regardless of the condition. This is especially useful when you need to perform an action before checking a condition, such as prompting for user input. On the other hand, the for-in loop offers a concise way to iterate over elements in a collection, such as a list or set, making it ideal for processing each item without worrying about indexes or loop counters.

do_while_input.dart

do_while_input.dart

copy
1234567891011
import 'dart:io'; void main() { int? number; do { stdout.write('Enter a positive number: '); String? input = stdin.readLineSync(); number = int.tryParse(input ?? ''); } while (number == null || number <= 0); print('You entered: $number'); }

The structure of a do-while loop starts with the do keyword, followed by a block of code that always executes at least once. After the block, the while keyword and a condition follow. The loop repeats as long as the condition remains true. This pattern is particularly useful for input validation, as you saw in the previous example: you prompt the user, check the input, and repeat until a valid number is entered. The guarantee that the prompt appears at least once is what sets do-while apart from other loops.

for_in_list.dart

for_in_list.dart

copy
123456
void main() { List<String> fruits = ['apple', 'banana', 'cherry']; for (var fruit in fruits) { print(fruit); } }

The for-in loop shines when you want to process every element in a collection. Its straightforward syntax improves readability and eliminates the need for manual index management. As the previous list printing example shows, you simply declare a variable to represent each element and let Dart handle the iteration. This approach leads to cleaner, more maintainable code, especially when working with lists, sets, or other iterable objects.

question mark

Which scenario best demonstrates when to use a do-while loop instead of a for-in loop in Dart?

Select the correct answer

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 2. Kapittel 2
some-alt