Dart While Loop

 

Dart While Loop

The while loop is used when the number of execution of a block of code is not known. It will execute as long as the condition is true. It initially checks the given condition then executes the statements that are inside the while loop. The while loop is mostly used to create an infinite loop.

Dart While Loop Flowchart

Dart While Loop

The syntax is given below.

Syntax:

  1. while(condition){  
  2.        //statement(s);  
  3.       // Increment (++) or Decrement (--) Operation;  
  4. }  

Here, if the condition returns the true, then the loop body is executed and condition is evaluated again. If the condition returns false then the loop is terminated and the control transfer to out of the loop.

Let's understand the following example.

Example - 1

  1. void main()   
  2. {  
  3.     int i = 1;  
  4.     while (i <= 5)  
  5.     {  
  6.         print( i);  
  7.         ++i;  
  8.     }  
  9. }  

Output:

1
2
3
4
5

Explanation:

Above example, we initialized the integer variable i with value 1 respectively, in the next statement we have defined while loop, that check the condition that, the value of i is smaller or greater than 5 in each iteration.

If the condition returns true then while loop body is executed and the condition is rechecked. It will be continued until the condition is false.

After that, the value of i is 6 that violated the condition; then, the loop is terminated. It printed the sequence of 1 to 5 on the console.

Infinite While Loop

When the while loop executes an endless time is called infinite while loop. Let's have a look at the infinite loop example.

Example -

  1. void main()   
  2. {  
  3.     int i = 1;  
  4.       
  5.     while (i <= 5)  
  6.     {  
  7.         print( i);  
  8.         --i;  
  9.     }  
  10. }  

We have made only one change in above code. We reduced the value of i for each iteration of while loop. So it will never match with the specified condition and became an infinite loop.

Example - 2

  1. void main()   
  2. {   
  3.     while (true)  
  4.     {  
  5.         print("Welcome to JavaTpoint");  
  6.     }  
  7. }  

It will print the given statement for infinite time. When we declare Boolean true in while loop, then it automatically became an infinite loop.

Logical Operator while loop

Sometimes we need to check the multiple conditions in a while loop. We can do this by using logical operators such as (||, &&, and !). Let's see the following concepts.

  • while (n1<5 && n2>10) - It will execute if both conditions are true.
  • while (n1<5 || n2>10) - It will execute if one of the condition is true.
  • while(!n1 = 10) - It will execute if n1 is not equal to 10.

Consider the following example.

Example -

  1. void main() {  
  2. int n1=1;  
  3. int n2=1;   
  4.    // We are checking multiple condition by using logical operators.  
  5.    while (n1 <= 4 && n2 <= 3)  
  6.    {  
  7.     print("n1 : ${n1}, n2: ${n2}");  
  8.     n1++;  
  9.     n2++;  
  10.    }  
  11. }   

Output:

n1 : 1, n2: 1
n1 : 2, n2: 2
n1 : 3, n2: 3

Explanation:

In the above code, we have assigned two variables n1 and n2 with the value 1 in both. Now we checked multiple conditions in the while loop where n1 is less than or equal to 4 and n2 is less than or equal to 3.

In the first iteration, it checked both values and printed the result. At one point, when the value of n1 and n2 is equal to 4. The n1 satisfied the condition one, but n2 did not meet the second condition, so the loop is terminated and printed the result to the screen

Comments