
For Loop in Dart
29 March, 2023
3
3
0
Contributors
For Loop
In the Dart programming language, the for
loop allows a programmer to repeat a block of code a specific number of times. It is a useful control flow statement for executing a block of code repeatedly until a certain condition is met.
Syntax
The syntax for a for
loop in Dart is as follows:
for (initialization; condition; increment) {
// code to be executed
}
The initialization
statement is executed once before the loop starts. The condition
is checked before each iteration of the loop, and the loop will continue as long as the condition is true. The increment
statement is executed after each iteration of the loop.
Example
Here is an example of a for
loop that counts from 1 to 10:
for (int i = 1; i <= 10; i++) {
print(i);
}
Here is an example of a for
loop that counts down from 100 to 1:
for (int i = 100; I >= 1; i--) {
print(i);
}
This code would output the numbers 1 through 10 to the console.
for
loop with List
The for
loop can also be used to iterate over the elements in a List
. Here is an example of a for
loop that prints out the elements in a List
:
List<String> fruits = ["apple", "banana", "orange"];
for (int i = 0; i < fruits.length; i++) {
print(fruits[i]);
}
This code would output "apple", "banana", and "orange" to the console.
for
loop with Map
The for
loop can also be used to iterate over the key-value pairs in a Map
. Here is an example of a for
loop that prints out the key-value pairs in a Map
:
Map<String, int> prices = {"apple": 0.99, "banana": 0.79, "orange": 0.59};
for (String key in prices.keys) {
print("The price of a $key is ${prices[key]}");
}
This code would output "The price of a apple is 0.99", "The price of a banana is 0.79", and "The price of a orange is 0.59" to the console.
for in loop
In Dart, the for-in-loop
is used to iterate over the elements of an iterable object, such as a list or a set. Here is an example of how to use a for-in-loop
void main() {
List<String> names = ['Alice', 'Bob', 'Charlie', 'Dave'];
for (String name in names) {
print(name);
}
}
In this example, we define a List
ofString
s called names
. We then use a for-in
loop to iterate over the elements of the list. The loop variable name
is declared as aString
, and it takes on the value of each element in names
in turn.
The loop body simply prints the value of name
to the console using the print()
function. This loop will print the names 'Alice', 'Bob', 'Charlie', and 'Dave' to the console, one per line.
You can use the for-in
loop with any iterable object in Dart, such as lists, sets, maps, and strings. Note that when using a for-in
loop with a Map
, the loop variable takes on the keys of the map by default. If you want to iterate over the values or key-value pairs of the map, you can use the values
or entries
property, respectively.
Conclusion
The for
loop is a useful control flow statement in the Dart programming language. It allows a programmer to repeat a block of code a specific number of times, or iterate over the elements in a List
or Map
. It is an efficient way to perform a task multiple times in a program.