6.3 Les Boucles

Sous Flowcode c'est le pictogramme qu'il faut utiliser pour ajouter une boucle




3 types de boucles existent

  1. Boucle "Tant que" (While:si la condition est testée au début)  (do ... while:si la condition est testée à la fin)  
  2. Boucle "Jusqu'à" ce que (Until)
  3. Compteur de boucle (faire n fois la boucle)


Sous Arduino



La fonction en langage C permettant de réaliser une boucle "Tant que" avec test au début est : while

Description

while loop will loop continuously, and infinitely, until the expression inside the parenthesis, () becomes false. Something must change the tested variable, or the while loop will never exit. This could be in your code, such as an incremented variable, or an external condition, such as testing a sensor.


Syntax

while (condition) {
  // statement(s)
}


Parameters

condition: a Boolean expression that evaluates to true or false.


Example Code

var = 0;
while (var < 200) {
  // do something repetitive 200 times
  var++;
}


La fonction en langage C permettant de réaliser une boucle "Tant que" avec test à la fin est : do...while

Description

The do … ​while loop works in the same manner as the while loop, with the exception that the condition is tested at the end of the loop, so the do loop will always run at least once.


Syntax

do {
  // statement block
} while (condition);


Parameters

condition: a Boolean expression that evaluates to true or false.


Example Code

int x = 0;
do {
  delay(50);          // wait for sensors to stabilize
  x = readSensors();  // check the sensors
} while (x < 100);



La fonction en langage C permettant de faire une boucle "Pour"  est : for 

Description

The for statement is used to repeat a block of statements enclosed in curly braces. An increment counter is usually used to increment and terminate the loop. The for statement is useful for any repetitive operation, and is often used in combination with arrays to operate on collections of data/pins.


Syntax

for (initialization; condition; increment) {
  // statement(s);
}


Parameters

initialization: happens first and exactly once.
condition: each time through the loop, condition is tested; if it’s true, the statement block, and the increment is executed, then the condition is tested again. When the condition becomes false, the loop ends.
increment: executed each time through the loop when condition is true.


Example Code

// Dim an LED using a PWM pin
int PWMpin = 10;  // LED in series with 470 ohm resistor on pin 10
void setup() {
  // no setup needed
}
void loop() {
  for (int i = 0; i <= 255; i++) {
    analogWrite(PWMpin, i);
    delay(10);
  }
}

 

Créé avec HelpNDoc Personal Edition: Générateur d'aides CHM gratuit