Arduino for Loop
The statements inside the curly brackets under for loop are executed repeatedly
according to the specified condition. An increment counter in the for loop is used to
increment or decrement the loop repetitions.
The for statement is commonly used for repetitive task or operation or to operate on
the group of data/pins in combination with arrays.
The syntax is:
1. for (initialization; condition; increment)
2. {
3. \\ statements
4. }
where,
o initialization: It is defined as the initialization of the variable.
o condition: The condition is tested on every execution. If the condition is true, it
will execute the given task. The loop ends only when the condition
becomes false.
o increment: It includes the increment operator, such as i + +, i - - , i + 1, etc. It is
incremented each time until the condition remains true.
For example,
1. for ( i = 0 ; i < 5 ; i + +)
The above statement will execute the loop for five times. The values of i will be from 0 to
4.
If the statement is:
1. for ( i = 0 ; i < = 5 ; i + +)
The above statement will execute the loop six times. The values of i will be from 0 to 5.
Note: If we do not want to execute the for loop again and again. Then, we can insert the for
loop in the void setup( ) function.
Example 1:
To print a message 'Arduino' 15 times.
To print a message 15 times or more is quite complicated using [Link] ( ), as the
code will become too lengthy.
To overcome this, programmers prefer to use for loop to execute a task multiple times,
while using a single statement.
Let's consider the below code.
1. int i;
2. void setup ( )
3. {
4. [Link](9600);
5. for ( i = 0 ; i < 15 ; i ++ )
6. {
7. [Link]( "Arduino");
8. }
9. }
10. void loop ( ) {
11. }
Output:
Example 2:
To use a multiplication increment
The multiplication increment in the for loop will generate a logarithmic progression.
Consider the below code:
1. int x;
2. void setup ( )
3. {
4. [Link](9600);
5. for (x = 2; x < 100; x = x * 2)
6. {
7. [Link](x);
8. }
9. }
10. void loop ( ) {
11. }
Output:
We can also declare the int data type directly in the for loop.
For example,
1. for (int x = 2; x < 100; x = x * 2)
Example 3: To fade an LED
Here, fade and LED means that LED will faint slowly.
Consider the below code:
1. const int pinPWM = 11; // here, we have initialized the PWM pin.
2. void setup ( )
3. {
4. [Link](9600);
5. }
6. void loop ( )
7. {
8. int x = 1;
9. for (int i = 0; i > -1; i = i + x)
10. {
11. analogWrite(pinPWM, i);
12. if (i == 255)
13. {
14. x = -1; // It will switch the direction at peak
15. }
16. delay(10); // It is delay time of 10 milliseconds
17. // the lesser the time, the more fading effect can be seen clearly
18. }
19. }
For the connection, we will connect the positive terminal of the LED in series with the
resistor to PIN 11 (PWM pin), and the negative terminal of the LED to GND.
Note: The for loops in C++ programming language is much more flexible than other types of
programming languages.
Example 4:
Consider the below code:
1. void setup ( )
2. {
3. int i;
4. [Link](9600);
5. for (i = 0; i < 4; i = i + 1)
6. {
7. [Link]( "Hello Arduino" );
8. }
9. [Link]( " DONE");
10. }
11. void loop ( )
12. {
13. }
The above code will print 'Hello Arduino' four times. After that the condition becomes
false, control comes out of the loop, and 'DONE' is printed.
Output:
Similarly, we can create any program using for loop accordingly.
Arduino while loop
The while loop() is the conditional loop that continues to execute the code inside the
parentheses until the specified condition becomes false.
The while loop will never exit until the tested condition is changed or made to stop. The
common use of a while loop in Arduino
includes sensor testing, calibration (calibrating the input of sensor), variable increment, etc.
The syntax is:
1. while (condition)
2. {
3. // code or set of statements
4. }
where
condition: It specifies the boolean expression, which determines the condition to be
true or false.
For example,
variable = 0;
1. while (variable < 100) {
2. // performs the specified task 100 times repeatedly
3. variable++ ; // increments after every execution
4. }
The above code inside the curly braces in while loop will execute continuously and
repeatedly as long as the variable is less than 100.
Flowchart
The flowchart is shown below:
Code Example
In order to change the flow of the program, we need to change the specified condition
inside the parentheses of while loop. The process is much like the if statement.
Let's understand the concept of while loop with two examples.
Example 1:
Consider the below code:
1. int a = 0;
2. void setup()
3. {
4. [Link](9600);
5. while( a < 5)
6. {
7. [Link]("Welcome to Arduino");
8. a = a + 1;
9. }
10. }
11. void loop()
12. {
13. }
Output:
The message inside the loop will be printed five times, as shown below:
Let's look at the code.
Explanation: At first, we have defined the variable a as 0, and initialized the serial
monitor as usual (means [Link]()). The loop first checks the condition specified
within the parentheses. At first, a =0. The condition is true (0 < 5). The code inside the
curly braces will execute, and the message is printed.
Now, the value of a is incremented (added 1 to a), and the loop executes again.
Similarly, five times the condition remains true. When a =5, the condition becomes false,
and the loop exits and drops. The code outside the loop is executed.
Let's consider another example.
Example 2:
The code is similar to the above example. We will only add some statements outside the
loop.
Consider the below code:
int a = 0;
1. void setup()
2. {
3. [Link](9600);
4. while( a < 5)
5. {
6. [Link]("Welcome to Arduino");
7. a = a + 1;
8. }
9. [Link]("DONE");
10. [Link]("Welcome to the code outside the loop");
11. }
12. void loop()
13. {
14. }
Output:
We can notice that as soon as the condition in the loop becomes false, the message
specified outside the loop is printed.
do...while
The working of the do-while loop is similar to the while loop. The condition inside the
do-while will execute at least once. It is because the condition is tested at the end of the
loop instead of the beginning.
The syntax is:
1. do
2. {
3. // code or set of statements
4. } while (condition);
where,
condition: It specifies the boolean expression, which determines the condition to be
true or false.
For example,
1. int b = 0;
2. do {
3. delay(100); // wait for stabilization of the sensors
4. b = readSensors(); // It checks the sensors
5. } while (x < 100); // specified condition
Flowchart
The flowchart is shown below:
Code Example
Let's understand the concept of the do-while loop with an example.
Example 1:
Consider the below code:
1. int a = 0;
2. void setup()
3. {
4. [Link](9600);
5. do
6. {
7. [Link]("Welcome to the do while loop");
8. a = a + 1;
9. } while( a < 3);
10. }
11. void loop()
12. {
13. }
Output: