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/YASS documentationIf statements

If statements do one thing if the condition is satisified, or do another thing if it is not. They are the most basic form of conditional checking and flow control.

YASS
if($a < 10)
  //Do something
elseif ($b > $a && $b < 15)
  //Do something else if something
else
  //Do something else
end if

//Alternatively, as of version 1.3.4.90, if statements can be a single line, terminated with a semi-colon
if ($m == 10) print("It does indeed");
  

Version 1.6.6 added an additional syntax for the if statement that uses the then keyword, officially known as if...then statements:

YASS
if $a < 10 then
  //Do something

if $a < 10 then print("It does indeed")
  

The then statement is different from a standard if statement in that it can only have a single action inside the statement and does not end with either a brace or end if:

YASS
if $a < 10 then
  print("Hello 1")
print("Hello 2")
  

The "Hello 2" string is not in the if statement however, the "Hello 1" is.

Prior to version 1.7.11, the then keyword was simply syntactic sugar and did not construct a action line if statement.

Else statements can also be added to if...then statements:

YASS
if $a < 10 then
  print("Hello 1")
else
  print("Hello 2")
  

It should noted that these else statements must be a single expression like the if...then statement.

Below is an example of a single bit adder program created using binary logic in ZPE:

YASS
function adder($x, $y)
  $xor = xor($x, $y)
  $land =  $x and $y
  $carry = 0
  $val = 0

  if($xor)
    $val = 1
  else
    if($land)
      $val = 1
      $carry = 1
    end if
  end if

  return ["result" => $val, "carry" => $carry]
end function

function main()

  print(adder(1, 0))
  print(adder(0, 0))
  print(adder(1, 1))

end function

For a more advanced version of this code, look at the documentation for the xor function.

else if

Version 1.6.7 also added the new else if to compliment the original elseif - either was valid until version 1.9.4:

YASS
if ($a < 10)
  //Do something
elseif ($b > $a && $b < 15)
  //Do something else if something
else if ($c > $a && $c < 15)
  //Do something else if something
else
  //Do something else
end if
  
The else if syntax was removed in ZPE 1.9.4. It was later discovered to cause serious issues when nested if statements are used.
Comments

There are no comments on this page.

New comment
Reply
Feedback
Comments are sent via email to me.