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/13 18:10] – ekontoturbo | en:iot-open:introductiontoembeddedprogramming2:cppfundamentals:loops [2023/11/23 10:20] (current) – pczekalski | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ==== Loops ==== | + | ====== 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 '' | ||
- | Loops are critical to control flow structures in programming. They allow executing statements or some part of the program repeatedly to process elements | + | ==== for ==== |
+ | **'' | ||
- | === for === | + | The construction of a '' |
- | **//for//** is a loop statement that specifies the number of execution times of the statements inside it. Each time all statements in the loop's body are executed is called an **iteration**. In this way, the loop is one of the basic programming techniques used for all programs and automation in general. | + | |
- | + | ||
- | The construction of a //for// loop is the following: | + | |
<code c> | <code c> | ||
for (initialization ; condition ; operation with the cycle variable) { | for (initialization ; condition ; operation with the cycle variable) { | ||
Line 13: | Line 14: | ||
</ | </ | ||
- | Three parts of the //for// construction are the following: | + | 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, | * ** 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 //TRUE//, | + | * **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, | * **operation with the cycle variable** specifies how the cycle variable is modified every iteration (incremented, | ||
- | The example of the //for// loop: | + | The example of the '' |
<code c> | <code c> | ||
for (int i = 0; i < 4; i = i + 1) | for (int i = 0; i < 4; i = i + 1) | ||
Line 28: | Line 29: | ||
} | } | ||
</ | </ | ||
- | On the initialisation of the //for// loop, the cycle variable | + | 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. | 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 === | + | ==== while ==== |
- | **//while//** loop statement is similar to the //for// statement but does not contain the cycle variable. Because of this, the //while// loop allows executing a previously unknown number of iterations. The loop management is realised using only **condition** that needs to be //TRUE// for the next cycle to execute. | + | **'' |
- | The construction of the //while// loop is the following: | + | The construction of the '' |
<code c> | <code c> | ||
while (condition is TRUE) | while (condition is TRUE) | ||
Line 43: | Line 44: | ||
</ | </ | ||
- | That way, the //while// loop can be used as a good instrument for the execution of a previously unpredictable program. For example, if it is necessary to wait until the signal from pin 2 reaches the defined voltage level = 100, the following code can be used: | + | That way, the '' |
<code c> | <code c> | ||
Line 59: | Line 60: | ||
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. | 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 === | + | ==== do...while |
- | The //do...while// loop works similarly to the //while// loop. The difference is that in the //while// loop, the condition is checked before entering the loop, but in the //do...while//, the condition is checked after the execution of the statements in the loop, and then if the condition is //TRUE// the loop repeats. As a result, the statements inside the loop will execute at least once, even if the test condition is //FALSE//. | + | The '' |
- | The construction of a //do while// loop is the following: | + | The construction of a '' |
<code c> | <code c> | ||
do { | do { | ||
Line 69: | Line 70: | ||
</ | </ | ||
- | If the same code is taken from the //while// loop example and used in the //do...while// loop, the difference is that the code will execute at least once, even if the //inputVariable// value is more than or equal to 100. The example code: | + | If the same code is taken from the '' |
<code c> | <code c> | ||
int inputVariable = analogRead(2); | int inputVariable = analogRead(2); |