Presentation is loading. Please wait.

Presentation is loading. Please wait.

16-Oct-15 Loops in Methods And compound conditions.

Similar presentations


Presentation on theme: "16-Oct-15 Loops in Methods And compound conditions."— Presentation transcript:

1 16-Oct-15 Loops in Methods And compound conditions

2 Overview Loops can be used in the main method and in Jeroo methods They work the same way, only the syntax is different. Compound conditions allow for more complicated choices.

3 Using a while loop in a Jeroo method Assume that a Jeroo named Kim is facing a line of flowers ahead. Have Kim pick all of those flowers, and stop as soon as there is no flower directly ahead. After picking all of the flowers, Kim should turn to the left. while( kim.isFlower(AHEAD) ) { kim.hop(); kim.pick(); } kim.turn(LEFT); How would this be written as part of a method?

4 method pickRow() { while(isFlower(AHEAD)) { hop(); pick(); } turn(LEFT); } Rewritten As part of a method The main program would be: method main() { Jeroo kim = new Jeroo(); kim.pickRow(); }

5 Simple and Compound Conditions A simple condition has one part. In the Jeroo language, a simple condition is formed by invoking a single sensor method. Examples: tiffany.isClear(RIGHT) walter.isFacing(EAST) A compound condition uses logical operators. The Jeroo language contains the three most commonly used logical operators: ! (NOT) ie: !Tiffany.isClear(RIGHT) && (AND) ie: Tiffany.isClear(RIGHT) && Tiffany.isClear(LEFT) || (OR) ie: Tiffany.hasFlower() || Tiffany.isClear(AHEAD)

6 Compound condition examples Boolean Expression (Java-style) & English Translation ! bob.isNet(AHEAD) There is not a net ahead of Bob bob.hasFlower() && bob.isClear(LEFT) Bob has at least one flower and there is nothing in the cell immediately to the left of Bob. bob.isWater(AHEAD) || bob.isWater(RIGHT) There is water ahead or to the right of Bob, or both Notice the COMPLETE CONDITIONS on both sides of the OR bob.isFacing(WEST) && ( ! bob.isNet(AHEAD) ) Bob is facing west and there is no net ahead Use these examples to write compound conditions

7 The Problem: Remove all the nets on Jessica’s right side as she crosses the island. Question: How do you know that a Jeroo has reached the end of the island? How do you say “ keep hopping until you reach the end of the island”? How do you say “keep removing nets until you reach the end of the island”? Answer: there is water ahead while (!isWater(AHEAD)) { hop(); } while(!isWater(AHEAD)) { // put the code here to // remove a net if there is one. } Some questions:

8 The End


Download ppt "16-Oct-15 Loops in Methods And compound conditions."

Similar presentations


Ads by Google