Arduino

Arduino for loop

In Arduino programming when different instructions are to be repeated multiple times despite writing that instruction multiple times a loop structure can be used. To make a loop out of a single instruction mostly “for” loop is used. This discourse briefly explains the working and usage of the for loop in Arduino programming.

Arduino for loop

The for loop in Arduino programming can be used in the set-up function, loop function or any other function declared in the code.

The body of the for loop consists of three parameters one is the control variable having integer data type, the second one is the condition which evaluates the input given and the last one is the increment or decrement in the value of the control variable. The syntax for the body of the for loop is given below:

for ( int; test-condition; increment/decrement)
{
   statement(s);
}

The control variable is also called the initialization of the loop as the loop starts from the value given to the control variable. The test condition is the expression which can include any type of operator either logical or mathematical.

If the control variable is true according to the test condition then the value of the control variable can be incremented or decremented after executing the body statements. However, if the output of the test condition is false the loop will be terminated. The working of the for loop can be further understood by the flow chart of the loop:

Example Codes

The for loop can be used for multiple purposes for example it can be used to generate a list of numbers having specific intervals either in ascending order or descending order. Similarly, it can be used as mentioned above to repeat a statement. To further explain three example codes of the for loop one having the increment and the other having the decrement and the third one having multiple statements having different mathematical operators is given.

Example 1 of for loop in Arduino programing

In this Arduino code the list of numbers from 0 to 3 are generated using a for loop in ascending order by using the increment operator.

void setup (void) {
   Serial.begin(9600);

   Serial.println(" Generating numbers in ascending order by for loop");
   for (int i=0; i<=3; i++) {
      Serial.print("i is : ");
      Serial.println(i);
   }
}
void loop(void) {
}

Output

Example 2 of the for loop in Arduino programing

The list of numbers generated from 0 to 3 are generated in descending order using the decrement operator in the for loop.

void setup (void) {

   Serial.begin(9600);

   Serial.println(" Generating numbers in descending order by for loop");
   for (int a=3; a>=0; a--) {
      Serial.print("a is : ");
      Serial.println(a);
   }
}

void loop(void) {
}

Output

Example 3 of the for loop in Arduino programing

The example code of for loop when multiple statements are to be repeated is given. There are two mathematical operators that are addition and multiplication are used in the given code. Here In the code there is one variable b having a value of 2 and the other variable used is the iteration number which changes after every iteration of the program.This program runs for only four iterations.

void setup (void) {
int b = 2;
int c;
int d;
   Serial.begin(9600);
   for (int i=1; i<=4; i++) {
     Serial.print(" ITERATION NO.:");
     Serial.println(i);
     Serial.print(" Addition: ");
      c = i+b;
      Serial.println(c);
     Serial.print("Multiplication: ");
      d= i*b;
      Serial.println(d);
   }
}
void loop(void) {
}

Output

Conclusion

In Arduino programming, if multiple statements are to be executed repeatedly, then instead of writing the statements again and again different types of loops can be used in Arduino. Among the different types of loops one type is for loop. In this write-up the for loop is explained with a flow chart and three different Arduino codes are given for getting the clear picture of working of the for loop.

About the author

Aaliyan Javaid

I am an electrical engineer and a technical blogger. My keen interest in embedded systems has led me to write and share my knowledge about them.