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 the control expression (discriminator) in the head with the branch labels (selectors). The number of branches is configurable (i.e. it depends on the number of selector lines you write into the text field, see below). 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 contrast to an IF statement, the controlling expression (discriminator) is not of Boolean type but typically yields an integral number or a character (in Structorizer it might also evaluate to a string). The selector values are then constants (literals) of that very data type to be checked against the actual value of the discriminator expression.

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

CASE statement insertion

Then you may press the toolbar button marked with a red box in the screenshot above, or you might select one of the menue entries "AddBeforeCASE statement" or "AddAfterCASE 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 expression result is to be equal to (a comma-separated list of selector values per line 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 result.) The label itself does not 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 in the diagrams):

Content of the CASE structure Generated diagram
CASE statement with default branch

CASE statement without default branch



CASE structure with multiple values per branch

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

After having filled in the text area of the element editor and committed, the result might look like this (you may now fill the different branches with suited elements from the toolbar):

Demo diagram after CASE statement insertion

Download the accomplished Demo.

The CASE structure is composed of

  • the head (comprising the triangular shape showing the discriminator 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 has been 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 will not 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.


  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.