1 / 17

Control Structures part 2

Control Structures part 2. Overview. Control structures cause the program to repeat a section of code or choose between different sections of code while loops repeat if statements choose Programs can use whiles and ifs in the Main method or in Jeroo methods

Download Presentation

Control Structures part 2

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Control Structurespart 2

  2. Overview • Control structures cause the program to repeat a section of code or choose between different sections of code • while loops repeat • if statements choose • Programs can use whiles and ifs in the Main method or in Jeroo methods • Control structures can use compound expressions to solve problems • Control structures can be nested inside each other to solve the most difficult problems

  3. Using a while loop in a Jeroo method • Assume that a Jeroo named Kim is not standing on a flower, but there is 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. As part of a method method pickRow(){ while(isFlower(AHEAD)) { hop(); pick(); } turn(LEFT); } The main program would be: method main(){ Jeroo kim = new Jeroo(); kim.pickRow(); }

  5. Review: The Conditional statements some statement if (condition ){ do if true} next statement some statement if (condition){ do if true } else{ do if false } next statement if( condition_1) { //statements that execute if condition_1 is true } else if ( condition_2){ //statements to execute when condition_2 is true} //more else if blocks as necessary else if (last_condition){ //statements to execute when last_condition is true} else { //statements to execute when // all conditions are false } if if-else Cascaded if-else

  6. Changing Cascaded-If into a Jeroo method method decide() { if( louisa.isFlower(AHEAD)) { louisa.hop(); louisa.pick(); } else if( louisa.isNet(AHEAD)) { louisa.toss(); } else if( louisa.isWater(AHEAD)) { louisa.plant(); } else if( louisa.isJeroo(AHEAD)) { louisa.give(AHEAD); } else { louisa.hop(); louisa.turn(LEFT); } • Assume that a Jeroo named Louisa is carrying at least one flower. • Have her check the cell ahead. • If that cell contains a flower, pick it. • If that cell contains a net, disable it. • If that cell contains water, plant a flower at the current location. • If that cell contains another Jeroo, give that Jeroo a flower. • Finally, if there is nothing in that cell, have her hop once and turn left. } To turn this into a Jeroo method, give it a name and remove the specific Jeroo name from the code

  7. 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)

  8. 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

  9. A more complex Programming Example • Have the Jeroo named Jessica (who has at least 1 flower) keep moving forward until she finds a net to her left or right. When she finds one, have her disable it and return to face her original direction again. After she disables a net, Jessica should hop one space ahead. Pseudocode: While there is not a net on the left or right hop If there is a net to the right then disable the net on the right and turn back Else if the net is not to the right, it must be on the left disable the net on the left and turn back hop one time first keep hopping until a net is found then disable the net, whichever side it is on then hop once

  10. Translate the pseudo code to code While there is not a net on the left or right hop If there is a net to the right then disable the net on the right and turn back Else if the net is not to the right, it must be on the left disable the net on the left and turn back jessica.hop() first problem • Which is the correct way to say: • “there is not a net on the left or right” ? • ! isNet(LEFT) || !isNet(RIGHT) • !isNet(LEFT) && !isNet(RIGHT) this can be read as: there is not a net on the left and there’s not a net on the right.

  11. Translate the pseudo code to code While there is not a net on the left or right hop If there is a net to the right then disable the net on the right and turn back Else if the net is not to the right, it must be on the left disable the net on the left and turn back jessica.hop() this has been solved before so how must it change if the net is on the left? turn(RIGHT); toss(); turn(LEFT); turn(LEFT); toss(); turn(RIGHT);

  12. Put it together: Which part still needs to be translated into code? while ( !isNet(LEFT) && !isNet(RIGHT) ) { hop(); } if there is a net to the right then else //if the net is not to the right, it must be on the left hop(); (isNet(RIGHT)) { turn(RIGHT); toss(); turn(LEFT); } { turn(LEFT); toss(); turn(RIGHT); } Ready to type in the code! Is this code for a main method or a Jeroo method? Why? The code does not specify a Jeroo name, so it belongs in a Jeroo method

  13. Control structures can be nested inside each other to solve the most difficult problems

  14. A problem requiring nested control structures • Remove all the nets on Jessica’s right side as she hops all the way across the island. The code to remove one net and move forward has already been written: if (isNet(RIGHT) ) { turn(RIGHT); toss(); turn(LEFT); } hop();

  15. 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. } The Problem: Remove all the nets on Jessica’s right side as she crosses the island. Some questions:

  16. Final version: Put the pieces together • Notice the indentation: while(! isWater(AHEAD)) { //remove the net if there is one. if( isNet(RIGHT)) { turn(RIGHT); toss(); turn(LEFT); } hop(); }

  17. The End

More Related