Dart Programming

Dart Loops

The Dart programming language is mostly used for designing the client-side of the mobile and web applications. This language is pretty much similar to the traditional C programming language; however, it is a relatively modern programming language. In this guide, we will be discussing the concept of loops in this programming language while sharing a few relevant examples.

Different Types of Loops in the Dart Programming Language

There are five different variants of loops that are used in the Dart programming language. The names of these loops are stated below:

Using the Loops in Dart in Ubuntu 20.04

To demonstrate the usage of the different loops in the Dart programming language, we have designed the following five examples. However, before proceeding with these examples, we need to ensure that the Dart SDK is present in our system. For that, we will check out its version in the following manner:

The output shown in the image above confirms the existence of the Dart SDK on our Ubuntu 20.04 system. Now, we can easily write and execute Dart scripts on our system.

Example # 1: Using the “For” Loop

This example is meant to teach you the usage of the simple “For” loop in Dart. In this example, we will simply be printing a message on the terminal multiple times by using this loop. The Dart script shown in the image below has been written to achieve this functionality:

In this Dart script, we have created a “main()” function inside which we have used a “For” loop that iterates from “i = 0 till 9” and prints a message on the terminal 10 times.

After creating this script, we can run it by using the following command:

$ dart run loops.dart

Here, “loops.dart” is the name of our Dart script file.

The output of our Dart script is shown in the image below. You can see that this script has correctly printed our desired message 10 times on the terminal. You can change the minimum limit and the iterating condition and increment this loop according to your needs.

Example # 2: Using the “For In” Loop

This example is designed to demonstrate the “For In” loop of the Dart programming language. This loop is mainly used with the collections in Dart. To explain its usage, we have designed the following Dart script:

In this modified script, we have created a numbered list with the title “numberList” and assigned the first five odd numbers to it. Then, we have used the “For In” loop that iterates through the length of this numbered list.

The elements of our numbered list (after the execution of this Dart script) are shown in the image below:

Example # 3: Using the “For Each” Loop in Dart

The “For Each” loop of Dart is also used with the collections in Dart. To demonstrate its usage, we have implemented the following Dart script in Ubuntu 20.04:

In this Dart script, we have created and used the same numbered list of the first five odd numbers that we have used in the example above. Then, we have called the “For Each” loop while using the title of this numbered list. We have created a variable “num” within this loop, which will contain the current element of the numbered list for every iteration of the loop. This loop will print all the elements of the numbered list on the terminal.

The output of this Dart script is shown in the image below:

Example # 4: Using the “While” Loop in Dart

For using the “While” loop in the Dart programming language, you need to pre-define the minimum and maximum limit of the loop, i.e., the starting point as well as the total number of iterations of the loop, whereas the increment is given inside the body of the loop. To understand this, you need to go through the following Dart script:

In this Dart script, we have first defined the total number of iterations by creating an “iterations” variable. We have kept the total number of iterations as “5” for this particular example. Moreover, we have kept the starting point of this loop as “1”. Then, we have inserted our “While” loop that iterates from “i” to “iterations”. This loop simply prints a message five times on the terminal. Finally, we have incremented our iterator after every iteration of the loop.

The output of this Dart script is shown in the image below. You can clearly see that our specified message was printed 5 times due to executing this Dart script in Ubuntu 20.04.

Example # 5: Using the “Do While” Loop in Dart

The “Do While” loop is pretty much similar to the “While” loop in Dart. The only dissimilarity is that the iterating condition is defined at the end in the case of the former one. You can understand this by taking a look at the following Dart script:

In this script, we have defined the starting and ending limits of the loop just like we did in the example above. Then, we have used the “do” part of the “Do While” loop, inside which we have printed a message followed by the increment statement. Finally, we have used the “while” part of the loop that states the iterating condition of the loop. This loop will also print the specified message 5 times on the terminal.

The output of this Dart script is displayed in the image beneath:

Conclusion

This article aimed to demonstrate the usage of the loops in the Dart programming language in Ubuntu 20.04. For doing that, we first introduced you to all the different types of loops that exist within this programming language, followed by the specific usage of each of these loops. By going through these examples, you can instantly learn the working of the different loops of the Dart programming language.

About the author

Saeed Raza

Hello geeks! I am here to guide you about your tech-related issues. My expertise revolves around Linux, Databases & Programming. Additionally, I am practicing law in Pakistan. Cheers to all of you.