Menu
Coddy logo textTech

Iterating Over a List

Part of the Fundamentals section of Coddy's Dart journey — lesson 55 of 94.

The for-in loop provides a simple way to iterate through all elements in a list. It's cleaner than using a traditional for loop with indices.

Create a list of fruits:

List<String> fruits = ['Apple', 'Banana', 'Orange'];

Use a for-in loop to iterate through each element:

for (String fruit in fruits) {
  print(fruit);
}

After executing the above code, the output will be:

Apple
Banana
Orange
challenge icon

Challenge

Beginner

In this challenge, you'll practice using a for-in loop to iterate through a list of fruits and print each one. The for-in loop is a simple way to process each element in a list one by one.

Complete the code below to print each fruit in the fruits list on a separate line using a for-in loop.

Expected output:

Apple
Banana
Orange
Mango
Strawberry

Cheat sheet

The for-in loop provides a simple way to iterate through all elements in a list:

List<String> fruits = ['Apple', 'Banana', 'Orange'];

for (String fruit in fruits) {
  print(fruit);
}

This will output each element on a separate line:

Apple
Banana
Orange

Try it yourself

void main() {
  // List of fruits (already defined for you)
  List<String> fruits = ['Apple', 'Banana', 'Orange', 'Mango', 'Strawberry'];
  
  // TODO: Use a for-in loop to iterate through the fruits list
  // and print each fruit on a separate line
  
  
  // The loop should print each fruit like this:
  // Apple
  // Banana
  // Orange
  // ...
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Fundamentals