Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
en:iot-open:programming_fundamentals_rtu:looping [2018/01/23 13:10] – created Agrisniken:iot-open:programming_fundamentals_rtu:looping [2020/07/20 09:00] (current) – external edit 127.0.0.1
Line 2: Line 2:
  
 ===== For ===== ===== For =====
 +**//For//** is a cycle operator that allows to specify the number of times when the same statements will be executed. In this way, similar to the //loop// function it allows to control the program execution. Each time when all statements in the body of the cycle are executed, is called the //iteration//. In this way, the cycle is one of the basic programming techniques that is used for all programs and automation in general.
 +
 +The construction of a //for// cycle is the following:
 +<code c>
 +for (initialization ; condition ; operation with the cycle variable) {
 +  //the body of the cycle
 +}
 +</code>
 +
 +Three parts of the //for// construction is the following:
 +  * //initialization// section usually initializes the value of the variable that will be used to iterate the cycle; this value can be 0 or any other value;
 +  * //condition// allows to manage the number of cycle iterations; the statements in the body of the cycle are executed when the condition is //TRUE//;
 +  * //operations with the cycle variable// allows to define the number of cycle iterations.
 +
 +The example of the //for// cycle:
 +<code c>
 +for (int i = 0; i < 4; i = i + 1) 
 +{
 +    digitalWrite(13, HIGH);
 +    delay(1000);
 +    digitalWrite(13, LOW);
 +    delay(1000);
 +}
 +</code>
 +On the initialization of the //for// cycle the variable //i=0// is defined. The condition states that the //for// cycle will be executed while the value of variable //i// will be less than 4 (i<4). In the operation with the cycle variable it is increased by 1 each time when the cycle is repeated.
 +
 +In this example above, the LED that is connected to the pin 13 of the Arduino board will turn on/off four times.
  
 ===== While ===== ===== While =====
 +**//While//** cycle operator is similar to the //for// cycle operator that is described above, but it does not contain the cycle variable. Because of this, the //while// cycle allows to executed previously unknown number of iterations. The management of the cycle is realized using only //condition// that needs to be //TRUE// for next operation to execute.
 +
 +The construction of the //while// cycle is the following:
 +<code c>
 +while (condition that is TRUE)
 +{
 +  //the body of the cycle
 +}
 +</code>
 +
 +That way the //while// cycle can be used as a good instrument for execution of a previously unpredictable program. For example, if it is necessary to wait until the signal from the pin 2 reaches the defined voltage level - 100, the following code can be used:
 +
 +<code c>
 +int inputVariable = analogRead(2);
 +while (inputVariable < 100)
 +{
 +    digitalWrite(13, HIGH);
 +    delay(10);
 +    digitalWrite(13, LOW);
 +    delay(10);
 +    inputVariable = analogRead(2);
 +}
 +</code>
 +
 +In the cycle the LED that is connected to the pin 13 of the Arduino board will be turned on/off while the signal will reach specified level.
 + 
  
 ===== Do while ===== ===== Do while =====
 +//Do while// cycle works the same way like the //while// loop. The difference is that in the //while// cycle the condition is checked before entering the loop, but in the //do while// cycle the condition is checked after execution of the statements in the loop and then if the condition is //TRUE// the loop repeats. As the result, the statements inside the cycle will execute at least once, even if the test condition is //FALSE//.
 +
 +The construction of a //do while// cycle is the following:
 +<code c>
 +do {
 +  //the body of the cycle
 +} while (condition that is TRUE);
 +</code>
 +
 +If the same code is taken from the //while// loop example and used in the //do while// cycle, 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:
 +<code c>
 +int inputVariable = analogRead(2);
 +do {
 +    digitalWrite(13, HIGH);
 +    delay(10);
 +    digitalWrite(13, LOW);
 +    delay(10);
 +    inputVariable = analogRead(2);
 +} while (inputVariable < 100);
 +</code>
 +
 +**Check yourself**
 +
 +1. What loop where the condition is checked after the loop body is executed?
 +
 +2. How long will the operators in the body of the loop will operate (while (x < 100))?
 +
 +3. What value will be for variable //a//  after code executing <code C> int a; for(a = 0; a < 10; a++) {} </code>
  
 +4. Which of the following operators are not loop(s) in Arduino IDE?
 +  * do while
 +  * while
 +  * repeat until
 +  * for
  
en/iot-open/programming_fundamentals_rtu/looping.1516713047.txt.gz · Last modified: 2020/07/20 09:00 (external edit)
CC Attribution-Share Alike 4.0 International
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0