Presentation is loading. Please wait.

Presentation is loading. Please wait.

IF 1-Jul-19.

Similar presentations


Presentation on theme: "IF 1-Jul-19."— Presentation transcript:

1 IF 1-Jul-19

2 Overview Without control structures,
everything happens in sequence, the same way every time Jeroo has two basic control structures while ( to repeat statements ) if (to make choices)

3 If Example Have the Jeroo named jessica check for a net to her right. If there is one, have her disable it and return to her current state. Whether or not she disables a net, jessica should hop one space ahead. if (jessica.isNet(RIGHT) ) { jessica.turn(RIGHT); jessica.toss(); jessica.turn(LEFT); } jessica.hop();

4 If-else statement When you want to choose one thing or another.
some statement if (condition) { do if true } else do if false next statement When you want to choose one thing or another. Check the condition If true – execute true branch , skip false branch If false – skip true branch, execute false branch execute “next statement”

5 If-else Example Notice where the code matches the problem description: 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. After he disables the net and turns around or simply turns right, Timmy must move one space forward. if (timmy.isNet(AHEAD)) { timmy.toss(); timmy.turn(LEFT); timmy.turn(LEFT) ; } else { timmy.turn(RIGHT); } timmy.hop();

6 Cascaded if statement style
This particular structure is often called a cascaded if. Only one block of code will be executed. 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 3 things about the coding style. 1. The {}’s are aligned with the start of the words if and else. 2. else if is two words. 3. The other statements are indented. There are 2 important things to observe about this structure 1. There is no limit on the number of else-if blocks. 2. The final else block is optional. else if is 2 words

7 Cascaded if-else Example
No notes, here Just an example Do you understand it? Could you write this code? 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.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.

8 Changing Cascaded-If into a Jeroo method
method decide() { 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. 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); } } To turn this into a Jeroo method, give it a name and remove the specific Jeroo name from the code

9 When problems get more complex
Plan before you program Write pseudocode which is a description somewhere between English and Java of what steps your program will need to take Break the problem into methods

10 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

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

12 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);

13 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 (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

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

15 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();

16 Some questions: Question:
The Problem: Remove all the nets on Jessica’s right side as she crosses 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. 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”?

17 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();

18 The End Jeroo has a help feature with all the details about the language. Help is here


Download ppt "IF 1-Jul-19."

Similar presentations


Ads by Google