Javatpoint Logo
Javatpoint Logo

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:


  • initialization: It is defined as the initialization of the variable.
  • 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.
  • 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,

The above statement will execute the loop for five times. The values of i will be from 0 to 4.

If the statement is:

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 Serial.println ( ), 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.


Arduino for Loop

Example 2:

To use a multiplication increment

The multiplication increment in the for loop will generate a logarithmic progression.

Consider the below code:


Arduino for Loop

We can also declare the int data type directly in the for loop.

For example,

Example 3: To fade an LED

Here, fade and LED means that LED will faint slowly.

Consider the below code:

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:

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.


Arduino for Loop

Similarly, we can create any program using for loop accordingly.

Next TopicArduino while loop

Youtube For Videos Join Our Youtube Channel: Join Now


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA