Dart Programming - while Loop


Advertisements

The while loop executes the instructions each time the condition specified evaluates to true. In other words, the loop evaluates the condition before the block of code is executed.

The following illustration shows the flowchart of the while loop −

While Loop

Following is the syntax for the while loop.

while (expression) {
   Statement(s) to be executed if expression is true  
}

Example

void main() { 
   var num = 5; 
   var factorial = 1; 
   
   while(num >=1) { 
      factorial = factorial * num; 
      num--; 
   } 
   print("The factorial  is ${factorial}"); 
}  

The above code uses a while loop to calculate the factorial of the value in the variable num.

The following output is displayed on successful execution of the code.

The factorial is 120 
dart_programming_loops.htm

Useful Video Courses


Video

Dart tutorial for Beginners

44 Lectures 4.5 hours

Sriyank Siddhartha

Video

Flutter Tutorial for Beginners with Dart

34 Lectures 4 hours

Sriyank Siddhartha

Video

Dart Masterclass Programming: iOS/Android Bible

69 Lectures 4 hours

Frahaan Hussain

Video

Flutter & Dart Development For Building iOS and Android Apps

117 Lectures 10 hours

Frahaan Hussain

Video

COMPLETE Google DART Programming- BOOTCAMP

22 Lectures 1.5 hours

Pranjal Srivastava

Video

AWS Mobile with Google Dart

34 Lectures 3 hours

Pranjal Srivastava

Advertisements