$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:
$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:
$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
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:
function gcd($a, $b) return $b < 1 ? return $a : return gcd($b, $a - ($b * floor($a / $b))) end function
ZPE 1.10.11+
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)
String comparison
String comparison has existed in ZPE for a while. ZPE's string comparison allows the comparison of two strings to see which comes first. This is useful for sorting algorithms. The following example shows this:
// Will print true print("5" > "4") // Will print false print("900" > "99")
But why will the first evaluate to true whereas the second to false? The answer is simple, ZPE is not performing numeric comparison, but rather checking to see which word would come first alphabetically: so 4 would come before 5, 900 would come before 99 because the first 0 in 900 would come before the second 9 in 99.
Short-circuit evaluation
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.