842
edits
Chris-barry (talk | contribs) m (→Conditionals) |
Legoboyvdlp (talk | contribs) (Cleanup) |
||
| Line 2: | Line 2: | ||
Sometimes you may only want to execute certain portions of your code, depending on conditions. This can be accomplished in several ways | Sometimes you may only want to execute certain portions of your code, depending on whether various conditions are met. This can be accomplished in several ways, one of the most straightforward ways being an '''if''' expression, which has the form: | ||
<syntaxhighlight lang="nasal"> | <syntaxhighlight lang="nasal"> | ||
var condition=1; # this evaluates to boolean true | var condition = 1; # this evaluates to boolean true | ||
if (condition) { | if (condition) { | ||
# true block, will be executed if condition is true | # true block, will be executed if condition is true | ||
| Line 13: | Line 13: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
If you wish to execute code only if a condition is '''not''' true, use the '''!''' operator: | |||
<syntaxhighlight lang="nasal"> | <syntaxhighlight lang="nasal"> | ||
var condition=1; # boolean true | var condition = 1; # boolean true | ||
if (!condition) { | if (!condition) { | ||
# false block, will be executed if condition is true | # false block, will be executed if condition is true | ||
| Line 30: | Line 30: | ||
if (condition) { | if (condition) { | ||
# true block, will be executed if condition is true | # true block, will be executed if condition is true i.e. FlightGear is paused | ||
} else { | } else { | ||
# false block, will be executed if condition is false | # false block, will be executed if condition is false | ||
| Line 64: | Line 64: | ||
<syntaxhighlight lang="nasal"> | <syntaxhighlight lang="nasal"> | ||
var condition = (1==1 and 2==2 and 3==3) or (4!=5 and !0); | var condition = (1 == 1 and 2 == 2 and 3 == 3) or (4 != 5 and !0); | ||
if (condition) { | if (condition) { | ||
# true block, will be executed if condition is true | # true block, will be executed if condition is true | ||
| Line 125: | Line 125: | ||
if (1==2) { | if (1==2) { | ||
print("wrong"); | print("wrong"); | ||
} else if (1==3) { # NOTE the space between else and if | } else if (1 == 3) { # NOTE the space between else and if | ||
print("wronger"); | print("wronger"); | ||
} else { | } else { | ||
| Line 136: | Line 136: | ||
<syntaxhighlight lang="nasal"> | <syntaxhighlight lang="nasal"> | ||
if (1==2) { | if (1 == 2) { | ||
print("wrong"); | print("wrong"); | ||
} elsif (1==3) { | } elsif (1 == 3) { | ||
print("wronger"); | print("wronger"); | ||
} else { | } else { | ||
edits