Vous êtes sur la page 1sur 10

IFT - 1903

INFORMATIQUE POUR L’INGÉNIEUR


Programmation avec MATLAB (P3)

A. ARESMOUK
SOMMAIRE

La structure de contrôle de répétition


- La boucle « FOR »
- La boucle « WHILE »
L’instruction « BREAK »

2
LES STRUCTURES DE CONTRÔLE
LA BOUCLE « FOR »
L’instruction « for » permet de répéter un bloc d’instructions, un
nombre déterminé de fois.
La syntaxe est de la forme:

for compteur = début : pas : fin


Séquence d’instructions
end

3
LES STRUCTURES DE CONTRÔLE
Exemple 1:

4
LES STRUCTURES DE CONTRÔLE
LA BOUCLE « FOR »
Exemple 2:

clear all
for i = 1:3
for j = 1:4
M(i, j) = i + j;
end
end
disp(M)

5
LES STRUCTURES DE CONTRÔLE
LA BOUCLE « FOR »
Exemple 2:

clear all >> Exemple


for i = 1:3
for j = 1:4 M=
M(i, j) = i + j; 2 3 4 5
end 3 4 5 6
end 4 5 6 7
disp(M)

6
LES STRUCTURES DE CONTRÔLE
LA BOUCLE « WHILE »
La syntaxe est de la forme:

while (relation)
Séquence d’instructions
end

La boucle est répétée tant que la relation reste vraie.

7
LES STRUCTURES DE CONTRÔLE
LA BOUCLE « WHILE »
Exemple 1:

8
LES STRUCTURES DE CONTRÔLE
L’instruction « BREAK »
L’instruction « break » permet de sortir d’une boucle « for » ou
« while ».

Exemple
clear all
for i = -2:2:10
if(i==4)
break
end
disp(i)
end

9
LES STRUCTURES DE CONTRÔLE
L’instruction « BREAK »
L’instruction « break » permet de sortir d’une boucle « for » ou
« while ».

Exemple
clear all >> Exemple
for i = -2:2:10 -2
if(i==6) 0
break 2
end 4
disp(i)
end

10

Vous aimerez peut-être aussi