Both sides previous revisionPrevious revisionNext revision | Previous revision |
en:iot-open:introductiontoembeddedprogramming2:cppfundamentals:loops [2023/11/17 14:33] – pczekalski | en:iot-open:introductiontoembeddedprogramming2:cppfundamentals:loops [2023/11/23 10:20] (current) – pczekalski |
---|
====== Loops ====== | ====== Loops ====== |
| {{:en:iot-open:czapka_b.png?50| General audience classification icon }}{{:en:iot-open:czapka_e.png?50| General audience classification icon }}\\ |
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, where sometimes there is no operating system, the whole software works in the main loop called a super loop. It means the program never ends and works until the power is off.\\ | 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, where sometimes there is no operating system, the whole software works in the main loop called a super loop. It means the program never ends and works until the power is off.\\ |
This is clearly visible in the Arduino programming model, with one part of the code executed once after power-on ''setup()'', and another executed repeatedly ''loop()''. In C/C++, there are three loop statements shown in this chapter. | This is clearly visible in the Arduino programming model, with one part of the code executed once after power-on ''setup()'', and another executed repeatedly ''loop()''. In C/C++, there are three loop statements shown in this chapter. |
| |
===== for ===== | ==== for ==== |
**''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. | **''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. |
| |
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. | **''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. |
| |
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 ''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''. |
| |