Else/fr: what changed
Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.
Title: Else/fr
| Old | New | Line | |
|---|---|---|---|
| 1 | + | [Else dans la référence du langage](<http://www.freepascal.org/docs-html/ref/refsu51.html#x144-15400013.2.3>) | |
| 2 | + | ||
| 3 | + | Else est un [[Keyword/fr|mot-clé]] qui introduit une action à faire quand la condition du if n'est pas vérifiée. | |
| 4 | + | ||
| 5 | + | # [[If/fr|If]] [[Then/fr|then]] else | |
| 6 | + | ``` | |
| 7 | + | if (condition) | |
| 8 | + | then ''true_statement'' | |
| 9 | + | else ''false_statement''; | |
| 10 | + | ``` | |
| 11 | + | ||
| 12 | + | La _condition_ est évaluée, si la valeur est vraie alors _true_statement_ est exécuté sinon _false_statement_ est exécuté. La valeur de condition doit se ramener à un booléen sous peine d'erreur de compilation. | |
| 13 | + | ||
| 14 | + | ### Plus d'instructions dans l'instruction "if then else" | |
| 15 | + | ||
| 16 | + | Si vous avez besoin de 2 instructions ou plus pour _true_statement_ ou _false_statement_ , alors la suite d'instructions doit être placé dans un [bloc](</index.php?title=Block/fr&action=edit&redlink=1> "Block/fr \(page does not exist\)") délimité par [[Begin/fr|begin]] et [[End/fr|end]]. | |
| 17 | + | ``` | |
| 18 | + | if boolean_condition then | |
| 19 | + | begin | |
| 20 | + | statement_one; | |
| 21 | + | statement_two; | |
| 22 | + | end | |
| 23 | + | else | |
| 24 | + | begin | |
| 25 | + | statement_three; | |
| 26 | + | statement_four; | |
| 27 | + | end; | |
| 28 | + | ``` | |
| 29 | + | ||
| 30 | + | Dans un usage normal, l'instruction "else" est une exception spéciale à la règle qui veut que chaque instruction est suivie d'un point-virgule. Ni le mot-clé "else" ni l'instruction qui le précède immédiatement peuvent avoir un point-virgule qui le suit. Dans l'exemple ci-dessus, la première instruction "end" n'est pas suivie d'un point-virgule, mais la dernière l'est. | |
| 31 | + | ||
| 32 | + | Toutefois, dans le cas des instructions "if" imbriquées, si le _else_ s'applique au _if_ le plus interne, le point-virgule _ne_ doit _pas_ apparaître avent le _else_ , si le _else_ s'applique au _if_ le plus externe alors le point-virgule _doit_ apparaître avant le _else_. | |
| 33 | + | ``` | |
| 34 | + | if a then | |
| 35 | + | if b then | |
| 36 | + | begin | |
| 37 | + | (..) | |
| 38 | + | end; | |
| 39 | + | else | |
| 40 | + | begin | |
| 41 | + | (..) | |
| 42 | + | end; | |
| 43 | + | ``` | |
| 44 | + | ||
| 45 | + | Dans ce cas, le "else" s'applique au "if a" | |
| 46 | + | ``` | |
| 47 | + | if a then | |
| 48 | + | if b then | |
| 49 | + | begin | |
| 50 | + | (..) | |
| 51 | + | end | |
| 52 | + | else | |
| 53 | + | begin | |
| 54 | + | (..) | |
| 55 | + | end; | |
| 56 | + | ``` | |
| 57 | + | ||
| 58 | + | Dans ce cas, le "else" s'applique au "if b". Si cela amène des ambiguïtés, elles peuvent être résolues en codant une instruction "else" "vide" : | |
| 59 | + | ``` | |
| 60 | + | if a then | |
| 61 | + | if b then | |
| 62 | + | begin | |
| 63 | + | (..) | |
| 64 | + | end | |
| 65 | + | else | |
| 66 | + | else | |
| 67 | + | begin | |
| 68 | + | (..) | |
| 69 | + | end | |
| 70 | + | ``` | |
| 71 | + | ||
| 72 | + | Avis du traducteur : pour éviter les ambiguïtés dans de tels cas, forcer l'emploi des _begin_ et _end_ pour bien délimiter les blocs. | |
| 73 | + | ||
| 74 | + | ||
| 75 | + | **Keywords:** [[Begin|begin]] — [[Do|do]] — [[Else|else]] — [[End|end]] — [[For|for]] — [[If|if]] — [[Repeat|repeat]] — [[Then|then]] — [[Until|until]] — [[While|while]] | |
| 76 | + | ||
| 77 | + | --- | |
| 78 | + | *From the Free Pascal wiki, page "Else/fr", as the Internet Archive kept it on 2025-02-18. 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).* |
