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

From Lazarus wiki
Jump to navigationJump to search
m (Reverted edits by Arturom (Talk); changed back to last version by Kees)
m (Fixed syntax highlighting)
(10 intermediate revisions by 6 users not shown)
Line 1: Line 1:
 +
{{CASE}}
 +
{{TYNavigator|IF|FOR..DO}}
 +
 
3Cb - CASE (author: Tao Yue, state: changed)
 
3Cb - CASE (author: Tao Yue, state: changed)
  
Line 4: Line 7:
  
 
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:
<delphi>
+
 
 +
<syntaxhighlight lang="pascal">
 
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
 
   Statement1
 
   Statement1
 
else
 
else
 
   Statement2;
 
   Statement2;
</delphi>
+
</syntaxhighlight>
  
 
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:
<delphi>
+
 
 +
<syntaxhighlight lang="pascal">
 
case b of
 
case b of
 
   1,7,2037,5: Statement1;
 
   1,7,2037,5: Statement1;
 
   otherwise  Statement2
 
   otherwise  Statement2
 
end;
 
end;
</delphi>
+
</syntaxhighlight>
  
 
The general form of the <tt>case</tt> statement is:
 
The general form of the <tt>case</tt> statement is:
<delphi>
+
 
 +
<syntaxhighlight lang="pascal">
 
case selector of
 
case selector of
 
   List1:    Statement1;
 
   List1:    Statement1;
Line 28: Line 34:
 
   otherwise Statement
 
   otherwise Statement
 
end;
 
end;
</delphi>
+
</syntaxhighlight>
  
 
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>.
Line 36: Line 42:
 
Note that the lists must consist of literal values. That is, you must use constants or hard-coded values -- you cannot use 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"
+
{{TYNavigator|IF|FOR..DO}}
|[[IF|previous]] 
 
|[[Contents|contents]]
 
|[[FOR..DO|next]]
 
|}
 

Revision as of 02:33, 7 February 2020

български (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.

 ◄   ▲   ►