Loops in Dart
Dart Loops
In Programming, loops are used to repeat a block of code until certain conditions are not completed. For, e.g., if you want to print your name 100 times, then rather than typing print(“your name”); 100 times, you can use a loop.
There are different types of loop in Dart. They are:
Note: The primary purpose of all loops is to repeat a block of code.
Print Your Name 10 Times Without Using Loop
Let’s first print the name 10 times without using a loop.
void main() {
print("John Doe");
print("John Doe");
print("John Doe");
print("John Doe");
print("John Doe");
print("John Doe");
print("John Doe");
print("John Doe");
print("John Doe");
print("John Doe");
}
Run Online
Print Your Name 10 Times Using Loop
We will learn for loop in the next section, paste the code and see the output. It will print your name 10 times.
void main() {
for (int i = 0; i < 10; i++) {
print("John Doe");
}
}
Run Online
What if you want to print your name 1000 times? Without using a loop, it will be difficult for you.
Note: Loops are helpful because they reduce errors, save time, and make code more readable.
What After This?
Move to a new section to understand each loop clearly.
Video
Watch our video on loop in Dart.