NOTES: Loops and conditions to control program flow
Jeroo has 2 basic control structures: _________ and _________.
Example: while there is a flower ahead of the jeroo named sam, keep picking it and hopping:
while (sam.___________(__________) )
{
sam.______();
sam.______();
}
_________ means NOT in Java.
Example: while there is not a net ahead of the jeroo named james, keep hopping:
while (____________________________)
{ james.hop();}
WHILE Example: Kim picks all flowers in a line of flowers ahead, then turns left |
method main()
{
Jeroo kim = new Jeroo( );
while( ___________________________) {
kim.hop();
kim.pick();
}
kim.turn(LEFT);} |
Conditions with IF
Example: Jessica should check for a net to the right. If there is one, disable it and return to the current state. Hop one space ahead. |
if (________________) {
jessica.turn(RIGHT);
jessica.toss();
jessica.turn(LEFT);
}jessica.hop();
|
If-Else example |
Have the Jeroo named Timmy check for a net straight ahead. If there is one, have him disable it and turn around. If there is not a net straight ahead, Timmy should turn right. When he finishes, Timmy must move one space forward.
|
if (timmy.isNet(AHEAD))
{
timmy.toss();
timmy.turn(LEFT);
timmy.turn(LEFT);
}
else {
timmy.________________; } timmy.hop();
|
Sensor methods: write the if statement to be used in a Jeroo method for each of these:
- Does this Jeroo have any flowers?
- Is this Jeroo facing West?
- Is there a flower here?
- Is there another Jeroo to the left?
- Is there a net ahead?
- Is there water to the right?
- Is there a clear space here?
(A clear space contains no flower, no net,
no water, and no Jeroo. )
|
- _____________________
- _____________________
- _____________________
- _____________________
- _____________________
- _____________________
- _____________________
|
Boolean expressions: Write the code for each of these expressions:
- Is there water to the right of tom? ______________________________________
- Is alice facing North? _______________________________________
- Does peter have any flowers? ______________________________________________
- Is there a flower in the square where anthony is standing? ________________________________
Write these complete IF statements:
- If ellis has flowers plant a flower, otherwise pick a flower.
- If there is a net ahead of walter turn right , otherwise hop
Use the HELP feature in Jeroo to finish these notes:
- The if structure: The if structure is used to define ___________________________________
- In the if-else structure only ____________________________ will be executed.
- In the cascaded-if structure, if all conditions are false _________________________ will be executed.
- The pretest while structure is used to define a block of code that will be __________________________ as long as _________________________________________.