$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
$x = 10 > 20 || 20 > 10 ? "Correct" : "Wrong"
On this occasion, the result of
$x will be
Version 1.7.10 also adds the ability to replace the colon in the ternary operation with the
otherwise, so the above could be rewritten as:
$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:
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
"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:
function gcd($a, $b) return $b < 1 ? return $a : return gcd($b, $a - ($b * floor($a / $b))) end function
One of the most common mistakes I see in early stage programmers is conditional expressions which rely on one variable or value being used over and over again but being ommitted. ZPE 1.10.11 now supports this and there is no performance penalty for using it:
print($x > 10 || > 20 && != 15)
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:
$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.