CASE/es: what changed
Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.
Title: Case/es
| Old | New | Line | |
|---|---|---|---|
| 1 | + | **Case** abre una sentencia "case". La sentencia "case" compara el valor de una expresión ordinal con cada uno de los selectores que contiene, pudiendo tratarse de selectores de tipo [[Const|constante]], un subrango, o una lista de ellos separados por [[Comma|comas]]. El campo del selector se encuentra separado del campo de acción por \[\[Colon\] \| dos puntos ":"\]. | |
| 2 | + | ||
| 3 | + | La sentencia "case" incluye [[Reserved word|las palabras reservadas]] [[Of]] y [[End]] . algunas veces [[Else]], también. | |
| 4 | + | ||
| 5 | + | ``` pascal | |
| 6 | + | ||
| 7 | + | case place of | |
| 8 | + | 1: ShowMessage('Medalla de oro'); | |
| 9 | + | 2: ShowMessage('Medalla de plata'); | |
| 10 | + | 3: ShowMessage('Medalla de bronce'); | |
| 11 | + | else ShowMessage('Mejor suerte la siguiente vez'); | |
| 12 | + | end; | |
| 13 | + | ``` | |
| 14 | + | ||
| 15 | + | ### QueEsCaracter | |
| 16 | + | ||
| 17 | + | ``` pascal | |
| 18 | + | ||
| 19 | + | function QueEsCaracter( c:char ):string; { recibe un parámetro "char" o carácter | |
| 20 | + | { y retorna un "string" o cadena } | |
| 21 | + | var | |
| 22 | + | s : string; | |
| 23 | + | begin | |
| 24 | + | s := ''; | |
| 25 | + | case c of | |
| 26 | + | '0' .. '9' : s := 'digito (0-9)'; | |
| 27 | + | 'a' .. 'z' : s := 'letras minúsculas(a-z)'; | |
| 28 | + | 'A' .. 'Z' : s := 'letras mayúsculas (A-Z)'; | |
| 29 | + | '+' , '-' : s := 'signo (+ o -)'; | |
| 30 | + | end; | |
| 31 | + | result := s; | |
| 32 | + | end; | |
| 33 | + | ``` | |
| 34 | + | ||
| 35 | + | ## Variante Record | |
| 36 | + | ||
| 37 | + | La palabra reservada **Case** se utiliza también en la declaración de los [[Record|Registros variantes]]. Un registro variante se conoce también cómo "unión etiquetada". | |
| 38 | + | ||
| 39 | + | ``` pascal | |
| 40 | + | ||
| 41 | + | type | |
| 42 | + | ||
| 43 | + | Escala_Kelvin = 223 .. 323; | |
| 44 | + | Escala_Celsius = -50 .. 50; | |
| 45 | + | ||
| 46 | + | Escala_Temperatura = ( celsius, kelvin ) ; | |
| 47 | + | Temperatura = record | |
| 48 | + | case escala : Escala_Temperatura of | |
| 49 | + | celsius : (valor_celcius : Escala_Celsius); | |
| 50 | + | kelvin : (valor_kelvin : Escala_Kelvin); | |
| 51 | + | end; | |
| 52 | + | ``` | |
| 53 | + | ||
| 54 | + | ## Usando Sets (conjuntos) | |
| 55 | + | ||
| 56 | + | Si te gustan los conjuntos con Sets, este ejemplo es muy simple: | |
| 57 | + | ||
| 58 | + | ``` pascal | |
| 59 | + | type | |
| 60 | + | medallas = (Oro, Plata, Bronce); // en caso que quieras que Oro inicie en 1, ponlo así (Oro=1, ...pero | |
| 61 | + | {[ ... ]} | |
| 62 | + | const | |
| 63 | + | SMedallas: array [medallas] of string[6] = ('Oro', 'Plata', 'Bronce'); // en [medallas], debes cambiarlo a [Oro..Bronce] ;) | |
| 64 | + | ||
| 65 | + | var | |
| 66 | + | lugar: integer; /// lugar inicia en 0; | |
| 67 | + | M: medallas; | |
| 68 | + | {[ ... ]} | |
| 69 | + | M := medallas(lugar); | |
| 70 | + | case M of | |
| 71 | + | Oro: ShowMessage('Ganaste la de ' + SMedallas[M]); | |
| 72 | + | Plata: ShowMessage('Ganaste la de ' + SMedallas[M]); | |
| 73 | + | Bronce: ShowMessage('Ganaste la de ' + SMedallas[M]); | |
| 74 | + | else | |
| 75 | + | ShowMessage('Mejor suerte la próxima! :)'); | |
| 76 | + | end; | |
| 77 | + | ``` | |
| 78 | + | ||
| 79 | + | Declaro que este código es implementación propia no sacada de ningún libro, por tanto creo tener derecho a poner por lo menos un punto para commentarios a edgarrod71@gmail.com. | |
| 80 | + | ||
| 81 | + | Otro ejemplo sin el arreglo SMedallas... | |
| 82 | + | ||
| 83 | + | ``` pascal | |
| 84 | + | type | |
| 85 | + | medallas = (Oro, Plata, Bronce); | |
| 86 | + | {[ ... ]} | |
| 87 | + | var | |
| 88 | + | lugar: integer; /// lugar inicia en 0; | |
| 89 | + | M: medalla; | |
| 90 | + | S: string; | |
| 91 | + | {[ ... ]} | |
| 92 | + | M := medalla(lugar); | |
| 93 | + | case M of | |
| 94 | + | Oro: writeStr(S, M); | |
| 95 | + | Plata: writeStr(S, M); | |
| 96 | + | Bronce: writeStr(S, M); | |
| 97 | + | else | |
| 98 | + | S := 'Mejor suerte la próxima! ;)'; | |
| 99 | + | end; | |
| 100 | + | ShowMessage(S); | |
| 101 | + | ``` | |
| 102 | + | ||
| 103 | + | ## Leer más | |
| 104 | + | ||
| 105 | + | - [The Case statement](http://www.freepascal.org/docs-html/ref/refsu40.html#x110-11700010.2.2) | |
| 106 | + | - [Record types](http://www.freepascal.org/docs-html/ref/refsu15.html#x38-450003.3.2) | |
| 107 | + | - [Tao Yue: Learn Pascal!](http://lazarus-ccr.sourceforge.net/pascal/pas3cb.html) Case | |
| 108 | + | - [Video: Free Pascal Tutorial 8 - Case Statements](http://www.youtube.com/watch?v=pMr2xtUu3x0) | |
| 109 | + | - [[Dialog Examples|ShowMessage]] | |
| 110 | + | - [[Type]] | |
| 111 | + | ||
| 112 | + | --- | |
| 113 | + | *From the Free Pascal wiki, page "Case/es", as it stood on 2020-01-24 after a change by Bart, 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).* | |
| 114 | + | Category: Castellano | |
| 115 | + | Category: Español |
