Nasal Conditionals: Difference between revisions

Jump to navigation Jump to search
Cleanup
(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. One of the most straightforward ways  being an '''if''' expression, which  has the form:
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>


To negate the whole thing, use the '''!''' operator:
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 {
842

edits

Navigation menu