# 3C: Loop Examples

Consider the following Arduino with the attached LEDs:

9 LEDs are attached to digital pins 5 through 13.

### Example #1

`for (int i = 5; i < 14; i++) {   digitalWrite(i, HIGH);   delay(100);   digitalWrite(i, LOW);   delay(100); }`

### Pattern for Example #1

Iteration
Number

Value of i

Pattern

 13 12 11 10 9 8 7 6 5
1 5
 ☼
2 6
 ☼
3 7
 ☼
4 8
 ☼
5 9
 ☼
6 10
 ☼
7 11
 ☼
8 12
 ☼
9 13
 ☼
Final value of i is 14

### Example #2

`for (int i = 13; i > 4; i--) {   digitalWrite(i, HIGH);   delay(100);   digitalWrite(i, LOW);   delay(100); }`

### Example #3

`for (int i = 5; i < 14; i = i + 2) {   digitalWrite(i, HIGH);   delay(100);   digitalWrite(i, LOW);   delay(100); }`

### Example #4

`for (int i = 0; i < 9; i++) {   digitalWrite(5+i, HIGH);   digitalWrite(13-i, HIGH);   delay(100);   digitalWrite(5+i, LOW);   digitalWrite(13-i, LOW);   delay(100); }`

### Example #5

`for (int i = 0; i < 5; i++) {   digitalWrite(9+i, HIGH);   digitalWrite(9-i, HIGH);   delay(100);   digitalWrite(9+i, LOW);   digitalWrite(9-i, LOW);   delay(100); }`

### Example #6

`for (int i = 0; i < 7; i++) {   digitalWrite(5+i, HIGH);   digitalWrite(6+i, HIGH);   digitalWrite(7+i, HIGH);   delay(100);   digitalWrite(5+i, LOW);   digitalWrite(6+i, LOW);   digitalWrite(7+i, LOW);   delay(100); }`

### Example #7

`for (int i = 0; i < 7; i = i + 2) {   digitalWrite(5+i, HIGH);   digitalWrite(6+i, HIGH);   digitalWrite(7+i, HIGH);   delay(100);   digitalWrite(5+i, LOW);   digitalWrite(6+i, LOW);   digitalWrite(7+i, LOW);   delay(100); }`

### Example #8

`for (int i = 0; i < 7; i++) {   digitalWrite(5+i, HIGH);   delay(100);   digitalWrite(5+i, LOW);   delay(100);    digitalWrite(7+i, HIGH);   delay(100);   digitalWrite(7+i, LOW);   delay(100); }`

### Example #9

`int i = 5; while(i < 14) {   digitalWrite(i, HIGH);   delay(100);   digitalWrite(i, LOW);   delay(100);   i++; }`

### Example #10

`int i = 5; while(i < 14) {   i++;   digitalWrite(i, HIGH);   delay(100);   digitalWrite(i, LOW);   delay(100); }`

### Example #11

`int i = 5; while(i < 14) {   digitalWrite(i, HIGH);   delay(100);   digitalWrite(i, LOW);   delay(100);    if (i == 8) break;    i++; }`

### Example #12

`i = 5; while(i < 14) {   if (i == 8 ) continue;    digitalWrite(i, HIGH);   delay(100);   digitalWrite(i, LOW);   delay(100);   i++; }`

### Example #13

`int i = 5; do {   digitalWrite(i, HIGH);   delay(100);   digitalWrite(i, LOW);   delay(100);   i++; } while (i < 14);`

### Example #14

`int i = 5; do {   digitalWrite(i, HIGH);   delay(100);   digitalWrite(i, LOW);   delay(100);   i++; } while (i < 5);`

### Example #15

`while (true) {   for (int i = 5; i < 14; i++) {     digitalWrite(i, HIGH);     delay(100);     digitalWrite(i, LOW);     delay(100);   } }`

### Example #16

`for (int i = 0; i < 5; i++) {   digitalWrite(5+i, HIGH);   digitalWrite(13-i, HIGH);   delay(100); }  for (int j = 0; j < 5; j++) {   digitalWrite(9+j, LOW);   digitalWrite(9-j, LOW);   delay(100); }`