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

From Lazarus wiki
Jump to navigationJump to search
Line 1: Line 1:
 
{{CASE}}
 
{{CASE}}
  
3Cb - CASE (auteur: Tao Yue, état: traduit)
+
3Cb - CASE (author: Tao Yue, state: changed)
  
Case ouvre une instruction case. L'instruction <tt>case</tt> compare la valeur d'une expression ordinale à chaque sélecteur, qui peuvent être une [[Const/fr|constante]], une sous-plage ou une liste de celles-ci séparées par des [[Comma/fr|virgules]]. Le champ sélecteur est séparée de l'action (instructions) par [[Colon/fr|deux-points (:)]].
+
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]].
  
Supposez que vous voulûmes vous brancher sur une voie si <tt>b</tt> vaut <tt>1, 7, 2037</tt> ou <tt>5</tt> et sur une autre sinon. Vous pourriez faire comme cela:
+
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:
 
<syntaxhighlight>
 
<syntaxhighlight>
 
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
   Instruction1
+
   Statement1
 
else
 
else
   Instruction2;
+
   Statement2;
 
</syntaxhighlight>
 
</syntaxhighlight>
  
Mais dans ce cas, il pourrait être plus simple de lister les nombres pour lesquels vous voulez que <tt>Instruction1</tt> s'exécute. Vous ferez alors cela  avec une instruction <tt>case</tt>:
+
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 <tt>case</tt> statement:
 
<syntaxhighlight>
 
<syntaxhighlight>
 
case b of
 
case b of
   1,7,2037,5: Instruction1;
+
   1,7,2037,5: Statement1;
   otherwise  Instruction2
+
   otherwise  Statement2
 
end;
 
end;
 
</syntaxhighlight>
 
</syntaxhighlight>
  
La forme générale de l'instruction <tt>case</tt> est:
+
The general form of the <tt>case</tt> statement is:
 
<syntaxhighlight>
 
<syntaxhighlight>
case sélecteur of
+
case selector of
   Liste1:   Instruction1;
+
   List1:   Statement1;
   Liste2:   Instruction2;
+
   List2:   Statement2;
 
   ...
 
   ...
   Listen:   Instructionn;
+
   Listn:   Statementn;
   otherwise Instruction
+
   otherwise Statement
 
end;
 
end;
 
</syntaxhighlight>
 
</syntaxhighlight>
  
La partie <tt>otherwise</tt> est facultative. Quand elle est disponible, elle diffère d'un compilateur à un autre. Dans plusieurs compilateurs, vous avez le mot <tt>else</tt> au lieu de <tt>otherwise</tt>.
+
The <tt>otherwise</tt> part is optional. When available, it differs from compiler to compiler. In many compilers, you use the word <tt>else</tt> instead of <tt>otherwise</tt>.
  
sélecteur est une variable d'un type de donnée ordinal. Vous ne pouvez pas utiliser de réels (ni de chaînes de caractères)!
+
selector is any variable of an ordinal data type. You may not use reals!
  
Observez que les listes consistent en valeurs littérales. A savoir, vous devez employer des constanted ou des valeurs en dur mais pas des variables.
+
Note that the lists must consist of literal values. That is, you must use constants or hard-coded values -- you cannot use variables.
  
 
{|style=color-backgroud="white" cellspacing="20"
 
{|style=color-backgroud="white" cellspacing="20"
|[[IF/fr|précédent]]   
+
|[[IF|previous]]   
|[[Contents/fr|table des matières]]  
+
|[[Contents|contents]]  
|[[FOR..DO/fr|suivant]]
+
|[[FOR..DO|next]]
 
|}
 
|}
  

Revision as of 22:03, 19 March 2014

български (bg) English (en) español (es) français (fr) 日本語 (ja) 中文(中国大陆)‎ (zh_CN)

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:

if (b = 1) or (b = 7) or (b = 2037) or (b = 5) then
  Statement1
else
  Statement2;

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:

case b of
  1,7,2037,5: Statement1;
  otherwise   Statement2
end;

The general form of the case statement is:

case selector of
  List1:    Statement1;
  List2:    Statement2;
  ...
  Listn:    Statementn;
  otherwise Statement
end;

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