6
edits
Philosopher (talk | contribs) m (→if ... then ... else: cleanup) |
mNo edit summary |
||
(3 intermediate revisions by 2 users not shown) | |||
Line 9: | Line 9: | ||
----------------------------------------------------------------------------- | ----------------------------------------------------------------------------- | ||
To toggle the Parking brake, use this. Entire code-block shown. Non-repeatable button. (You have to specifically set <repeatable> to true if you want repeatable. It is assumed to be false. | To toggle the Parking brake, use this. Entire code-block shown. Non-repeatable button. (You have to specifically set <repeatable> to true if you want repeatable. It is assumed to be false.) | ||
<syntaxhighlight lang="xml"> | <syntaxhighlight lang="xml"> | ||
<button n="1"> | <button n="1"> | ||
Line 70: | Line 70: | ||
In Nasal, we write "then" as "{" and "endif" as "}". Each "action" (or statement) is terminated by a semicolon: | In Nasal, we write "then" as "{" and "endif" as "}". Each "action" (or statement) is terminated by a semicolon: | ||
<syntaxhighlight lang=" | <syntaxhighlight lang="nasal"> | ||
if (condition) | if (condition) | ||
{ # the opening curly brace means THEN | { # the opening curly brace means THEN | ||
Line 79: | Line 79: | ||
Actions can be arbitrary Nasal expressions, including function calls and other conditional blocks. | Actions can be arbitrary Nasal expressions, including function calls and other conditional blocks. | ||
== An example == | ==== An example ==== | ||
Lets say that if x is less than 5 we want to add 2 to it. We write: | Lets say that if x is less than 5 we want to add 2 to it. We write: | ||
<syntaxhighlight lang=" | <syntaxhighlight lang="nasal"> | ||
if (x < 5) { x = x + 2; } | if (x < 5) { x = x + 2; } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
The more readable way of writing it is | The more readable way of writing it is | ||
<syntaxhighlight lang=" | <syntaxhighlight lang="nasal"> | ||
if (x < 5) { | if (x < 5) { | ||
x = x + 2; | x = x + 2; | ||
Line 93: | Line 93: | ||
And we can even omit the braces if there's only one action: | And we can even omit the braces if there's only one action: | ||
<syntaxhighlight lang=" | <syntaxhighlight lang="nasal"> | ||
if (x < 5) # no opening brace... | if (x < 5) # no opening brace... | ||
x = x + 2; # ...so the if statement ends at the first semicolon | x = x + 2; # ...so the if statement ends at the first semicolon | ||
Line 99: | Line 99: | ||
If we also want to add 1 to y if the condition is true, we write | If we also want to add 1 to y if the condition is true, we write | ||
<syntaxhighlight lang=" | <syntaxhighlight lang="nasal"> | ||
if (x < 5) { | if (x < 5) { | ||
x = x + 2; | x = x + 2; | ||
Line 115: | Line 115: | ||
For example: | For example: | ||
<syntaxhighlight lang=" | <syntaxhighlight lang="nasal"> | ||
if (x < 5) { | if (x < 5) { | ||
x += 2; # means x = x + 2 | x += 2; # means x = x + 2 | ||
Line 125: | Line 125: | ||
Now lets pretend that we still want to increase x by 2 if it less than 5, and if it is 5 or more we want to add 1 to it. We use else. | Now lets pretend that we still want to increase x by 2 if it less than 5, and if it is 5 or more we want to add 1 to it. We use else. | ||
It looks like this | It looks like this | ||
<syntaxhighlight lang=" | <syntaxhighlight lang="nasal"> | ||
if (x < 5) { | if (x < 5) { | ||
x = x + 2; | x = x + 2; | ||
Line 147: | Line 147: | ||
Writing our code properly we get | Writing our code properly we get | ||
<syntaxhighlight lang=" | <syntaxhighlight lang="nasal"> | ||
if (x < 5) { | if (x < 5) { | ||
x = x + 2; | x = x + 2; | ||
Line 160: | Line 160: | ||
Easier to understand each condition separately and much less chance of an error. | Easier to understand each condition separately and much less chance of an error. | ||
Our above example would become | Our above example would become | ||
<syntaxhighlight lang=" | <syntaxhighlight lang="nasal"> | ||
if (x < 5) { | if (x < 5) { | ||
x = x + 2; | x = x + 2; | ||
Line 176: | Line 176: | ||
But the advantage is that we are forced to write out each condition exactly as it should be tested. Easier to understand and easier to maintain. | But the advantage is that we are forced to write out each condition exactly as it should be tested. Easier to understand and easier to maintain. | ||
== Another example == | ==== Another example ==== | ||
Lets say that you want something to happen only if the gear is up. But the only property you can read is GearDown. Then you write | Lets say that you want something to happen only if the gear is up. But the only property you can read is GearDown. Then you write | ||
<syntaxhighlight lang=" | <syntaxhighlight lang="nasal"> | ||
if (!GearDown) { | if (!GearDown) { | ||
action; | action; | ||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
The ! means not. So it translates as: If the gear is not down perform action. | The ! means not. So it translates as: If the gear is not down perform action. | ||
Line 451: | Line 450: | ||
See [http://flightgear.org | See [http://forum.flightgear.org/viewtopic.php?f=24&t=17851 this post] for an example of using this. | ||
Line 468: | Line 467: | ||
Any complaints/suggestions/questions/kudos can be posted [http://flightgear.org | Any complaints/suggestions/questions/kudos can be posted [http://forum.flightgear.org/viewtopic.php?f=24&t=17892 here]. | ||
edits