| if: Use this when you are concerned about a
condition being true |
Example |
if (
condition ) {
// do these commands when
condition is true
} |
//
when the robot is on an avenue larger
// than 5 turn it blue
if ( this.getAvenue() > 5 ) {
this.setColor( Color.blue );
} |
| if...else: Use this when you want to do one
thing when the condition is true and another thing when the condition is
false |
|
if (
condition ) {
// do these commands when
condition is true
} else {
// do these commands when
condition is false
} |
//
when the robot is on an avenue larger
// than 5 turn it blue
// otherwise it should be yellow in colour
if ( this.getAvenue() > 5 ) {
this.setColor( Color.blue );
} else {
this.setColor( Color.yellow );
} |
| if...else...if: Use this when you have
several possible outcomes of a decision |
|
if (
condition #1 ) {
// do these commands when
condition #1 is true
} else if ( condition #2 ) {
// do these commands when
condition #2 is true
} else if ( condition #3 ) {
// do these commands when
condition #3 is true
} else {
// do these commands when other
conditions are not true
} |
//
when the robot is on an avenue less than 3 turn it blue
// when it is on avenue less than 6 turn it yellow
// otherwise it should be cyan in colour
if ( this.getAvenue() <= 2 ) {
this.setColor( Color.blue );
} else ( this.getAvenue() <= 5 ){
this.setColor( Color.yellow );
} else {
this.setColor( Color.cyan );
} |