IF 17 Dec21 Overview n Without control structures

  • Slides: 18
Download presentation
IF 17 -Dec-21

IF 17 -Dec-21

Overview n Without control structures, n n n everything happens in sequence, the same

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

If Example n Have the Jeroo named jessica check for a net to her

If Example n 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. is. Net(RIGHT) ) { jessica. turn(RIGHT); jessica. toss(); jessica. turn(LEFT); } jessica. hop();

If-else statement some statement 1. 2. When you want to choose one thing or

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

If-else Example n n Notice where the code matches the problem description: Have the

If-else Example n n 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. is. Net(AHEAD)) { timmy. toss(); timmy. turn(LEFT) ; } else { timmy. turn(RIGHT); } timmy. hop();

Cascaded if statement style This particular structure is often called a cascaded if. Only

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

Cascaded if-else Example n n n n Assume that a Jeroo named Louisa is

Cascaded if-else Example n n n n 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. is. Flower(AHEAD)) { louisa. hop(); louisa. pick(); } else if (louisa. is. Net(AHEAD)) { louisa. toss(); } else if (louisa. is. Water(AHEAD)) { louisa. plant(); } else if (louisa. is. Jeroo(AHEAD) ) { louisa. give(AHEAD); } else { louisa. hop(); louisa. turn(LEFT); } No notes, here Just an example Do you understand it? Could you write this code?

Changing Cascaded-If into a Jeroo method n n n n Assume that a Jeroo

Changing Cascaded-If into a Jeroo method n n n n 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. method decide() { if( louisa. is. Flower(AHEAD)) { louisa. hop(); louisa. pick(); } else if( louisa. is. Net(AHEAD)) { louisa. toss(); } else if( louisa. is. Water(AHEAD)) { louisa. plant(); } else if( louisa. is. Jeroo(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

When problems get more complex n n n Plan before you program Write pseudocode

When problems get more complex n n n 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

A more complex Programming Example n Have the Jeroo named Jessica (who has at

A more complex Programming Example n 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 first keep hopping until a net is found 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 then disable the net, whichever side it is on then hop once

Translate the pseudo code to code While there is not a net on the

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” ? 1. ! is. Net(LEFT) || !is. Net(RIGHT) 2. !is. Net(LEFT) && !is. Net(RIGHT) this can be read as: there is not a net on the left and there’s not a net on the right.

Translate the pseudo code to code While there is not a net on the

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() turn(RIGHT); toss(); turn(LEFT); this has been solved before so how must it change if the net is on the left? turn(LEFT); toss(); turn(RIGHT);

Put it together: while ( !is. Net(LEFT) && !is. Net(RIGHT) ) { hop(); }

Put it together: while ( !is. Net(LEFT) && !is. Net(RIGHT) ) { hop(); } if there is a net to the right then { } else { } Which part still needs to be translated into code? (is. Net(RIGHT)) turn(RIGHT); toss(); turn(LEFT); //if the net is not to the right, it must be on the left turn(LEFT); toss(); turn(RIGHT); Ready to type in the code! hop(); 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

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

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

A problem requiring nested control structures n Remove all the nets on Jessica’s right

A problem requiring nested control structures n 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 (is. Net(RIGHT) ) { turn(RIGHT); toss(); turn(LEFT); } hop();

Some questions: The Problem: Remove all the nets on Jessica’s right side as she

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

Final version: Put the pieces together n Notice the indentation: while(! is. Water(AHEAD)) {

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

The End Jeroo has a help feature with all the details about the language.

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