Writing Joystick Code: Part 3: Difference between revisions

m
Line 64: Line 64:
=== if ... then ... else ===
=== if ... then ... else ===


Sometimes you only want to do something if a certain condition is true (or false.) In this case you use an if clause. The general form is
Sometimes you only want to do something if a certain condition is true (or false.) In this case you use an if clause. The general form in most programming languages is
  if (condition) then action endif


We write then as {, and endif as }.
: '''if''' '''('''condition''')''' '''then''' action '''endif'''
And each "action" 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="php">
<syntaxhighlight lang="php">
 
if (condition)
if (condition)
{ # the opening curly brace means THEN
{ # the opening curly brace means THEN
  action;
  action;
} # the closing curly brace means ENDIF, i.e. end of this block
} # the closing curly brace means ENDIF, i.e. end of this block
 
</syntaxhighlight>
</syntaxhighlight>


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 ==
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="php">
<syntaxhighlight lang="php">
  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="php">
<syntaxhighlight lang="php">
  if (x < 5) {
if (x < 5) {
    x = x + 2;
  x = x + 2;
   }
}
</syntaxhighlight>
 
And we can even omit the braces if there's only one action:
<syntaxhighlight lang="php">
if (x < 5) # no opening brace...
   x = x + 2; # ...so the if statement ends at the first semicolon
</syntaxhighlight>
</syntaxhighlight>


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="php">
<syntaxhighlight lang="php">
  if (x < 5) {
if (x < 5) {
    x = x + 2;
  x = x + 2;
    y + y + 1;
  y = y + 1;
  }
}
</syntaxhighlight>
</syntaxhighlight>


Line 112: Line 116:


<syntaxhighlight lang="php">
<syntaxhighlight lang="php">
  if (x < 5) {
if (x < 5) {
    x += 2; # means x = x + 2
  x += 2; # means x = x + 2
    y += 1; # means y = y + 1
  y += 1; # means y = y + 1
  }
}
</syntaxhighlight>
</syntaxhighlight>


Line 122: Line 126:
It looks like this
It looks like this
<syntaxhighlight lang="php">
<syntaxhighlight lang="php">
  if (x < 5) {
if (x < 5) {
    x = x + 2;
  x = x + 2;
  }
} else {
  else {
  x = x + 1;
    x = x + 1;
}
  }
</syntaxhighlight>
</syntaxhighlight>


Now lets say we have the following rules:
Now lets say we have the following rules:
  If x < 5 add 2 to it
* If x < 5 add 2 to it
  If x < 10 add 1 to it
* If x < 10 add 1 to it
  If x is 10 or more add 7 to it. In this case elseif becomes useful.
* If x is 10 or more add 7 to it.


In English our code would be
In English our code would be
  If (x < 5) then (x = x + 2) else if (x < 10) then (x = x + 1).
* If (x < 5) then (x = x + 2) else if (x < 10) then (x = x + 1).
  If none of these are true then (x = x + 7). End of if clause.
* If none of these are true then (x = x + 7).
* End of if clause.


We test for x < 5 first. If it fails we test for x < 10. If x , 5 succeeds we do (x = x + 2) and then move on past the end of if clause.
We test for x < 5 first. If it fails we test for x < 10. If x , 5 succeeds we do (x = x + 2) and then move on past the end of if clause.
Line 144: Line 148:
Writing our code properly we get
Writing our code properly we get
<syntaxhighlight lang="php">
<syntaxhighlight lang="php">
 
if (x < 5) {
  if (x < 5) {
  x = x + 2;
    x = x + 2;
} elsif (x < 10) {
  }
  x = x + 1;
  else if (x < 10) {
} else {
    x = x + 1;
  x = x + 7;
  }
}
  else {
    x = x + 7;
  }
</syntaxhighlight>
</syntaxhighlight>


You can use more than one elseif, but it gets messy. If you have a number of tests then it is better to test for each case individually.
You can use more than one elsif, but it gets messy. If you have a number of tests then it might be better to test for each case individually.
Easier to understand 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="php">
<syntaxhighlight lang="php">
  if (x < 5) {
if (x < 5) {
    x = x + 2;
  x = x + 2;
  }
}
  if (x >= 5) and (x < 10) {
if (x >= 5) and (x < 10) {
    x = x + 1;
  x = x + 1;
  }
}
  if (x >= 10) {
if (x >= 10) {
    x = x + 7;
  x = x + 7;
  }
}
</syntaxhighlight>
</syntaxhighlight>


Line 175: 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 ==


Lets say that you want something to happen only if the gear is up. But the only property you can read is Gear-Down. 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="php">
<syntaxhighlight lang="php">
  if (!Gear-Down) {
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.
Beacuse we use < and > we will need to enclose it all with CDATA, in order not to mess up the XML syntax.


See the discussion of variables below for an example of the use of if.
See the discussion of variables below for an example of the use of if.
395

edits