Sign in Join
Wiki copytemporary

Basic Pascal Tutorial/Chapter 3/CASE

Sent here from CASE.

English · Español · Français · Български · 中文(中国) · 日本語

◄ ▲ ►

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

Case opens a case statement. The case statement compares the value of an ordinal expression to each selector, which can be a constant, a subrange, or a list of selectors 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:

pascal
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:

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

The general form of the case statement is:

pascal
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. Many compilers 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.

◄ ▲ ►

From the Free Pascal wiki, page "Basic Pascal Tutorial/Chapter 3/CASE", as the Internet Archive kept it on 2025-04-01, last changed on 2025-03-01. Copyright stays with the author who wrote it, according to the old wiki's Licensing page. If you have concerns about this material, message the admin.

Revision 2, last changed by WikiArchive .

Edited on wiki.freepascal.org by 12 people, 2007–2022

From the official wiki's backup of 26 May 2024: who edited this page there, and when. Edits made there after that date were not recorded, and the earlier versions are not shown here; the main page says why.

WhenWhoWhat changed
KeesNew page: 3Cb - CASE 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</s...
Kees
Kees
IonicMerged the old "case" with the new "CASE" site. (Case -> CASE redirection; changed CASE content.)
Kees
Arturom
Arturom
Arturom
JonasReverted edits by Arturom (Talk); changed back to last version by Kees (minor)
VincentText replace - "delphi>" to "syntaxhighlight>" (minor)
Olaf (minor)
Jwdietrich
Jwdietrich
Swen (minor)
E-ric
E-ric
E-ric
FTurtle
FTurtle
TrevFixed syntax highlighting (minor)
Kai BurghardtKai Burghardt moved page CASE to Basic Pascal Tutorial/Chapter 3/CASE: tidy up main name space: create subpage hierarchy for basic Pascal tutorial [cf. [[Special: PermaLink/149778#Cluttering of main name... (minor)
Kai Burghardtbypass redirects [cf. discussion]
Kai Burghardtbypass language bar/categorization template redirect [cf. discussion] (minor)

Talk about this page

Nobody has opened a bench for this page yet. Sign in to open one.