While loop in Arduino

While loop in Arduino

  • The while loop() is a conditional loop that keeps running code inside parentheses until the provided condition is false.
  • Until the tested condition is modified or forced to cease, the while loop will never escape. 
  • Sensor testing, calibration (calibrating the sensor input), variable increment, and other uses of a while loop in Arduino are popular.

Syntax:

while (condition)  

{  

// set of statements  

}

Flowchart:

While loop in arduino
While loop in Arduino

Example 1:
int a=0;
void setup()  
{  
  Serial.begin(9600);  
while( a < 5)  
{  
  Serial.println("Welcome to Arduino");  
  a = a + 1;  
}  
}  
  void loop()  
  {  
  }  

Expected Output:
While loop in arduino
While in arduino

Example 2:

int a = 0;  
void setup()  
{  
  Serial.begin(9600);  
while( a < 5)  
{  
  Serial.println("Welcome to Arduino");  
  a = a + 1;  
}  
  Serial.println("DONE");  
  Serial.println("Welcome to the code outside the loop");  
}  
  void loop()  
  {  
  }  

Expected Output:

Post a Comment

Previous Post Next Post