Basic Pascal Tutorial/Chapter 3/CASE

From Free Pascal wiki
Revision as of 13:29, 1 February 2010 by Arturom (talk | contribs)
Jump to navigationJump to search

Template:Traducción 3Cb - CASE (author: Tao Yue, state: changed)

Case abre un bucle. La sentencia case hace una comparación del valor ordinal de la expresión con cada uno de los valores incluidos en el bucle puede ser una constante, un rango o una lista de valores separados por coma.


Suponga que desea ejecutar una acción si b es 1, 7, 2037, ó 5 u otra acción en caso contrario. Puede hacerlo de esta forma: <delphi> if (b = 1) or (b = 7) or (b = 2037) or (b = 5) then

 Statement1

else

 Statement2;

</delphi>

But in this case, it would be simpler to list the numbers for which you want Statement1 to execute. You would do this with a case statement: <delphi> case b of

 1,7,2037,5: Statement1;
 otherwise   Statement2

end; </delphi>

The general form of the case statement is: <delphi> case selector of

 List1:    Statement1;
 List2:    Statement2;
 ...
 Listn:    Statementn;
 otherwise Statement

end; </delphi>

The otherwise part is optional. When available, it differs from compiler to compiler. In many compilers, you use the word else instead of otherwise.

selector is any variable of an ordinal data type. You may not use reals!

Note that the lists must consist of literal values. That is, you must use constants or hard-coded values -- you cannot use variables.

previous contents next