Découvrez notre Chaîne YouTube "Ingénierie et Projets"
Découvrez notre Chaîne Secondaire "Information Neuronale et l'Ingénierie du Cerveau"

Objectifs

  1. A quoi sert la boucle for ?
  2. La syntaxe
  3. Les erreurs à éviter

Syntaxe

int i; % Indice de la boucle

for (i=valeur initiale; i<=valeur finale; pas d'incrémentation)

{

  instruction 1;

  instruction 2;

  ...

}

Exemple 1

int i; % Pas entier (croissant)

for (i=0; i<=10; i=i++) % 0, 1, 2, ..., 10

{

  instruction 1;

  instruction 2;

  ...

}

Exemple 2

float i; % Pas fractionnel 

for (i=0; i<=2; i=i+0.1) % 0, 0.1, 0.2, ..., 2

{

  instruction 1;

  instruction 2;

  ...

}

Exemple 3

int i; % Indice décroissant  

for (i=10; i>=0; i=i--) % 10, 9, 8, 7, ..., 0

{

  instruction 1;

  instruction 2;

  ...

}

Les erreurs à éviter

  1. Oublie des accolades {}
for (i=0; i<10; i++)

  instruction1; % Incluse dans la boucle  

  instruction2; % Non incluse dans la boucle

  ...;          % Non incluse dans la boucle
  1. Modification de l’indice de la boucle
for (i=0; i<10; i++)

{

  instruction 1;

  instruction 2;

  i=3; % A ne pas faire % !!!!!!!!!!!!

  ...

}
Click to rate this post!
[Total: 0 Average: 0]

0 commentaire

Laisser un commentaire

Emplacement de l’avatar

Votre adresse e-mail ne sera pas publiée. Les champs obligatoires sont indiqués avec *

Anti-Robot *

You have successfully subscribed to the newsletter

There was an error while trying to send your request. Please try again.

FPGA | Arduino | Matlab | Cours will use the information you provide on this form to be in touch with you and to provide updates and marketing.