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

From Lazarus wiki
Jump to navigationJump to search
(Merged the old "case" with the new "CASE" site. (Case -> CASE redirection; changed CASE content.))
Line 4: Line 4:
  
 
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 <tt>b</tt> is <tt>1, 7, 2037,</tt> or <tt>5</tt>; and another way if otherwise. You could do it by:
<font color="#006699"><strong>if</strong></font> <font color="#000000"><strong>(</strong></font>b <font color="#000000"><strong>=</strong></font> <font color="#ff0000">1</font><font color="#000000"><strong>)</strong></font> <font color="#006699"><strong>or</strong></font> <font color="#000000"><strong>(</strong></font>b <font color="#000000"><strong>=</strong></font> <font color="#ff0000">7</font><font color="#000000"><strong>)</strong></font> <font color="#006699"><strong>or</strong></font> <font color="#000000"><strong>(</strong></font>b <font color="#000000"><strong>=</strong></font> <font color="#ff0000">2037</font><font color="#000000"><strong>)</strong></font> <font color="#006699"><strong>or</strong></font> <font color="#000000"><strong>(</strong></font>b <font color="#000000"><strong>=</strong></font> <font color="#ff0000">5</font><font color="#000000"><strong>)</strong></font> <font color="#006699"><strong>then</strong></font>
+
<delphi>
  Statement1
+
if (b = 1) or (b = 7) or (b = 2037) or (b = 5) then
<font color="#006699"><strong>else</strong></font>
+
  Statement1
  Statement2<font color="#000000"><strong>;</strong></font>
+
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 <tt>case</tt> statement:
 
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:
<font color="#006699"><strong>case</strong></font> b <font color="#006699"><strong>of</strong></font>
+
<delphi>
  <font color="#ff0000">1</font><font color="#000000"><strong>,</strong></font><font color="#ff0000">7</font><font color="#000000"><strong>,</strong></font><font color="#ff0000">2037</font><font color="#000000"><strong>,</strong></font><font color="#ff0000">5</font><font color="#000000"><strong>:</strong></font> Statement1<font color="#000000"><strong>;</strong></font>
+
case b of
  otherwise  Statement2
+
  1,7,2037,5: Statement1;
<font color="#006699"><strong>end</strong></font><font color="#000000"><strong>;</strong></font>
+
  otherwise  Statement2
 +
end;
 +
</delphi>
  
 
The general form of the <tt>case</tt> statement is:
 
The general form of the <tt>case</tt> statement is:
<font color="#006699"><strong>case</strong></font> selector <font color="#006699"><strong>of</strong></font>
+
<delphi>
    List1<font color="#000000"><strong>:</strong></font>   Statement1<font color="#000000"><strong>;</strong></font>
+
case selector of
    List2<font color="#000000"><strong>:</strong></font>   Statement2<font color="#000000"><strong>;</strong></font>
+
  List1:    Statement1;
    <font color="#000000"><strong>.</strong></font><font color="#000000"><strong>.</strong></font><font color="#000000"><strong>.</strong></font>
+
  List2:    Statement2;
    Listn<font color="#000000"><strong>:</strong></font>   Statementn<font color="#000000"><strong>;</strong></font>
+
  ...
    otherwise Statement
+
  Listn:    Statementn;
<font color="#006699"><strong>end</strong></font><font color="#000000"><strong>;</strong></font>
+
  otherwise Statement
 +
end;
 +
</delphi>
  
 
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>.
 
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>.

Revision as of 16:45, 5 January 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