While Loop Example Program in Dart


This is a Dart program that demonstrates the use of a while loop. The program starts by importing the dart:io library, which provides classes for reading input from the console. The next line declares a variable i and initializes it to the value 1.

The program then starts a while loop that runs as long as i is less than or equal to 10. Inside the loop, the program prints the value of i on the console using the print() function.

After printing the value of i, the program increments the value of i by 2 using the expression i = i + 2. This means that on each iteration of the loop, i will be incremented by 2. Once i is greater than 10, the loop will terminate, and the program will exit.

Overall, this program demonstrates how to use a while loop in Dart to perform a set of instructions repeatedly as long as a certain condition is true. In this case, the program prints the numbers 1, 3, 5, 7, and 9 on the console by incrementing i by 2 on each iteration of the loop.

Source Code

import 'dart:io';
//While loop
void main() {
   var i=1;
   while(i<=10)
   {
      print(i);
      i=i+2;//i=i+1
   }
}
To download raw file Click Here

Output

1
3
5
7
9