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

An element of type IF statement represents an alternative in the control flow. It is used at a decision point where the algorithm must take one of two different ways. The IF statement comprises the condition and both emerging paths. It is important to understand, however, that both branches join together again at the bottom of the element, i.e. after having passed the selected branch (whichever of them it is), the control flow will continue below the IF element in either case.

The text of the IF element is to contain the logical condition. It may be represented by any Boolean expression, i.e. an expression that evaluates either to true or to false. If the value computes to true then the left branch will be taken, otherwise the right branch.

Please note:
The labels for the "TRUE" and "FALSE" branch in the graphical presentation can be modified in Structure Preferences (menu item "Preferences => Structures").

But let us explain it with a simple example: imagine you have the task to find out whether an input value is the greater than zero. This obviously requires first an input instruction for a variable, say A, and then a decision - you will have to compare the value of the variable with 0 and generate a different text output in both cases. This simple algorithm might look like this, where the entire IF statement including the branches is marked yellow (note that if you select an IF statement then only its "head", i.e. the upper rectangle consisting of the three triangles containing the condition and the branch labels will be highlighted, see step 5 below):

Demo diagram with an if statement.

If the condition comes true then the OUTPUT instruction in the left-hand branch (labelled TRUE) will be executed, otherwise the one in the right-hand branch (labelled FALSE).

No how do you add an IF statement to your diagram? Assume the program is already named and the input instruction already been placed (see Instruction):

1. Select the element where you want to append an IF statement, then click on the IF statement symbol in the toolbar:

Select the IF-Statement
 
2. Insert the condition

When the element editor opens then the text field will contain some default string (which is configurable in the Structure Preferences). Replace this default string by the actual condition. This may be a comparison, a variable with Boolean content, or several conditions combined by logical operators.

Accepted logical operators are and (or &&), or (or ||), not (or !), and xor (or ^), where the latter stands for exclusive or (A xor B is true if exactly one of the conditions A or B is true). In our case the condition is A > 0:

Edit the IF-Statement If one of the two branches is meant to remain empty (i.e. you are just going to create a "conditioned instruction") then formulate the condition in such a way that the non-empty branch is the "TRUE" branch, which is always on the left-hand side. (If you accidently did otherwise you may simply have the branches swapped and the condition negated by means of transmutation.)

3. Add the elements to be executed depending on the condition, e.g. Instructions, to the respective branches (see Diagram/Add elements) by selecting the (empty) branch and pressing the toolbar button for the intended element type:

Add an instruction in the IF-Statement Fill in the elements according to the algorithm you have in mind (see above).

4. If you want to have more than one instruction executed in a branch then add them after or before the first one (i.e. make sure to have selected an element of the branch when inserting or appending another one):

Add an instruction after the first one in the IF-Statement

5. If you want to add further elements after the entire IF statement (remember that no matter, which branch was chosen and executed, the control flow will continue with the element following the IF statement) you must select its head first:

Add an instruction after the IF statement

6. This is what the algorithm would look like after the insertion of an unconditioned OUTPUT instruction after the IF statement (note that neither the INPUT instruction nor the now selected OUTPUT instruction are part of the alternative):

Completed IF statement

Download Demo

Each branch of an alternative my consist of a sequence of arbitrary elements, not only instructions,  i. e., of course alternatives may be nested:

Demo program with nested alternatives