Writing Joystick Code: Part 3: Difference between revisions

Jump to navigation Jump to search
No edit summary
Line 85: Line 85:


We write then as {, and endif as }.
We write then as {, and endif as }.
And each "action" is terminated by a semicolon:


Lets say that if x is less than 5 we want to add 2 to it. We write
 
   if (x < 5) { x = x + 2 }
 
Lets say that if x is less than 5 we want to add 2 to it. We write:
<syntaxhighlight lang="php">
   if (x < 5) { x = x + 2}
</syntaxhighlight>


The more readable way of writing it is
The more readable way of writing it is
<syntaxhighlight lang="php">
   if (x < 5) {
   if (x < 5) {
     x = x + 2
     x = x + 2;
   }
   }
</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">
   if (x < 5) {
   if (x < 5) {
     x = x + 2;
     x = x + 2;
     y + y + 1
     y + y + 1;
   }
   }
</syntaxhighlight>


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


Now lets say we have the following rules:
Now lets say we have the following rules:
Line 121: Line 132:


Writing our code properly we get
Writing our code properly we get
<syntaxhighlight lang="php">
   if (x < 5) {
   if (x < 5) {
     x = x + 2
     x = x + 2;
   }
   }
   elseif (x < 10) {
   elseif (x < 10) {
     x = x + 1
     x = x + 1;
   }
   }
   else {
   else {
     x = x + 7
     x = x + 7;
   }
   }
</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 elseif, but it gets messy. If you have a number of tests then it is better to test for each case individually.
Easier to understand and much less chance of an error.
Easier to understand and much less chance of an error.
Our above example would become
Our above example would become
<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>


Because each if is tested we need to have the (x >= 5) otherwise if x = 1 both the first and second if conditions would be satisfied.
Because each if is tested we need to have the (x >= 5) otherwise if x = 1 both the first and second if conditions would be satisfied.
Line 150: Line 166:


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 Gear-Down. Then you write
<syntaxhighlight lang="php">
   if (!Gear-Down) {
   if (!Gear-Down) {
     action
     action;
   }
   }
</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.
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.

Navigation menu