In this task we have to guide the frog safely across a river. The river is far too fast moving for the frog and it will drown if it falls in. We can however hop onto either a passing lilly pad or a tree trunk, then continue moving right once were aboard. Similar to the previous task involving traffic, we will have to wait for either a lilly pad or a tree trunk to arrive before we can cross.
| nextToRiver() | Evaluates to true if the frog is directly next to a river |
| nextToPad() | Evaluates to true if there is a lilly pad near to the frog |
| nextToLog() | Evaluates to true if there is a log near to the frog |
Similar to the previous task, we need to move to the edge of the river and then loop while nextToRiver() is true. This time however, we can use the instruction frogRight() when either nextToPad() or nextToLog() is true.
To do this we use the symbol '||', which is two | symbols back to back. Programmers call this an 'Or operator'. An expression, followed by ||, followed by another expression evaluates to true if either of the expressions is true.
| First Expression | Operator | Second Expression | Result |
|---|---|---|---|
| Is the world round? | || |
Is the sky blue? | true |
| Is the world round? | || |
Is the sky green? | true |
| Is the world flat? | || |
Is the sky blue? | true |
| Is the world flat? | || |
Is the sky green? | false |
The if statement in the while loop will look like this:
if( nextToPad() || nextToLog() )
frogRight(3);
Write a program to guide the frog safely across the river
Run the program several times until it has used both a lilly pad and a tree trunk to cross