IF 1-Jul-19.

Slides:



Advertisements
Similar presentations
13-Jun-14 OOP features of Jeroo. Overview In this presentation we will discuss these topics: OOP terminology Jeroo syntax constructors methods.
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
An Object-Oriented Approach to Programming Logic and Design
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.
Conditionals How do we solve tasks in which every particular of a task is not specifically known? – A robot needs the ability to survey its immediate environment.
Program Design and Development
Alice Learning to program: Part Two by Ruthie Tucker and Jenna Hayes Under the direction of Professor Susan Rodger Duke University, July 2008.
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.
Step 2: Solve the 1st side/layer
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.
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,
Pseudocode Simple Program Design Third Edition A Step-by-Step Approach 2.
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.
CMP-MX21: Lecture 4 Selections Steve Hordley. Overview 1. The if-else selection in JAVA 2. More useful JAVA operators 4. Other selection constructs in.
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.
16-Dec-15 Control Structures VB. Overview Without control structures, everything happens in sequence, the same way every time Jeroo has two basic control.
Programming Logic and Design, Introductory, Fourth Edition1 Understanding the Three Basic Structures Structure: a basic unit of programming logic Any program.
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.
Karel the Robot – Review Primitive Commands move pickbeeper putbeeper turnleft turnoff Karel’s program statements are separated by a semicolon (;) Copyright.
INVITATION TO Computer Science 1 11 Chapter 2 The Algorithmic Foundations of Computer Science.
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!
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Programming Logic and Design Fourth Edition, Introductory Chapter 2 Understanding Structure.
Methods 9-Mar-17.
JavaScript Controlling the flow of your programs with ‘if’ statements
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.
Control Structures part 2
Selection (also known as Branching) Jumail Bin Taliba by
The switch Statement, and Introduction to Looping
Jeroo Code 18-Jul-18.
Programming Mehdi Bukhari.
Transition to Code Upsorn Praphamontripong CS 1110
Introduction To Flowcharting
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,
Control Structures.
OOP features of Jeroo 8-Nov-18.
Control Structures (Structured Programming) for controlling the procedural aspects of programming CS1110 – Kaminski.
Program Design Introduction to Computer Programming By:
Understanding the Three Basic Structures
Java Programming Control Structures Part 1
Algorithm Discovery and Design
If selection construct
Problem Solving Designing Algorithms.
Introduction and Code 18-Jan-19.
Overview Introduction to Jeroo: What is Jeroo? Where did it come from?
Control Structures 5-Apr-19.
ICT Gaming Lesson 2.
Nested If Statements While Loops
Program Flow.
Control Structures part 2
Control Structures (Structured Programming) for controlling the procedural aspects of programming CS1110 – Kaminski.
ASP control structure BRANCHING STATEMENTS
CprE 185: Intro to Problem Solving (using C)
Control Structures 12-May-19.
Python While Loops.
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.
2.2 Algorithms 26-Jun-19.
OOP features of Jeroo 3-Jul-19.
Indentation & Comments
Control Structures VB part 2
Jeroo Code 7-Sep-19.
Presentation transcript:

IF 1-Jul-19

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)

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

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”

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

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

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.

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

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

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 Jeroo has a help feature with all the details about the language. Help is here