The if
statement executes statement
if condition
is true
.
main {
if condition {
statement
}
}
In this example, if
statement executes statement1
if condition
is true
.
Otherwise, it executes optional else
clause statement2
.
main {
if condition {
statement1
} else {
statement2
}
}
You can add more conditions to if
statement with elif
statements.
main {
if condition1 {
statement1
} elif condition2 {
statement2
} elif condition3 {
statement3
} else {
statement4
}
}
The ternary operation is the only operation that takes three operands. This operation is frequently used as an alternative to an if…else statement.
main {
condition ? statement1 : statement2
}
Same as regular control flow you can surround if condition
with parenthesis
if your condition is multiline.
This way it would be more self-describing to other people what is going on in
this block of code.
main {
if (
condition1 &&
condition2 &&
condition3
) {
statement
}
}