Basic Pascal Tutorial/Chapter 3/CASE/bg

From Lazarus wiki
Revision as of 22:31, 21 April 2021 by Alpinistbg (talk | contribs) (Created page with "{{CASE}} {{TYNavigator|IF|FOR..DO}} 3Cb - CASE (author: Tao Yue, state: changed) Операторът за разглеждане на случаи <tt>case</tt> сравн...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigationJump to search

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

 ◄   ▲   ► 

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

Операторът за разглеждане на случаи case сравнява стойността на редови израз с всеки селектор. Това може да бъде константа, 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.

 ◄   ▲   ►