S T R U C T O R I Z E R - User Guide
Elements > CASE statement

A CASE element (or "selection") is a multi-way forking of the control flow within an algorithm. The branch to choose is determined by a comparison of the value of a control expression (discriminator) with the branch labels (selectors). The number of branches is configurable. Usually, a CASE structure provides a "ragpicker" branch (called default, else, or otherwise branch) placed right-most in order to catch any discriminator value not matching any explicit selector.

In order to add a CASE element select the element it is to precede or to succeed.

CASE statement insertion

Then you may press one of the toolbar buttons marked with a red box in the screenshot above (for this example the right one was used in order to insert the CASE statement after the selected element), or you might select one of the menue entries "Add => Before => CASE statement" or "Add => After => CASE statement" in either the context menu or the "Diagram" menu. (Or you simply press one of the function keys <F10> or <Shift><F10>, depending on whether the CASE element is to be inserted after or before the currently selected element.) This will open the element editor for a new CASE statement (the default text occurring in the editor field can be modified in the Structure Preferences):

Filled-in CASE editor

The text to be entered into a CASE form is interpreted like this:

  • line 1: the discriminator expression being evaluated (in the example above variable A);
  • line 2..(n-1): the constant values the discriminator should match (a comma-separated list of selector values is possible) in order to enter the (i-1)th branch;
  • line n (the last line): the label for the "else" branch. (As outlined above, this is where the execution jumps into, when none of the selector constants from lines 2..(n-1) matches the discriminator.) The label itself doesn't matter, unless you name it "%" - which would suppress the default branch.

If you want to have a CASE statement without a default branch (= else part), simply write "%" into the last line. In that case, the last column will not be drawn.

The selector values to be matched against ought to be integral, character, or string constants (literals), see examples below (the CASE element head is coloured cyan blue in the diagrams):

Content of the CASE structure Generated diagram
A
1
3
5
sinon
CASE statement with default branch
A
1
3
5
%

CASE statement without default branch

month
1,3,5,7,8,10,12
4,6,9,11
2
default

 

CASE structure with multiple values per branch

(Only supported by Structorizer version 3.22-31 and later)

After having filled in and committed the element editor, the result might look like this:

Demo diagram after CASE statement insertion

Download the accomplished Demo.

The CASE structure is composed of

  • the head (comprising the triangular shape showing the discriminating choice expression and the trapezoid shapes containing the branch selectors) and
  • the respective branches, which are initially empty instructions (indicated by the ∅ symbols).

In order to insert elements to a branch select the respective branch and insert the elements needed in the usual way (see Diagram/Add element).

In order to append an element after the entire CASE statement select the head of the CASE structure first and then add the element to follow as usual (see Diagram/Add element).

To pass a CASE element means to evaluate the discriminating expression and to execute the branch the selector of which matches the discriminator value. After the branch is executed, the control flow passes to the next element appended below the entire CASE element - independent of the selected branch.

Note: On editing an existing CASE element (e.g. after double-clicking or pressing the <Enter> key while the CASE head is selected) you may insert or remove lines. But be aware that the instruction sequences of the branches themselves won't move when you e.g. insert new case lines close to top. They will stick to their original position (left-bound) rather than to their originally corresponding label. In the first example of the table above, inserting a new line "2" between "1" and "3" would result in the following situation where the red coloured branches are now associated to wrong selectors and will have to be moved rightwards by dragging them one by one or by a manual cut and paste sequence:

Branch association error after case line insertion

If you reduce the number of case lines, in contrast, then the supernumerous branches will automatically be deleted just from right to left - no matter which lines you removed. Hence if you had deleted the line "1" in the original example you would end up with all three remaining branches being wrongly associated (marked with red colour again):

Branch association error after case line deletion

So edit with care! If you want to avoid hassle then add new cases preferably near the end of the list (though the default case must always be the last one) and copy the contents of branches prone to vanish before you start to remove obsolete selector lines from the text.

Hints:

  1. You may decompose a CASE element into an equivalent set of nested IF elements simply by means of the transmutation wand Magic wand icon for transmutation. (But you may not transmute nested alternatives into a CASE element. The efforts to check the prerequisites for this conversion would be too expensive.)
  2. An option in Structure Preferences allows you to rotate atomic or collapsed branches in CASE elements with many branches in order to reduce width. See an example in Structure Preferences.