Else/es: what changed
Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.
Title: Else/es
| Old | New | Line | |
|---|---|---|---|
| 1 | + | [Else at Language Reference](http://www.freepascal.org/docs-html/ref/refsu51.html#x144-15400013.2.3) | |
| 2 | + | ||
| 3 | + | Else es una [[Keyword|keyword]] que estable que acción hacer si la condición evaluada es [[False|false]]. | |
| 4 | + | ||
| 5 | + | # [[If]] [[Then|then]] else | |
| 6 | + | ||
| 7 | + | ``` pascal | |
| 8 | + | if (condition) | |
| 9 | + | then true_statement | |
| 10 | + | else false_statement; | |
| 11 | + | ``` | |
| 12 | + | ||
| 13 | + | Se evalúa el valor de *condition*, si resulta ser verdadero (true), se ejecuta *true_statement*, caso contrario se ejecuta *false_statement*. Si el valor del condicional no arroja un valor booleano ocurrirá un error. | |
| 14 | + | ||
| 15 | + | ### Más sentencias en la sentencia "if then else" | |
| 16 | + | ||
| 17 | + | Si se necesitan dos o más sentencias para true_statement o false_statement, el grupo de sentencias deben ubicarse dentro de un [[Block]] [[Begin|begin]] ... [[End|end]]. | |
| 18 | + | ||
| 19 | + | ``` pascal | |
| 20 | + | if boolean_condition then | |
| 21 | + | begin | |
| 22 | + | statement_one; | |
| 23 | + | statement_two; | |
| 24 | + | end | |
| 25 | + | else | |
| 26 | + | begin | |
| 27 | + | statement_three; | |
| 28 | + | statement_four; | |
| 29 | + | end; | |
| 30 | + | ``` | |
| 31 | + | ||
| 32 | + | Normalmente la sentencia "else" es una excepción especial a la regla que establece que cada sentencia debe ser seguida por un punto y coma. Ni la palabra reservada "else" ni la sentencia que la precede inmediatamente deben ser seguidas por punto y coma. En el ejemplo precedente, la primer sentencia "end" no es seguida por punto y coma pero la última sí. | |
| 33 | + | ||
| 34 | + | Sin embargo, en el caso de sentencias "if" anidadas, si el *else* corresponde al if "interno", no debe haber un punto y coma antes del *else*, pero si el *else* corresponde hacia fuera del else, sí debe haber un punto y coma antes: | |
| 35 | + | ||
| 36 | + | ``` pascal | |
| 37 | + | if a then //comienza if a | |
| 38 | + | if b then //comienza if b (si a = True) | |
| 39 | + | begin | |
| 40 | + | (..) //bloque para if b = True | |
| 41 | + | end; //fin de if b | |
| 42 | + | else //else de if a porque lo precede un sentencia end finalizada en punto y coma | |
| 43 | + | begin | |
| 44 | + | (..) //bloque para if a = False | |
| 45 | + | end; | |
| 46 | + | ``` | |
| 47 | + | ||
| 48 | + | En este caso, "else" corresponde a "if a". | |
| 49 | + | ||
| 50 | + | ``` pascal | |
| 51 | + | if a then //comienza if a | |
| 52 | + | if b then //comienza if b (si a = True) | |
| 53 | + | begin | |
| 54 | + | (..) //bloque para if b = True | |
| 55 | + | end | |
| 56 | + | else //else de if b porque lo precede una sentencia que no finaliza en punto y coma | |
| 57 | + | begin | |
| 58 | + | (..) //bloque para if b = False | |
| 59 | + | end; | |
| 60 | + | ``` | |
| 61 | + | ||
| 62 | + | En este caso, el *else* corresponde a "if b". Si esto casusa una ambigüedad, se puede resolver con una sentencia "else" vacía: | |
| 63 | + | ||
| 64 | + | ``` pascal | |
| 65 | + | if a then //comienzo if a | |
| 66 | + | if b then //comienzo de if b (si a = True) | |
| 67 | + | begin | |
| 68 | + | (..) //bloque para if b = True | |
| 69 | + | end | |
| 70 | + | else //else "vacío" de if b | |
| 71 | + | else //else de if a (si a = false) | |
| 72 | + | begin | |
| 73 | + | (..) //bloque para if a = False | |
| 74 | + | end; | |
| 75 | + | ``` | |
| 76 | + | ||
| 77 | + | También se puede resolver utilizando bloques begin..end. | |
| 78 | + | ||
| 79 | + | ``` pascal | |
| 80 | + | if a then | |
| 81 | + | begin //comienzo if a (si a = True) | |
| 82 | + | if b then //comienzo de if b (si a = True) | |
| 83 | + | begin | |
| 84 | + | (..) //bloque para if b = True | |
| 85 | + | end; | |
| 86 | + | end //fin de if a (si a = True) | |
| 87 | + | else //else de if a (si a = false) | |
| 88 | + | begin | |
| 89 | + | (..) //bloque para if a = False | |
| 90 | + | end; | |
| 91 | + | ``` | |
| 92 | + | ||
| 93 | + | --- | |
| 94 | + | *From the Free Pascal wiki, page "Else/es", as it stood on 2020-01-14 after a change by Trev, 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).* | |
| 95 | + | Category: Control Structures/es | |
| 96 | + | Category: Pascal/es |
