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

Sharpen your coding skills—try JavaScript challenges on TOOLX now!

advertisement

Looping Statement in Dart Programming

Looping statement in dart


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.

void main()
{
int num = 1;
for(num; num<=10; num++)
 {
print(num);
 }
}



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.

void main()
{
var list = [10, 20, 30, 40, 50];
 for(var i in list)
 {
 print(i);
 }
}



c. while loop Statement

while loop will execute a block of statement until the condition is true.

void main()
{
var a = 1;
var num = 5;
 while(a<num)
 {
 print(a);
 }
}



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.

void main()
{
do{
  print(n);
  n--;
  }
while(n>=0);
}

Dart Looping statement types of Loop loops in Dart for loop for in loop while loop do while loop

advertisement