Skip to content

Latest commit

 

History

History
196 lines (108 loc) · 17 KB

conditional-execution.md

File metadata and controls

196 lines (108 loc) · 17 KB

Execution Flow | Conditional Execution

[ Draft ]

back

Contents

Introduction

Conditional execution is a common form of execution flow control, where the next step of a program is based on a decision. The If statement is the most common example of this.

The notations introduced here are just suggestions. They aim to demonstrate how Circular notation can be used for conditional execution statements, as if they were regular commands. The suggestions here might not be perfect, but it shows the principles, as a base for any variation of notation.

If

The If statement is argueably the most common form of conditional execution.

Then

The If Then statement takes a Boolean condition and a reference to the command to run when the Boolean is True.

In a diagram this looks as follows.

The circle is the Boolean condition. The square is the command to execute when the Boolean condition is True.

The Then command can be defined right inside the call to the If statement, but you can also define the Then outside the If with the aid of a reference line:

The Boolean condition is usually defined elsewhere as well, making the condition point to another symbol.

Else

The If Else statement takes a Boolean condition, a command reference to the command to run when the Boolean is True and one when it is False.

The circle is the Boolean condition. The square named Then is the command to execute when the Boolean condition is True. The square named Else is the command to execute when the Boolean condition is False. For the If Else statement, the Then and Else are also usually defined outside the If with the aid of a reference line reference:

And the condition is usually defined elsewhere as well:

The definition of the If execution flow command is part of a system module of execution flow command. The public elements of the definition look like this:

In the definition, the condition and the clauses are not filled in yet.

Else If

The Else If statement is a lot like the If statement, but then the Else has another If associated to it.

Below is an example of the diagrammatic expression of an Else If statement.

The diamond is the Else If command. It is an overload of the If command. The top circle inside the diamond is the condition of the first If. If the condition returns True, then the Then clause is executed. In the middle of the diamond there is a nonagon. Inside the nonagon any number of Else If's can be specified. An Else If object also has a condition and a Then clause. If the condition returns True then the Then clause is executed. If the condition is False, then the next Else If’s condition is evaluated. If none of the Else If’s conditions return True, then the Else clause is executed, which is visible at the bottom of the diamond. The Else clause is optional. If the Else clause is not used, it can be left out of the diagram.

The conditions and the clauses can all be references to something defined outside the diamond. The conditions and the clauses can also be filled in right inside the diamond.

The definition of the Else If command could become part of a system module of execution flow commands. The public elements of the definition would look like this:

Nothing is filled in yet for the condition, the Then clause or the Else clause, and there are no Cases defined yet. But a class for a Case is defined. The Case class defines a condition and a Then clause.

There is another, separate definition of the Else If command, that is the same as the other Else If command definition, except without an Else clause in it.

That way the notation for conditional execution statements could be implemented as a library just like any other command.

Select Case

The Select Case statement is a form of conditional execution. It is also sometimes called a switch statement. In this kind of statement the next step to take is selected out of a list of several options. A few different variations of notation will be discussed.

Select Case (exact value)

Concept

This article explains the form of Select Case where a variable is compared to different values, to choose the next step to take.

This form of Select Case compares a given variable with several different values. If the variable equals one of the values, the step associated with that value is executed. If the variable equals none of the values, an alternative command can be executed.

This type of Select Case only works with objects, that hold a binary value.

Select Case takes an object, that holds a binary value as the Variable of the comparison. Furthermore, Select Case defines a variable amount of Cases. Each Case-object contains a value to compare the variable to and a reference to the command to execute when the variable equals the value. Select Case can also take a reference to a command, that might be executed, when the variable matches none of the values. This alternative command is called the Else clause of the Select Case statement. The Else clause of the statement can be left out, if it is not required.

Diagram

Below is an example of the diagrammatic expression of a Select Case statement for comparing exact values.

The diamond is a call to the Select Case command. The circle inside the diamond has the title Select. It points to an object outside the command call. This might be the variable to which several values might be compared. In the middle of the diamond there is a nonagon, that represents the cases: values to which the variable might be compared. The nonagon can contain any number of cases. Each circle inside a nonagon is a Case. Each case defines a value, and the command to call, if the variable matches the value. In this example, there are two Cases. One Case has the value 1 and the other Case has the value 2. Each Case has a command associated to it. Those command references are pointing to clauses defined outside the diamond. At the bottom of the diamond there is also a command reference called Else. It points to a clause defined outside the diamond. The command pointed to might be called if the variable matches none of the values defined in the Cases. If the Else clause is not used, it can be left out of the call and then it might not be shown in the diagram.

The values for the cases were entered literally into the case. A value for a case can also be defined as a pointer to an object that holds the value. The same way, the variable could also have gotten an exact value, and not be a pointer to an object outside the diamond. The command references did not might point to something defined outside of the diamond either. The commands could have been defined right inside the diamond, but it often looks more intuitive to define clauses outside the diamond.

The definition of the Select Case execution flow commands is part of a system module of execution flow commands. The public elements of the definition look like this:

Nothing is filled in yet as the Select or Else, and there are no Cases defined, but a class for a Case is defined.

There is another, separate definition of the Select Case command for comparison of exact values, that is the same as the other Select Case command definition, except that it might not have an Else clause in it.

Select Case (split formula)

Concept

There are two forms of Select Case, as mentioned in the article Select Case. This article explains the form of Select Case where one half of a formula is combined with several other halves of the formula, to choose the next step to take.

