'if' and 'else' statements
To control which code is executed when, you will have to use conditions. Conditions are one or more criterias that need to be met for specific code to be executed. For example, you will bring a jacket if it is running, else you will bring a hat.
Conditions in Wing are simple: you insert the if keyword, followed by your condition, and the
do
keyword. All code below will be executed only if the condition is true, until you insert the end
keyword.if your condition here do
print Hello!
end
This will execute
print Hello!
only if your condition here
(or whatever condition you are trying to use) is true. If the condition is false, nothing will happen and the code inside of the if
block will be skipped entirely.In some cases, you may want to run specific code in case your condition is false. In contrary to many other languages, Wing does not give you the option to invert a condition.
Taking the example above, if you want to run code only when
your condition here
is false, you will have to do the following:if your condition here do
else do
print Hello!
end
Futhermore, if you want to run different code depending on a condition, you can do the following:
if true $morning do
print Good morning!
else do
print Good afternoon!
end
The code above will display "Good morning!" when
true $morning
is true (that is, in this specific case, when the variable $morning
is true), and "Good afternoon!" otherwise.Wing makes it possible to add conditions inside of conditions as much as technical resources permit (that is, until you run out of RAM). You could do the following:
if true $morning do
print Good morning!
else do
if true $afternoon do
print Good afternoon!
else do
print Good evening!
end
end
The code above will display "Good morning!" when
true $morning
is true, and if it is not, it will display "Good afternoon!" when true $afternoon
is true, and "Good evening!" otherwise.This code is equivalent to the following code in JavaScript:
if (morning) {
console.log("Good morning!");
} else if (afternoon) {
console.log("Good afternoon!");
} else {
console.log("Good evening!");
}
There is a wide range of conditions Wing can check for, it is even possible to add new conditions through JavaScript binding.
Last modified 5mo ago