This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
en:iot-open:introductiontoembeddedprogramming2:cppfundamentals:loops [2023/06/25 17:08] – created ktokarz | 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 very important, 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 allows specifying the number of times of execution statements inside it. Each time when all statements in the body of the loop are executed is called the **iteration**. In this way, the loop is one of the basic programming techniques that is 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 initialization | + | * ** initialisation** section usually initialises the value of the cycle variable that will be used to iterate the loop; the initialisation |
- | * **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 initialization | + | On the initialisation |
- | In the example above Arduino function digitalWrite is used. It sets the logical state high or low at the chosen pin. If there is a LED 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 it does not contain the cycle variable. Because of this, the //while// loop allows executing a previously unknown number of iterations. The management | + | **'' |
- | 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 in a similar way 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 { | ||
//The body of the loop | //The body of the loop | ||
- | } while (condition that is TRUE); | + | } while (a condition that is TRUE); |
</ | </ | ||
- | 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); | ||
Line 80: | Line 81: | ||
} while (inputVariable < 100); | } while (inputVariable < 100); | ||
</ | </ | ||
- | |||
- | **Check Yourself** | ||
- | |||
- | 1. What is a kind of loop, where the condition is checked after the loop body is executed? | ||
- | |||
- | 2. How long will the statements in the body of the loop execute [while (//x// < 100)]? | ||
- | |||
- | 3. What value will be for variable //a// after code execution? <code C> int a; for(a = 0; a < 10; a++) {} </ | ||
- | |||
- | 4. Which of the following statements are not loop(s) in C/C++? | ||
- | * // | ||
- | * //while//, | ||
- | * //repeat until//, | ||
- | * //for//. |