To use this website fully, you first need to accept the use of cookies. By agreeing to the use of cookies you consent to the use of functional cookies. For more information read this page.

Official ZPE Programming Environment documentationConditions

YASS
$cond1 = $a || $b && $c
$cond2 = $a OR $b AND $c
  

As of version 1.4.3 the ternary operator can be used after a condition. This powerful operator is used to specify a return value on both true and false values:

YASS
$x = 10 > 20 || 20 > 10 ? "Correct" : "Wrong"
  

On this occasion, the result of $x will be "Correct".

Version 1.7.10 also adds the ability to replace the colon in the ternary operation with the keyword otherwise, so the above could be rewritten as:

YASS
$x = 10 > 20 || 20 > 10 ? "Correct" otherwise "Wrong"
  

As well as this, you can call functions or whatever else you'd like to do, but they must be a single clause statement:

YASS
if(10 > 20 || 20 < 10 ? print("Yes") : print("No"));
  

Wrapping a condition in a single line if statement ensures that the code is concise and short. What happens in the above statement is that the evaluation on the first part is done and then the value false comes back. As a result, the ternary statement returns a function. The function print is given the value "No" and it is then called. Since the if statement does not return anything, the return value is not given back to anything.

On a personal note, I would not recommend this function and would stick to full if statements as opposed to this function. The ternary operator can be used successfully to make a single line greatest common divisor function:

YASS
function gcd($a, $b)

  return $b < 1 ? return $a : return gcd($b, $a - ($b * floor($a / $b)))

end function
        

Notes

ZPE 1.7.4 re-introduces short-circuit evaluation of conditions within the language. This was something that was first introduced back in ZPE 1.4.x but later removed due to a major bug in the code.

Short-circuit evaluation is designed to allow both more flexibility in the runtime and better performance. For example with short-circuit evaluation, the following would work and without it would crash:

YASS
$j = -1
$l = [3, 5, 7]

while ($j < list_get_length($l))
  print($j)
  if($j >= 0 && $l[$j] > 5)
    print($l[$j])
  end if
  $j++
end while
        

The reason it would crash without short-circuit evaluation is down to accessing an index that is less than 0 ($j is less than 0) but in short-circuit evaluation because the first part of the statement evaluates to false, it will not proceed to check the second part. This is common in programming languages across the board.

Disabling short-circuit evaluation

Short-circuit evaluation can be disabled from the properties file. This allows testing without it. By default, it is enabled.

Comments

There are no comments on this page.

New comment
Reply
Provide feedback on this page
Comments are sent via email to me.