Compound statements, in addition to making expressions easier to read and maintain, also make it easier to precisely control the flow of expressions. Rather than waiting for compound statements to run to their natural conclusion, you can prematurely end them using one of the following statements:
- Exit - Terminates the current statement and continues on from the point immediately after it.
- Stop - Terminates the current expression in its entirety.
When used in combination with logical branching such as JumpTo and in-line If statements, you can build conditional clauses into your expressions that result in early termination under specific conditions.
For example, consider the following While loop:
While:(GuessAge < 150)
If:(GuessAge = PersonAge)
AgeMessage:= "You are " & GuessAge & " years old!";
GuessAge:= GuessAge + 1;
AgeMessage:= "You don't look a day over 149!";
Each time the contents of the loop are executed, an expression tests whether the current value of GuessAge is equal to the value of PersonAge. If the two values match, a message stating the person's age will be assigned to AgeMessage.
If the Stop statement was not present, the While loop would continue to iterate until its end condition was met - specifically, that the guess reaches 150 - even if the person's actual age had been found before that point. Adding the Stop statement tells the expression parser to immediately cease all further expressions, not only halting the loop but also skipping the final AgeMessage assignment that is meant to occur only when the value of GuessAge reaches 150.
The following example uses two While loops in combination with Exit statements:
While:(GuessFirstNumber <= 10)
If:(GuessFirstNumber = FirstNumber)
GuessFirstNumber:= GuessFirstNumber + 1;
While:(GuessSecondNumber <= 10)
If:(GuessSecondNumber = SecondNumber)
GuessSecondNumber:= GuessSecondNumber + 1;
GuessResults:= "The first number is " & GuessFirstNumber & " and the second is " & GuessSecondNumber;
Whereas in the previous example, locating the target number caused the expression to end completely, this new example shows how Exit statements terminate the current compound structure - here, a While loop - without affecting the remainder of the expression. Each of the paired While loops shown above are designed to return a different target number; when the first target number is found, the first loop exits and allows the second loop to begin.
Stop and Exit statements each have a similar but quite distinct purpose. They can both be used in the same expression if needed, but you should always try to use the correct statement for the task. Using Stop in place of Exit might work with smaller expressions, but if you ever have to expand your code bear in mind the unintended side-effects the substitution may cause.