Difference between revisions of "Basic Pascal Tutorial/Chapter 3/IF/fr"
Inspirator (talk | contribs) m (Fautes d'orthographes) |
m (Fixed syntax highlighting) |
||
Line 4: | Line 4: | ||
L'instruction <tt>IF</tt> vous permet de bifurquer selon le résultat d'une opération booléenne. Le format d'une branche unique est : | L'instruction <tt>IF</tt> vous permet de bifurquer selon le résultat d'une opération booléenne. Le format d'une branche unique est : | ||
− | <syntaxhighlight> | + | <syntaxhighlight lang=pascal> |
if BooleanExpression then | if BooleanExpression then | ||
InstructionSiVrai; | InstructionSiVrai; | ||
Line 12: | Line 12: | ||
L'instruction <tt>IF</tt> accepte une unique instruction. Si vous voulez vous brancher sur une instruction composée, vous devez utiliser un [[Block|bloc]] <tt>begin-end</tt> pour délimiter les instructions : | L'instruction <tt>IF</tt> accepte une unique instruction. Si vous voulez vous brancher sur une instruction composée, vous devez utiliser un [[Block|bloc]] <tt>begin-end</tt> pour délimiter les instructions : | ||
− | <syntaxhighlight> | + | <syntaxhighlight lang=pascal> |
if BooleanExpression then | if BooleanExpression then | ||
begin | begin | ||
Line 21: | Line 21: | ||
Il y a aussi la sélection à deux voies : | Il y a aussi la sélection à deux voies : | ||
− | <syntaxhighlight> | + | <syntaxhighlight lang=pascal> |
if BooleanExpression then | if BooleanExpression then | ||
InstructionSiVrai | InstructionSiVrai | ||
Line 31: | Line 31: | ||
Si vous avez besoin d'une sélection multiple, imbriquez simplement les instructions <tt>if</tt> : | Si vous avez besoin d'une sélection multiple, imbriquez simplement les instructions <tt>if</tt> : | ||
− | <syntaxhighlight> | + | <syntaxhighlight lang=pascal> |
if Condition1 then | if Condition1 then | ||
Instruction1 | Instruction1 | ||
Line 42: | Line 42: | ||
Soyez prudent avec l'imbrication. Parfois, l'ordinateur ne fera pas ce que vous voulez : | Soyez prudent avec l'imbrication. Parfois, l'ordinateur ne fera pas ce que vous voulez : | ||
− | <syntaxhighlight> | + | <syntaxhighlight lang=pascal> |
if Condition1 then | if Condition1 then | ||
if Condition2 then | if Condition2 then | ||
Line 51: | Line 51: | ||
Le <tt>else</tt> correspond toujours au <tt>if</tt> le plus proche, donc l'ordinateur interprète le bloc de code précédent comme : | Le <tt>else</tt> correspond toujours au <tt>if</tt> le plus proche, donc l'ordinateur interprète le bloc de code précédent comme : | ||
− | <syntaxhighlight> | + | <syntaxhighlight lang=pascal> |
if Condition1 then | if Condition1 then | ||
if Condition2 then | if Condition2 then | ||
Line 60: | Line 60: | ||
Vous pouvez obtenir la même chose avec l'instruction nulle : | Vous pouvez obtenir la même chose avec l'instruction nulle : | ||
− | <syntaxhighlight> | + | <syntaxhighlight lang=pascal> |
if Condition1 then | if Condition1 then | ||
if Condition2 then | if Condition2 then | ||
Line 70: | Line 70: | ||
Ou vous pouvez utiliser un bloc <tt>begin-end</tt>. Mais la meilleure façon de clarifier le code précédent est de réécrire la condition : | Ou vous pouvez utiliser un bloc <tt>begin-end</tt>. Mais la meilleure façon de clarifier le code précédent est de réécrire la condition : | ||
− | <syntaxhighlight> | + | <syntaxhighlight lang=pascal> |
if not Condition1 then | if not Condition1 then | ||
Instruction1 | Instruction1 |
Revision as of 11:14, 17 February 2020
│
български (bg) │
English (en) │
français (fr) │
日本語 (ja) │
中文(中国大陆) (zh_CN) │
Tutoriel de Pascal Objet : Structures de contrôle / Branchements conditionnels / Si, sinon
L'instruction IF vous permet de bifurquer selon le résultat d'une opération booléenne. Le format d'une branche unique est :
if BooleanExpression then
InstructionSiVrai;
Si l'expression booléenne est évaluée à true, l'instruction est exécutée. Sinon, elle est ignorée.
L'instruction IF accepte une unique instruction. Si vous voulez vous brancher sur une instruction composée, vous devez utiliser un bloc begin-end pour délimiter les instructions :
if BooleanExpression then
begin
Instruction1;
Instruction2
end;
Il y a aussi la sélection à deux voies :
if BooleanExpression then
InstructionSiVrai
else
InstructionSiFaux;
Si l'expression booléenne est évaluée à FALSE, l'instruction suivant le else sera réalisée. Notez que vous ne pouvez pas utiliser le point-virgule après l'instruction précédant le else. Cela amène l'ordinateur à la traiter comme une sélection à branche unique, le laissant à se demander d'où l'autre est venue.
Si vous avez besoin d'une sélection multiple, imbriquez simplement les instructions if :
if Condition1 then
Instruction1
else
if Condition2 then
Instruction2
else
Instruction3;
Soyez prudent avec l'imbrication. Parfois, l'ordinateur ne fera pas ce que vous voulez :
if Condition1 then
if Condition2 then
Instruction2
else
Instruction1;
Le else correspond toujours au if le plus proche, donc l'ordinateur interprète le bloc de code précédent comme :
if Condition1 then
if Condition2 then
Instruction2
else
Instruction1;
Vous pouvez obtenir la même chose avec l'instruction nulle :
if Condition1 then
if Condition2 then
Instruction2
else
else
Instruction1;
Ou vous pouvez utiliser un bloc begin-end. Mais la meilleure façon de clarifier le code précédent est de réécrire la condition :
if not Condition1 then
Instruction1
else
if Condition2 then
Instruction2;
Ces exemples illustrent où l'opérateur not est utile. Si Condition1 était une expression comme : (not(a < b) or (c + 3 > 6)) and g, inverser l'expression serait plus difficile que la préfixer par not.
Aussi, remarquez comment l'indentation transmet la logique du code du programme à un humain, mais le compilateur ignore l'indentation.
← | Sommaire | → |