Kursinhalt
Introduction to Dart
Introduction to Dart
For-Loop in Dart
A for
loop repeats a particular block of code multiple times. For example, if we want to check each student's grade in a class of 32 students, we loop from 1 to 32.
The for
loop is used to repeat a section of code a known number of times.
Some examples:
- Calculate the average age of the school's students. Although you (the programmer) may need help to learn how many students are in the school, the computer can do it. Dart achieves this by using the
length
property on theList
; - Print the even numbers from 1 to 500;
- To search in a
List
of numbers, such as finding the highest score on a test. Again, the computer knows how many grades are in theList
, so afor
loop is appropriate.
For-Loops Syntax
Let's look at the syntax of the for
loop using the example code below:
The for loop in Dart has three parts:
-
Initialization: variable initialization will control the number of loop iterations. In this case, we initialize the variable with name i and with the value 0;
-
Condition: this is the condition that determines whether to continue iterating the loop. In this case, we check if the value i is less than 5;
-
Increment or Decrement are the operations performed on the counter at the end of each loop iteration.
Each of these instructions in the for loop is separated by a semicolon ;
.
Example
In this example, we use a for
loop to print a message to the console five times. Here's how it works:
main
void main() { for (int day = 1; day <= 7; day++) { print("Day $day: Time to exercise!"); } }
Imagine you're building a simple workout reminder. This code prints a message for each day, reminding you to exercise.
int day = 1
– initializes the variableday
with 1 (representing the first workout day);day <= 7
– keeps the loop running as long asday
is less than or equal to 7 (one full week);day++
– increasesday
by 1 after each reminder;print("Day $day: Time to exercise!")
– prints"Day 1: Time to exercise!"
,"Day 2: Time to exercise!"
, and so on until day 7.
For-In Loops Syntax
Here's how the for-in
loop works in Dart, broken down into a simple structure:
for
– the keyword that starts the loop;(Type variable in collection)
– the loop structure:Type
– the data type of each item in the collection (e.g.,String
,int
);variable
– a temporary variable that holds the current item;in collection
– the collection (list) that the loop iterates over;
{}
– the loop body, which contains the code that runs for each item.
The for
loop allows you to work with lists conveniently.
Example
Let's look at an example:
main
void main() { List<String> fruits = ["apple", "banana", "orange"]; for (String fruit in fruits) { print(fruit); } }
This code defines a list of strings representing different fruits
: "apple"
, "banana"
, and "orange"
. The list is stored in the variable fruits
.
We use a for-in
loop to go through each item in the fruits
list. This loop automatically retrieves each fruit one by one and assigns it to the variable fruit
. Here, fruit
holds the value of the current fruit during each iteration.
Inside the loop, the current fruit
is printed to the console. The loop then moves to the next fruit and repeats the process until all items in the list have been printed.
This approach is useful when working with lists, such as displaying a shopping list or processing user-selected items.
Task
You are given a list of product names. Your task is to complete the condition inside the loop so that only products that contain the letter "o" are printed.
main
void main() { List<String> products = [ "Laptop", "Headphones", "Mouse", "Keyboard", "USB Cable", "Monitor" ]; for (String product in products) { if (___) { // Complete the condition print(product); } } }
- Complete the
if
condition inside the loop to check if the product name contains the letter"o"
. - Ensure that only products containing
"o"
(uppercase or lowercase) are printed. - Expected Output:
Use .contains("o")
to check if the string contains the letter "o"
.
main
void main() { List<String> products = [ "Laptop", "Headphones", "Mouse", "Keyboard", "USB Cable", "Monitor" ]; for (String product in products) { if (product.toLowerCase().contains("o")) { // Check if "o" is in the product name print(product); } } }
Danke für Ihr Feedback!