Difference between revisions of "Basic Pascal Tutorial/Chapter 3/CASE"

From Free Pascal wiki
Jump to navigationJump to search
m (Reverted edits by Arturom (Talk); changed back to last version by Kees)
Line 1: Line 1:
{{Traducción|ci=es|art=CASE}}
 
 
3Cb - CASE (author: Tao Yue, state: changed)
 
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.
+
Case opens a case statement. The case statement compares the value of ordinal expression to each selector, which can be a [[Const|constant]], a subrange, or a list of them separated by [[Comma|commas]]. Selector field separated to action field by [[Colon]].
  
<!--
+
Suppose you wanted to branch one way if <tt>b</tt> is <tt>1, 7, 2037,</tt> or <tt>5</tt>; and another way if otherwise. You could do it by:
Suppose you wanted to branch one way if b is 1, 7, 2037, or 5; and another way if otherwise. You could do it by:
 
-->
 
 
 
Suponga que desea ejecutar una acción si <tt>b</tt> es <tt>1, 7, 2037,</tt> ó <tt>5</tt> u otra acción en caso contrario. Puede hacerlo de esta forma:
 
 
<delphi>
 
<delphi>
 
if (b = 1) or (b = 7) or (b = 2037) or (b = 5) then
 
if (b = 1) or (b = 7) or (b = 2037) or (b = 5) then

Revision as of 16:31, 4 February 2010

3Cb - CASE (author: Tao Yue, state: changed)

Case opens a case statement. The case statement compares the value of ordinal expression to each selector, which can be a constant, a subrange, or a list of them separated by commas. Selector field separated to action field by Colon.

Suppose you wanted to branch one way if b is 1, 7, 2037, or 5; and another way if otherwise. You could do it by: <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