Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

euphoria
communitycreator

What is short-circuiting in Euphoria?

NDUKWE CHIDERA K.

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

What is short-circuiting?

Loops and other flow control statements like:

  • if statement
  • while statement
  • loop until statement

These 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.

How to use an and in the condition expression

if 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.

Where an or is used to join two parts of a condition expression

if 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.

Procedures/functions used as a condition and joined by and operator

Y 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.

Procedures/functions used in loop condition and joined by or operator

Y 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 for and or true for or, 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:

Code

--declare variables
sequence y, x
atom z
--assign them value with logical opertors
y = 3 or {7,8,9}
x = 4 and {10,11,12}
z = 4 or 8
print(1,y)
puts(1,"\n")
print(1,x)
puts(1,"\n")
print(1,z)

Explanation

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.

Code

sequence mySeq
atom counter
mySeq = "Assign these tasks to all students"
-- find 'a' or 'A' in mySeq
counter = 1
while counter <= length(mySeq) and mySeq[counter] != 'a' and mySeq[counter] != 'A' do
counter += 1
print(1,counter)
end while

Explanation

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:

  • Lines 1 and 2: We declare variables mySeq and counter.
  • Line 4: This line is a comment.
  • Line 5: We initialize the counter variable.
  • Lines 6 to 9: We create a while loop block, which searches for the characters a and A in the sequence variable mySeq.

RELATED TAGS

euphoria
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring