For and While Loops
Understanding how to repeat actions efficiently is essential in programming. Dart provides two main looping constructs for repeated execution: the for loop and the while loop. Both allow you to process items, perform calculations, or repeat code until a certain condition is met, but their syntax and typical use cases differ. You will often use these loops when working with collections like lists, or when you need to perform an action a specific number of times, or as long as a condition remains true.
sum_list.dart
12345678910void main() { List<int> numbers = [2, 4, 6, 8, 10]; int sum = 0; for (int i = 0; i < numbers.length; i++) { sum += numbers[i]; } print('The sum is $sum'); }
A for loop in Dart is structured with three main parts: an initializer, a condition, and an increment expression. In the summing example, the loop starts with int i = 0 (the initializer), continues as long as i < numbers.length (the condition), and increases i by one after each iteration (i++). This pattern is especially useful when you know ahead of time how many times you want to repeat an action, such as processing every element in a list. Each pass through the loop body adds the current number to the sum, and after the loop finishes, you have the total of all numbers in the list.
count_char.dart
123456789101112131415void main() { String text = "banana"; String target = "a"; int count = 0; int index = 0; while (index < text.length) { if (text[index] == target) { count++; } index++; } print('The letter "$target" appears $count times.'); }
A while loop in Dart checks a condition before each iteration and continues looping as long as that condition is true. In the character counting example, the loop starts with index at 0 and checks if index < text.length before each pass. Inside the loop, it compares the current character to the target and increments count if they match. After each check, it moves to the next character by increasing index. This structure is especially useful when you do not know in advance how many times you need to repeat the action, but you want to continue until a certain condition is no longer true. For loops are ideal for iterating a known number of times, such as traversing a list by index, while while loops are better when the number of iterations depends on dynamic conditions, as in scanning a string for a specific value.
Merci pour vos commentaires !
Demandez à l'IA
Demandez à l'IA
Posez n'importe quelle question ou essayez l'une des questions suggérées pour commencer notre discussion
Can you give examples of for and while loops in Dart?
What are some common mistakes to avoid when using loops in Dart?
How do I choose between a for loop and a while loop in practice?
Génial!
Completion taux amélioré à 10
For and While Loops
Glissez pour afficher le menu
Understanding how to repeat actions efficiently is essential in programming. Dart provides two main looping constructs for repeated execution: the for loop and the while loop. Both allow you to process items, perform calculations, or repeat code until a certain condition is met, but their syntax and typical use cases differ. You will often use these loops when working with collections like lists, or when you need to perform an action a specific number of times, or as long as a condition remains true.
sum_list.dart
12345678910void main() { List<int> numbers = [2, 4, 6, 8, 10]; int sum = 0; for (int i = 0; i < numbers.length; i++) { sum += numbers[i]; } print('The sum is $sum'); }
A for loop in Dart is structured with three main parts: an initializer, a condition, and an increment expression. In the summing example, the loop starts with int i = 0 (the initializer), continues as long as i < numbers.length (the condition), and increases i by one after each iteration (i++). This pattern is especially useful when you know ahead of time how many times you want to repeat an action, such as processing every element in a list. Each pass through the loop body adds the current number to the sum, and after the loop finishes, you have the total of all numbers in the list.
count_char.dart
123456789101112131415void main() { String text = "banana"; String target = "a"; int count = 0; int index = 0; while (index < text.length) { if (text[index] == target) { count++; } index++; } print('The letter "$target" appears $count times.'); }
A while loop in Dart checks a condition before each iteration and continues looping as long as that condition is true. In the character counting example, the loop starts with index at 0 and checks if index < text.length before each pass. Inside the loop, it compares the current character to the target and increments count if they match. After each check, it moves to the next character by increasing index. This structure is especially useful when you do not know in advance how many times you need to repeat the action, but you want to continue until a certain condition is no longer true. For loops are ideal for iterating a known number of times, such as traversing a list by index, while while loops are better when the number of iterations depends on dynamic conditions, as in scanning a string for a specific value.
Merci pour vos commentaires !