for

Description

The for statement is used to repeat a block of statements enclosed in curly braces. An increment counter is usually used to increment and terminate the loop. The for statement is useful for any repetitive operation, and is often used in combination with arrays to operate on collections of data/pins.

Syntax

for (initialization; condition; increment) { // statement(s); }

If there is only one statement, the curly braces can be omitted.

for (initialization; condition; increment) // a single statement;

Parameter Values

  • initialization: happens first and exactly once.
  • condition: each time through the loop, condition is tested; if it's true, the statement block, and the increment is executed, then the condition is tested again. When the condition becomes false, the loop ends.
  • increment: executed each time through the loop when condition is true.

Example Code

Example Code 1

void setup() { Serial.begin(9600); Serial.println("====== TEST START ======"); for (int i = 1; i <= 5; i++) { Serial.print("Inside the FOR loop: i = "); Serial.println(i); } Serial.println("====== TEST END ========"); } void loop() {}

In the example above, the code in the loop will run, over and over again five times.

The result on Serial Monitor:

COM6
Send
====== TEST START ====== Inside the FOR loop: i = 1 Inside the FOR loop: i = 2 Inside the FOR loop: i = 3 Inside the FOR loop: i = 4 Inside the FOR loop: i = 5 ====== TEST END ========
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

Example explained:

  • Initialization: int i = 1 sets a variable before the loop starts.
  • Condition: i <= 5 defines the condition for the loop to run. If the condition is true (i is less than or equal to 5), the loop will start over again. if it is false (i is greater than 5), the loop will end.
  • Increment: i++ increases a value each time the code block in the loop has been executed.

The above code is equivalent to:

void setup() { Serial.begin(9600); Serial.println("====== TEST START ======"); int i = 1; while ( i <= 5) { Serial.print("Inside the FOR loop: i = "); Serial.println(i); i++; } Serial.println("====== TEST END ========"); } void loop() {}

Example Code 2

// Dim an LED using a PWM pin int PWMpin = 10; // LED in series with 470 ohm resistor on pin 10 void setup() { // no setup needed } void loop() { for (int i = 0; i <= 255; i++) { analogWrite(PWMpin, i); delay(10); } }

※ NOTES AND WARNINGS:

Loop Forever

The following while loop loops forever:

for (;;) { // statement(s); }

How to escape the for loop

There are three ways to escape the for loop:

  • The condition of the for loop becomes false.
  • The execution of the code reaches a break statement inside the loop.
  • The execution of the code reaches a goto statement inside the loop, which jumps to a label located outside of the loop.

Others

The C++ for loop is much more flexible than for loops found in some other computer languages, including BASIC. Any or all of the three header elements may be omitted, although the semicolons are required. Also the statements for initialization, condition, and increment can be any valid C++ statements with unrelated variables, and use any C++ datatypes including floats. These types of unusual for statements may provide solutions to some rare programming problems.

For example, using a multiplication in the increment line will generate a logarithmic progression:

for (int x = 2; x < 100; x = x * 1.5) { println(x); }

Generates: 2,3,4,6,9,13,19,28,42,63,94

Another example, fade an LED up and down with one for loop:

void loop() { int x = 1; for (int i = 0; i > -1; i = i + x) { analogWrite(PWMpin, i); if (i == 255) { x = -1; // switch direction at peak } delay(10); } }

See Also

ARDUINO BUY RECOMMENDATION

Arduino UNO R3
Arduino Starter Kit
Please note: These are Amazon affiliate links. If you buy the components through these links, We will get a commission at no extra cost to you. We appreciate it.

※ OUR MESSAGES