If/Else Statements
An if statement is a conditional statement that executes a block of code if a condition is truthy. In LemonScript, truthy is any value that is not null
or false
.
To write an if statement, you can use the if
keyword with this syntax:
if ([condition]) { [code]}
The code will execute if the condition is truthy. You can also add an else statement at the end which will execute if the condition is not truthy:
if ([condition]) { [code]} else { [code]}
Here's an example that uses the if statement:
var x = 1var y = 7
if (x < 2) y = 12
print(y) # 12
#
And, Or, and !In conditions, you can also use and
, or
, and !
to combine conditions. Let's suppose x
is truthy and y
is not truthy.
Here's a table showing what the condition would return when using these operators:
Example | Output | Explanation |
---|---|---|
x and y | false | This returns false because x is truthy, but y is not, and the condition needs both values to be truthy |
x or y | true | This returns true because the or conditional only needs one value to be truthy |
!x | false | This returns false because it flips the truthiness of the value |
!y | true | Flips the truthiness |
x and !y | true | This returns true because x is truthy, and the opposite truthiness of y is also true |
#
Else If StatementsIn LemonScript, there is also a elif
keyword. An elif keyword cannot be used without an if
statement. The syntax is as follows:
if ([condition]) { [code]} elif ([condition]) { [code]}
This code is the same this as:
if ([condition]) { [code]} else { if ([condition]) { [code] }}
This means that you dont have to use large nested if and else statements. Here is an example of an elif
statement:
var time = 14
if (time < 6) print("night")elif (time == 6) print("dawn")elif (time < 12) print("morning")elif (time == 12) print("noon")elif (time < 18) print("afternoon")elif (time < 20) print("evening")else print("night")