4,400
edits
m (Update forum links) |
m (Use Nasal highlighter) |
||
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 81: | Line 81: | ||
==== 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 179: | Line 179: | ||
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; |