Control Structures part 2

Slides:



Advertisements
Similar presentations
Using Jeroo Dianne Meskauskas
Advertisements

A8 – Control Structures if, if-else, switch Control of flow in Java Any sort of complex program must have some ability to control flow.
Nested If Statements While Loops
More on Algorithms and Problem Solving
1 Chapter Five Selection and Repetition. 2 Objectives How to make decisions using the if statement How to make decisions using the if-else statement How.
11-May-15 Control Structures part 2. Overview Control structures cause the program to repeat a section of code or choose between different sections of.
Programming in Jessica By Joaquin Vila Prepared by Shirley White Illinois State University Applied Computer Science Department.
SIMPLE PROGRAMS Jeroo – Chapter 4 –. Basic Concepts Jeroo (Java/C++/object-oriented) programing style is case-sensative. Be consistent in coding Logic.
Loops (Part 1) Computer Science Erwin High School Fall 2014.
Program Design and Development
Slides prepared by Rose Williams, Binghamton University Chapter 3 Flow of Control Loops in Java.
Loops Repetition Statements. Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional.
CHAPTER 10 Recursion. 2 Recursive Thinking Recursion is a programming technique in which a method can call itself to solve a problem A recursive definition.
Chapter 2: Algorithm Discovery and Design
ECE122 L9: While loops March 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 9 While Loops.
11 Chapter 4 LOOPS AND FILES. 22 THE INCREMENT AND DECREMENT OPERATORS To increment a variable means to increase its value by one. To decrement a variable.
17-Sep-15 Using Jeroo. Overview In this presentation we will discuss: What is Jeroo? Where did it come from? Why use it? How it works. Your first assignments.
Chapter 2: Algorithm Discovery and Design Invitation to Computer Science, C++ Version, Third Edition.
5-Oct-15 Introduction and Code. Overview In this presentation we will discuss: What is Jeroo? Where can you get it? The story and syntax of Jeroo How.
16-Oct-15 Loops in Methods And compound conditions.
25-Oct-15 Jeroo Code. Overview In this presentation we will discuss: How to write code in Jeroo How to run a Jeroo program.
ㅎㅎ logical operator if if else switch while do while for Third step for Learning C++ Programming Repetition Control Structures.
COMPUTER PROGRAMMING. Iteration structures (loops) There may be a situation when you need to execute a block of code several number of times. In general,
Recursion – means to recur or to repeat – A different way to get a robot to repeat an action A programming language that allows recursive definitions (and.
13-Nov-15 Control Structures. Overview Without control structures, everything happens in sequence, the same way every time Jeroo has two basic control.
1 Karel – Chapter 5 Conditionally Executing Instructions Note: Original slides provided by and modified for Mr. Smith’s AP Computer.
Bunny Eat Broccoli Repetition – Simple loops and Conditional loops Susan Rodger Duke University July 2011.
16-Dec-15 Control Structures VB. Overview Without control structures, everything happens in sequence, the same way every time Jeroo has two basic control.
Mile-long hurdle race Suppose that we want to program Karel to run a one-mile long hurdle race, where vertical wall sections represent hurdles. The hurdles.
17-Feb-16 Methods. Overview In this presentation we will discuss these 4 topics: Main method vs. Jeroo methods Choosing behaviors to turn into methods.
Selection structures in Jeroo! if, if else, if, else if, else if, else.
Flow Control in Imperative Languages. Activity 1 What does the word: ‘Imperative’ mean? 5mins …having CONTROL and ORDER!
Programming Logic and Design Fourth Edition, Introductory Chapter 2 Understanding Structure.
Methods 9-Mar-17.
Copyright © 2014 Pearson Addison-Wesley. All rights reserved. 4 Simple Flow of Control.
Mile-long hurdle race Suppose that we want to program Karel to run a one-mile long hurdle race, where vertical wall sections represent hurdles. The hurdles.
Chapter 3: Decisions and Loops
The switch Statement, and Introduction to Looping
Jeroo Code 18-Jul-18.
ECS10 10/10
Chapter 3 Loops Section 3.3 Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska Anchorage.
Bunny Eat Broccoli Repetition – Simple loops and Conditional loops
Transition to Code Upsorn Praphamontripong CS 1110
CS 106A, Lecture 2 Programming with Karel
The order in which statements are executed is called the flow of control. Most of the time, a running program starts at the first programming statement,
Programming Fundamentals
JavaScript: Control Statements I
Algorithm and Ambiguity
OOP features of Jeroo 8-Nov-18.
Complex Conditionals Human languages are ambiguous
Understanding the Three Basic Structures
Java Programming Control Structures Part 1
Using the sensor Lesson 5.
Three Special Structures – Case, Do While, and Do Until
Chapter 6: Repetition Statements
Algorithm and Ambiguity
Introduction and Code 18-Jan-19.
Overview Introduction to Jeroo: What is Jeroo? Where did it come from?
Python Programming Language
Control Structures 5-Apr-19.
Nested If Statements While Loops
Control Structures part 2
Control Structures 12-May-19.
LOOPS The loop is the control structure we use to specify that a statement or group of statements is to be repeatedly executed. Java provides three kinds.
OOP features of Jeroo 3-Jul-19.
Indentation & Comments
Control Structures VB part 2
Flow of Control Flow of control is the order in which a program performs actions. Up to this point, the order has been sequential. A branching statement.
IF 1-Jul-19.
Jeroo Code 7-Sep-19.
Presentation transcript:

Control Structures part 2 19-May-18

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

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?

while(isFlower(AHEAD)) { hop(); pick(); } turn(LEFT); 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(); }

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

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

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)

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

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

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.

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

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

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

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

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”?

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

The End