advertisement
Looping Statement in Dart Programming
In Dart, looping statements are used to execute a block of code multiple times, until it matches the given condition. These statements are also called iteration statements. Loop statements are useful to iterate over a collection/list of items or to perform a task multiple times.
Types of Looping Statements
- for loop
- for in loop
- while loop
- do while loop
a. For loop Statement
for loop is used on a block of code when it is required to be executed for a specific number of times. for loop takes a variable as an iterator assigns it with an initial value and iterates through the loop body as long as the test condition is true. The loop iteration starts from an initial value and executes only once. The condition is a test-expression and it is checked after each iteration. for loop will execute until false is returned by the given condition. for loop also contains an expression where the iterator value is increased or decreased.
b. for...in loop Statement
for...in loop is a more advanced form of the for loop. It takes an expression or object as an iterator and iterates through the elements one at a time in sequence. The value of the element is bound to var, which is valid and available for the loop body. Once the loop statements are executed as per the current iteration, the next element is fetched from the iterator, and it loops another time. When there are no more elements in the iterator, for loop ends.
c. while loop Statement
while loop will execute a block of statement until the condition is true.
d. do while loop Statement
do while loop is similar to the while loop. However, the only difference is that it executes the loop statement first and then, checks the given condition for the next iteration. After which it executes the next step only if the condition is true.
advertisement
Conversation
Your input fuels progress! Share your tips or experiences on prioritizing mental wellness at work. Let's inspire change together!
Join the discussion and share your insights now!
Comments 0