Sign in Join
Wiki copytemporary

Basic Pascal Tutorial/Chapter 3/IF/fr: what changed

Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.

Title: Basic Pascal Tutorial/Chapter 3/IF/fr

OldNewLine
1+[[Basic Pascal Tutorial/fr|Tutoriel de Pascal Objet]] : Structures de contrôle / Branchements conditionnels / **Si, sinon**
2+
3+L'instruction `IF` vous permet de bifurquer selon le résultat d'une opération booléenne. Le format d'une branche unique est :
4+
5+``` pascal
6+if BooleanExpression then
7+ InstructionSiVrai;
8+```
9+
10+Si l'expression booléenne est évaluée à `true`, l'instruction est exécutée. Sinon, elle est ignorée.
11+
12+L'instruction `IF` accepte une unique instruction. Si vous voulez vous brancher sur une instruction composée, vous devez utiliser un [[Block|bloc]] `begin-end` pour délimiter les instructions :
13+
14+``` pascal
15+if BooleanExpression then
16+begin
17+ Instruction1;
18+ Instruction2
19+end;
20+```
21+
22+Il y a aussi la sélection à deux voies :
23+
24+``` pascal
25+if BooleanExpression then
26+ InstructionSiVrai
27+else
28+ InstructionSiFaux;
29+```
30+
31+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.
32+
33+Si vous avez besoin d'une sélection multiple, imbriquez simplement les instructions `if` :
34+
35+``` pascal
36+if Condition1 then
37+ Instruction1
38+else
39+ if Condition2 then
40+ Instruction2
41+ else
42+ Instruction3;
43+```
44+
45+Soyez prudent avec l'imbrication. Parfois, l'ordinateur ne fera pas ce que vous voulez :
46+
47+``` pascal
48+if Condition1 then
49+ if Condition2 then
50+ Instruction2
51+else
52+ Instruction1;
53+```
54+
55+Le `else` correspond toujours au `if` le plus proche, donc l'ordinateur interprète le bloc de code précédent comme :
56+
57+``` pascal
58+if Condition1 then
59+ if Condition2 then
60+ Instruction2
61+ else
62+ Instruction1;
63+```
64+
65+Vous pouvez obtenir la même chose avec l'instruction nulle :
66+
67+``` pascal
68+if Condition1 then
69+ if Condition2 then
70+ Instruction2
71+ else
72+else
73+ Instruction1;
74+```
75+
76+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 :
77+
78+``` pascal
79+if not Condition1 then
80+ Instruction1
81+else
82+ if Condition2 then
83+ Instruction2;
84+```
85+
86+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`.
87+
88+Aussi, remarquez comment l'indentation transmet la logique du code du programme à un humain, mais le compilateur ignore l'indentation.
89+
90+---
91+*From the Free Pascal wiki, page "Basic Pascal Tutorial/Chapter 3/IF/fr", as it stood on 2022-08-20 after a change by Kai Burghardt, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).*

Back to history