Course Content
Introduction to Dart
Introduction to Dart
for-Loops
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.
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.
Note
It is important to remember that for
loops can be infinite if the condition never becomes false
. Remember, it is essential to check the condition carefully.
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 i = 1; i <= 5; i = i + 1) { print("Iteration ${i}"); } }
-
int i = 1
- We initialize a variable i with the value 1. This variable will be used as a counter for iterations; -
i <= 5
- We set a condition that specifies the loop should continue as long as i is less than or equal to 5; -
i++
- After each iteration, the variable i is incremented by 1; -
print("Iteration ${i}")
- We print a message to the console, where${i}
represents the value of the iteration counter. In the first iteration, it will be"Iteration 1"
, in the second iteration,"Iteration 2"
, and so on.
for-Loop with List
The for
loop allows you to work with lists conveniently.
Let's look at an example:
main
void main() { List<String> fruits = ["apple", "banana", "orange"]; for (String fruit in fruits) { print(fruit); } }
Let's take a closer look at how this program works:
First, we create a List
containing three strings that represent different fruits
: "apple"
, "banana"
, and "orange"
. This list is stored in the variable fruits
.
We use a for
loop, which is designed to iterate through each item in the fruits
list. Each item in the List
will be retrieved one by one and stored in the variable fruit
. Here, fruit
is a variable that takes the value of each fruit during each iteration.
Within the loop, we print the current fruit
, which is stored in the variable fruit
, to the console using the print function. Afterward, the code moves on to the next fruit
and repeats until all the fruits have been printed to the console.
Thanks for your feedback!