1. Home
  2. CASE directive

CASE directive

Introduces a “multiple-selection” control structure.

No options

Parameter

    expression Expression which is evaluated to an integer, indicating which set of statements to execute

Description

A multiple-selection control structure consists of several alternative blocks of statements. The first of these is introduced by a CASE statement. This has a single parameter, which is an expression that must yield a single number. Subsequent blocks are each introduced by an OR statement. There can then be a final block, introduced by an ELSE statement. The whole structure is terminated by an ENDCASE statement. Thus the general form is: first

CASE expression

statements

then either none, one or several blocks of statements of the form

OR

statements

then, if required, a block of the form

ELSE

statements

and finally the statement

ENDCASE

Genstat rounds the expression in the CASE expression to the nearest integer, k say, and then executes the kth block of statements. If there is no kth block present (as for example if k is negative) the block of statements following the ELSE statement is executed, if there is such a block; otherwise an error diagnostic is given.

This example prints the salient details about each day in the song The twelve days of Christmas. The scalar Day indicates which day it is.

CASE Day

  PRINT 'a partridge in a pear tree'

OR

  PRINT 'two turtle doves and a partridge in a pear tree'

OR

  PRINT 'three French hens, two turtle doves\

    and a partridge in a pear tree'

OR

  PRINT 'four calling birds, three French hens ...'

OR

  PRINT 'five gold rings ...'

OR

  PRINT 'six geese a-laying ...'

OR

  PRINT 'seven swans a-swimming ...'

OR

  PRINT 'eight maids a-milking ...'

OR

  PRINT 'nine drummers drumming ...'

OR

  PRINT 'ten pipers piping ...'

OR

  PRINT 'eleven ladies dancing ...'

OR

  PRINT 'twelve lords a-leaping ...'

ELSE

  PRINT 'sorry, no delivery today'

ENDCASE

CASE statements can be nested to any depth.

Options: none.

Parameter: unnamed.

See also

Directives: OR, ELSE, ENDCASE, EXIT, IF, FOR, CALCULATE.

Commands for: Program control.

Updated on March 8, 2019

Was this article helpful?