Fermat
Search
K

If-else

If-else statement checks for a condition. If it's true, it evals the first branch, otherwise it evals the second - The second branch is optional.
if [condition]:
[body]
else:
[body]
end
Here, you can find an example that will print true since the if-clause is always true:
if true:
print true
else:
print false
end
Output:
> true
We also support different if conditions by using the else if statement after the if one. Importantly, if there is an else if statement, the last condition must be an else clause.
if [condition]:
[body]
else if [condition]:
[body]
else:
[body]
end
As an example:
if false:
print false
else if 4 is 4:
print "comparator"
else:
print "nothing"
end
Output:
> "comparator"
You can use a number or structure in the [condition] with the following boolean outcomes:
  • Number. false in case of the 0 number otherwise true.
  • Structure. false in case of an empty structure - e.g. a list without elements - otherwise true.
if 1:
print true
end
Output:
> true
Last modified 1yr ago