This form of Select Case takes a fixed first part of a formula. Then it combines it to several other halves of the formula. If the complete formula returns the Boolean value True, the step associated with that other half of the formula is executed. If none of the resultant formulas renders the Boolean value of True, then an alternative step can be executed.

Half a formula can either be a value, or an operation for which one operand is yet to be filled in.
If the first half of a formula is a value, then the other halves of the formula need to be operations for which an operand is yet to be filled in. The first half of the formula might then be filled in as the operand missing in the other half of the formula.
If the first half of a formula is an operation for which one operand is yet to be filled in, then the other halves of the formula need to be values, to fill in as the operand.

It is not limited to just mathematical formulas. You can use any command for which a parameter is to yet to be filled in. This type of Select Case is not limited to objects, that hold binary data. This type of Select Case works with all kinds of objects. However, the result of the resultant formula has to be a Boolean value.

The implementation of the Select Case command one by one calculates the Boolean results of the resultant formulas. If the result of formula is True, then the command associated with that second half of the formula is called. If multiple resultant formulas return True, then all the associated commands are executed. If all resultant formulas were processed and none of the formulas returned True, then the alternative command is run.

Diagram

There are two forms of this statement: the first half of the formula is a value or the first half of the formula is an operation with an operand missing. These two forms have a different definition and look different in the diagram

Below is an example of the diagrammatic expression of a Select Case statement for split formulas, of which the first half of the formula is a value.

The diamond is a call to the Select Case command. The circle inside the diamond has the title Select. It points to an object outside the command call. This is called the variable. It is the value, that might be filled into the missing operands of the other halves of the formula. In the middle of the diamond there is a nonagon, that represents the cases: different operations into which the variable might be filled in. The nonagon can contain any number of cases. Each circle inside a nonagon is a Case. Each case defines a command for which the variable is filled in. Each case also defines the command to call when the result of the formula is True. In this example, there are two Cases. The other halves of the formula are not drawn out in full detail. That might obscure the picture in this demonstration. The literals of the half formulas are shown. The command definitions of the half formulas are not pointed out, and the build-up of the formula’s is not fully graphically drawn out with objects connected with operations, because that might obscure the picture of this demonstration, but they do belong in the diagram, though.
One Case is the half formula > 2 . X is to be filled in as the first operand of this formula, which might make the resultant formula X > 2. The second Case is the half formula = 10 – Y. X is to be filled in as the first half of the formula, which might make the resultant formula X = 10 – Y.
Each Case has a command associated to it. Those command references are pointing to clauses defined outside the diamond. When a resultant formula of the case returns True, then the command associated with the case might be executed. At the bottom of the diamond there is also a command reference called Else. It points to a clause defined outside the diamond. The command pointed to might be called if none of the formulas results in the Boolean value True. If the Else clause is not used, it can be left out of the call and then it might not be shown in the diagram.

Below is an example of the diagrammatic expression of a Select Case statement for split formulas, of which the first half of the formula is half a formula, and the second halves of the formula are values to be filled in as the missing operand in the first half of the formula.

The diamond is a call to the Select Case command. The square at the top of the diamond is the first half of the formula. It is a greater than operation. The first operand of the formula is already filled in with the object X, by having the parameter point out of the diamond to the object called X. The second parameter of the operation is yet to be filled in by the Cases of the Select Case statement. In the middle of the diamond there is a nonagon, that represents the cases: different values to be filled in as the missing operand of the formula. The nonagon can contain any number of cases. Each circle inside a nonagon is a Case. Each case defines a value to be filled in as the missing operand of the formula. Each case also defines the command to call when the result of the formula is True. In this example, there are two Cases. One Case is the value 2. This value might be filled in as the missing operand of the formula. This makes the resultant formula X > 2. The second Case is not a fixed value, but points to the variable Y, which is defined outside the diamond. Y is filled in as the missing operand of the formula. This makes the resultant formula X > Y.
Each Case has a command associated to it. Those command references are pointing to clauses defined outside the diamond. Every command for which the resultant formula returns True is executed. At the bottom of the diamond there is also a command reference called Else. It points to a clause defined outside the diamond. The command pointed to might be called if none of the formulas results in the Boolean value True. If the Else clause is not used, it can be left out of the call and then it might not be shown in the diagram.

In the examples above, each value, that was literally filled in, could also have been a pointer to something remote. Conversely, everything that was a pointer to something outside the diamond, could also have been defined directly inside the diamond.

The definition of the Select Case execution flow commands is part of a system module of execution flow commands. The public elements of the definition for a value as the first part of the formula looks like this:

Nothing is filled in yet as the Select or Else, and there are no Cases defined, but a class for a Case is defined. There is also a definition without an Else clause in it.

The public elements of the definition for half a formula as the first part of the formula looks like this:

Nothing is filled in yet as the Select or Else, and there are no Cases defined, but a class for a Case is defined.

There is another, separate definition of the Select Case command for formulas, that is the same as the other Select Case command definition, except that it might not have an Else clause in it.

Outtakes

<< implementation >>

The implementation of the If command is quite simple. It simply calls a few machine instructions to start the right command, based on whether the Boolean is True or False.

The implementation of the Else If command is quite simple. It simply calls a few machine instructions to start a command, based on whether a Boolean value is True or False.

The implementation of the Select Case command is not too complicated. It simply calls a few machine instructions to compare a variable to a value and to start the right command when a match is found.