This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| en:iot-open:introductiontoembeddedprogramming2:cppfundamentals:loops [2023/11/17 18:45] – pczekalski | en:iot-open:introductiontoembeddedprogramming2:cppfundamentals:loops [2023/11/23 12:20] (current) – pczekalski | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| + | ====== Loops ====== | ||
| + | {{: | ||
| + | Loops are critical to control flow structures in programming. They allow executing statements or some part of the program repeatedly to process elements of data tables and texts, making iterative calculations and data analysis. In the world of microcontrollers, | ||
| + | This is clearly visible in the Arduino programming model, with one part of the code executed once after power-on '' | ||
| + | ==== for ==== | ||
| + | **'' | ||
| + | |||
| + | The construction of a '' | ||
| + | <code c> | ||
| + | for (initialization ; condition ; operation with the cycle variable) { | ||
| + | //The body of the loop | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | Three parts of the '' | ||
| + | * ** initialisation** section usually initialises the value of the cycle variable that will be used to iterate the loop; the initialisation value is ofter 0 but can be any other value, | ||
| + | * **condition** allows managing the number of loop iterations; the statements in the body of the loop are executed when the condition is '' | ||
| + | * **operation with the cycle variable** specifies how the cycle variable is modified every iteration (incremented, | ||
| + | |||
| + | The example of the '' | ||
| + | <code c> | ||
| + | for (int i = 0; i < 4; i = i + 1) | ||
| + | { | ||
| + | digitalWrite(13, | ||
| + | delay(1000); | ||
| + | digitalWrite(13, | ||
| + | delay(1000); | ||
| + | } | ||
| + | </ | ||
| + | On the initialisation of the '' | ||
| + | |||
| + | In the example above, the Arduino function digitalWrite is used. It sets the logical state high or low at the chosen pin. If an LED is connected to pin 13 of the Arduino board, it will turn on/off four times. | ||
| + | |||
| + | ==== while ==== | ||
| + | **'' | ||
| + | |||
| + | The construction of the '' | ||
| + | <code c> | ||
| + | while (condition is TRUE) | ||
| + | { | ||
| + | //The body of the loop | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | That way, the '' | ||
| + | |||
| + | <code c> | ||
| + | int inputVariable = analogRead(2); | ||
| + | while (inputVariable < 100) | ||
| + | { | ||
| + | digitalWrite(13, | ||
| + | delay(10); | ||
| + | digitalWrite(13, | ||
| + | delay(10); | ||
| + | inputVariable = analogRead(2); | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | In the loop above, the LED that is connected to pin 13 of the Arduino board will be turned on/off until the signal reaches the specified level. | ||
| + | |||
| + | ==== do...while ==== | ||
| + | The '' | ||
| + | |||
| + | The construction of a '' | ||
| + | <code c> | ||
| + | do { | ||
| + | //The body of the loop | ||
| + | } while (a condition that is TRUE); | ||
| + | </ | ||
| + | |||
| + | If the same code is taken from the '' | ||
| + | <code c> | ||
| + | int inputVariable = analogRead(2); | ||
| + | do { | ||
| + | digitalWrite(13, | ||
| + | delay(10); | ||
| + | digitalWrite(13, | ||
| + | delay(10); | ||
| + | inputVariable = analogRead(2); | ||
| + | } while (inputVariable < 100); | ||
| + | </ | ||