Académique Documents
Professionnel Documents
Culture Documents
Chapitre 3 :
LES INSTRUCTIONS DE BRANCHEMENT
ET LES BOUCLES
2. LES BOUCLES
2
1. LES INSTRUCTIONS DE
BRANCHEMENT CONDITIONNEL
L'instruction IF ... ELSE
● Il s'agit de l'instruction:
Algorithmique Langage C
si (condition)
if (expression)
alors
{
bloc d'instructions 1
/* bloc d'instructions 1 */
}
sinon
else
bloc d'instructions 2
{
finsi
/* bloc d'instructions 2 */
}
3
1. LES INSTRUCTIONS DE
BRANCHEMENT CONDITIONNEL
L'instruction IF ... ELSE
Organigramme :
vraie
suite du programme
4
1. LES INSTRUCTIONS DE
BRANCHEMENT CONDITIONNEL
L'instruction IF ... ELSE
Le bloc "sinon" est optionnel :
Organigramme :
Algorithmique Langage C
si (condition) alors if (expression)
{ oui non
condition
bloc d'instructions /* bloc d'instructions */ vraie
}
Finsi
bloc d'
instructions
#include<stdio.h>
#include<conio.h>
main()
{
int A, B, max, min;
printf(“Donner A :”);
scanf(“%d”,&A);
printf(“Donner B :”);
scanf(“%d”,&B);
6
1. LES INSTRUCTIONS DE
BRANCHEMENT CONDITIONNEL
L'instruction IF ... ELSE
if (A>B)
{
max=A ;
min=B;
}
else
{
max=B ;
min=A;
}
printf(“le max = %d et min = %d\n",max,min) ;
getch(),
}
7
1. LES INSTRUCTIONS DE
BRANCHEMENT CONDITIONNEL
Les opérateurs logiques
Test d'égalité: if (a==b) " si a égal b "
Algorithmique Langage C
selon (expression) faire switch (expression)
constante-1 : liste d'instructions 1 {
constante-2 : liste d'instructions 2 case constante-1 : liste d'instructions 1; break;
... case constante-2 : liste d'instructions 2; break;
constante-n : liste d'instructions n ...
autre : liste d'instructions case constante-n : liste d'instructions n; break;
fin selon default : liste d'instructions; break;
}
2. LES BOUCLES
Boucle FOR
● Sa forme la plus générale est la suivante :
Algorithmique Langage C
pour v de vi à vf faire for (initialisation ; condition de continuité ;
bloc d'instructions modification)
finpour {
............;
initialisation
/* bloc d'instructions */
...........;
condition de
continuité vraie
}
non
oui
bloc d'instructions
modification
suite du programme
10
2. LES BOUCLES
Boucle FOR
Remarques :
• Les {} ne sont pas nécessaires lorsque le bloc ne comporte qu'une seule
instruction.
• Les 3 instructions du for ne portent pas forcément sur la même variable.
Exemple 1:
i=0
i=i+1
11
2. LES BOUCLES
Boucle FOR
resultat = 0;
for(i = 0 ; resultat<30 ; i++)
{
/* bloc d'instructions */
resultat = resultat + 2 * i;
}
12
2. LES BOUCLES
Boucle FOR
Exemple 4 :
2. LES BOUCLES
Boucle DO … WHILE
Algorithmique Langage C
répéter do
bloc d'instructions {
jusqu’à (condition) /* bloc d'instructions */
}
Organigramme : while (expression);
bloc d'
instructions
non
condition
oui vraie
suite du programme
14
2. LES BOUCLES
Boucle DO … WHILE
Le test se faisant après, le bloc est exécuté au moins une fois.
Exemple :
Réécrire l’exemple 4 de la boucle for en utilisant la boucle do … while.
2. LES BOUCLES
Boucle WHILE
Algorithmique Langage C
tant que (condition) faire while (condition)
bloc d'instructions {
fintantque /* bloc d'instructions */
}
Organigramme :
oui non
condition
vraie
bloc d'
instructions
16
2. LES BOUCLES
2.3. Boucle WHILE
Le test se fait à l’entrée de la boucle, le bloc d'instructions peut ne jamais être exécuté.
Remarques :
Les {} ne sont pas nécessaires lorsque le bloc ne comporte qu'une seule instruction.
On peut rencontrer la construction suivante :
2. LES BOUCLES
2.3. Boucle WHILE
expr 1; expr 1;
for ( expr 1 ; expr 2 ; expr 3) ≡ while ( expr 2 ) ≡ Do
instructions ; { {
instructions ; instructions ;
expr 3; expr 3;
} }
while (expr 2 ) ;
18
2. LES BOUCLES
2.3. Boucle WHILE
Exemple :