Last Updated: 20/07/2021
Lesson 4 - for loop
Lessons >> Basics >> Lesson 4 - for loop
Lesson Contents
Using "for loop" to control program flow
Using different Comparison Operators,< (smaller than) and > (greater than)
INcreasing and decreasing values in the foor loop
Negative values in the loop
Nested "for loops"
Nesting "for loops" with "if else"
Adding a second integer variable
Understanding how delay() works inside and outside the "for loop"
Example 1: forLoopv1.ino
Click to Download code:forLoopv1.ino
Simple introduction to the "for loop"
/* 21/07/2021
forLoopv1
Purpose, learn "for loop control"
*/
//An int is an integer, a Number, any value between -32,768 and 32,767
int q;
void setup() {
// Serial.begin(9600) starts serial communication. 9600bps bits per second = 1200 characters per second
Serial.begin(9600);
//Send script name
Serial.println("forLoopv1...");
//this will print a blank line
Serial.println(" ");
}
void loop() {
//q starts with a value of Zero, the loop continues while q smaller than 20
for(q=0;q<20;q++){
//print the value of q to the serial monitor
Serial.print("Val q: ");
Serial.println(q);
//delay within the for loop
delay(100);
}
//delay outside the for loop
delay(1000);
}
Example 2: forLoopv2.ino
Click to Download code:forLoopv2.ino
Increasing and decreasing the value of "q" in the "for loop"
"for loop" with negative values.
/* 21/07/2021
forLoopv2
Purpose, to see different versions of the for loop code
*/
//An int is an integer, a Number, any value between -32,768 and 32,767
int q;
void setup() {
// Serial.begin(9600) starts serial communication. 9600bps bits per second = 1200 characters per second
Serial.begin(9600);
//Send script name
Serial.println("forLoopv2...");
//this will print a blank line
Serial.println(" ");
}
void loop() {
//Example 1
Serial.print("Example 1");
//q starts with a value of Zero, the loop continues while q smaller than 20
for(q=0;q<20;q++){
//print the value of q to the serial monitor
Serial.print("Val q: ");
Serial.println(q);
//delay within the for loop
delay(100);
}
//delay outside the for loop
delay(1000);
//example 2
Serial.print("Example 2");
for(q=30;q<40;q++){
//print the value of q to the serial monitor
Serial.print("Val q: ");
Serial.println(q);
//delay within the for loop
delay(100);
}
//delay outside the for loop
delay(1000);
//example 3
Serial.print("Example 3");
//q is reducing in value through the loop
//q-- subtracts 1 from the value of q
for(q=50;q>35;q--){
//print the value of q to the serial monitor
Serial.print("Val q: ");
Serial.println(q);
//delay within the for loop
delay(100);
}
//delay outside the for loop
delay(1000);
//example 4
Serial.print("Example 4");
//q is reducing in value through the loop
//q-- subtracts 1 from the value of q
//displays q going negative
for(q=10;q>-10;q--){
//print the value of q to the serial monitor
Serial.print("Val q: ");
Serial.println(q);
//delay within the for loop
delay(100);
}
//delay outside the for loop
delay(1000);
}
Example 3: forLoopv3.ino
Click to Download code:forLoopv3.ino
Nested "for loops"
introducing a second variable
/* 21/07/2021
forLoopv3
Purpose, nest for loops
*/
//An int is an integer, a Number, any value between -32,768 and 32,767
int q;
int w;
void setup() {
// Serial.begin(9600) starts serial communication. 9600bps bits per second = 1200 characters per second
Serial.begin(9600);
//Send script name
Serial.println("forLoopv3...");
//this will print a blank line
Serial.println(" ");
}
void loop() {
//Nested for loops
Serial.println("Nested Loop");
//q starts with a value of Zero, the loop continues while q smaller than 20
for(q=0;q<20;q++){
for(w=10;w>-1;w--){
//print the value of q to the serial monitor
Serial.print("Val q: ");
Serial.print(q);
Serial.print(" Val w: ");
Serial.println(w);
//delay within the for loop
delay(100);
}
}
//delay outside the for loop
delay(1000);
}
Example 4: forLoopv4.ino
Click to Download code:forLoopv4.ino
Nesting "for loops" and "if else"
/* 21/07/2021
forLoopv4
Purpose, nest "for loops" and "if else"
*/
//An int is an integer, a Number, any value between -32,768 and 32,767
int q;
int w;
void setup() {
// Serial.begin(9600) starts serial communication. 9600bps bits per second = 1200 characters per second
Serial.begin(9600);
//Send script name
Serial.println("forLoopv4...");
//this will print a blank line
Serial.println(" ");
}
void loop() {
//Nested for loops and if
Serial.println("Nested for and if");
//q starts with a value of Zero, the loop continues while q smaller than 20
for (q = 0; q < 20; q++) {
if (q == 5) {
for (w = 10; w > -1; w--) {
//print the value of q to the serial monitor
Serial.print("q: ");
Serial.print(q);
Serial.print(" w: ");
Serial.println(w);
//delay within the for loop
delay(100);
}
} else {
Serial.print("q: ");
Serial.println(q);
}
}
//delay outside the for loop
delay(1000);
}
Additional Resource Links
Don't forget to use the Reference in you Arduino IDE and look at the Control Structures for loop and Comparison Operators for more help on this lesson.Comments
This site has been designed to be child friendly, this means that comments cannot be added to videos or directly to the site. To add a comment or ask a question please email the address in this image:
![](images/address.gif)