Loops and other flow control statements like:
if
statementwhile
statementloop until
statementThese statements need to evaluate a particular condition before it performs some action based on the output of the evaluation. Some of these conditions use the logical operators:
AND
OR
Note: If included in the conditions of these loops, these operators will cause what is known as a
short circuit
.
Short-circuiting is a process that the Euphoria parser uses to evaluate loop conditions that contain and
or or
by ignoring the second side of the condition expression after evaluating the first part.
The Euphoria parser ends the evaluation of a loop or control statement condition, using AND
/OR
logical operators if the first part of the condition evaluates as true
for or
or false
for an and
.
Let’s see some examples where if
, while
, and loop until
statements use and
or or
in their condition, a short circuit will be used to evaluate the condition.
and
in the condition expressionif y = x and z < x
In plain sight, the script parser is supposed to check if both conditions (y = x, z < x)
returned as false
before taking the next action. But the Euphoria parser performs a short circuit evaluation by default in this case by ending the loop condition evaluation. If y = x
evaluates as false
because it assumes that the entire expression is false
. In the short circuit, the other part of the condition z < x
is not checked if the first part y = x
evaluates as false
.
or
is used to join two parts of a condition expressionif y > x or z < x
The parser will assume the whole expression as true
if y > x
evaluates as true
.
This same situation is true
for conditions that have procedures and functions to evaluate. Only those functions whose return value from the condition used in the code will cause an error because they were not evaluated.
and
operatorY and Z
If Y
is a function or an expression that evaluates as false
the whole condition is considered as false
without evaluating Z
. Suppose the evaluation of Z
is to return a value used in the code to be executed. This causes an error during run time. The same also applies to the next case.
or
operatorY or Z
If Y
is a function or an expression evaluates to true
the whole expression is considered true
both the evaluation of Z
, which may not be true
.
Note: Short circuits have a generalization approach of the parser. It simply generalizes for both sides of the condition depending on the outcome of evaluating the first part. If the first part evaluates to
false
forand
ortrue
foror
, it simply skips the evaluation of the other part of the condition expression.
The evaluation of and
and or
using the short circuit technique by Euphoria script parser only happens inside decision-making expressions. These are found in the if
statement, while
statement, and the loop until
statement. It is not used in other contexts. For example, the assignment statement:
Y = 4 and {7,8,9}
and
Y = 4 and {7,8,9}
In both cases, the parser will assume that we wish to get a sequence variable Y
which will have a length the same as the greatest length among the two presented choices filled with 1
's. The output will always be 1
if this compares two atoms.
Let’s look at the code below:
--declare variablessequence y, xatom z--assign them value with logical opertorsy = 3 or {7,8,9}x = 4 and {10,11,12}z = 4 or 8print(1,y)puts(1,"\n")print(1,x)puts(1,"\n")print(1,z)
In the code above, the short circuit does not affect the assignment statement. If it did, the output of expression at line 5 would be y
, set to the value 3
.
sequence mySeqatom countermySeq = "Assign these tasks to all students"-- find 'a' or 'A' in mySeqcounter = 1while counter <= length(mySeq) and mySeq[counter] != 'a' and mySeq[counter] != 'A' docounter += 1print(1,counter)end while
If the short circuit technique is not employed to evaluate the condition in this loop, the variable counter
eventually becomes greater than length(mySeq)
. It happened in the first iteration. In this case, a subscript out-of-bounds error occurs when mySeq[counter]
is evaluated on the final iteration.
But with short-circuiting, the loop condition evaluation terminates immediately when counter <= length(mySeq)
The first part of the condition becomes false
.
The evaluation of mySeq[counter] != 'a'
and mySeq[counter] != 'A'
will not be done by the Euphoria parser because of short circuit thereby averting the danger of No subscript
error occurring.
Let’s look at each line of the code:
mySeq
and counter
.counter
variable.while
loop block, which searches for the characters a
and A
in the sequence variable mySeq
.