Chapter 7.2 While – Conditional Loops. What is the BEST description of how many times a while loop executes? A.Some fixed number of times that the programmer.

Slides:



Advertisements
Similar presentations
As you come in… Sign in (in back) and pick up Badge Name Card – on computer… Log in: Launch/Start Alice Any questions? (of any kind) DOWNLOADS FOR TODAY:
Advertisements

Iteration While / until/ for loop. Iteration: while/ Do-while loops Iteration continues until condition is false: 3 important points to remember: 1.Initalise.
While loops.
Karel J Robot Chapter 6.
How SAS implements structured programming constructs
Lecture 12 or President’s Day Holiday Chapter 7.2 While Loops – Finish up our race of the penguins.
As you come in…  Sign in (in back) and pick up  Badge  Name Card – write your first name LARGELY on back  Log in:  Launch/Start Alice  Any questions?
CS0004: Introduction to Programming Repetition – Do Loops.
Repetition Structures
While: Indefinite Loops Alice. Repetition In some situations, we don’t know exactly how many times a block of instructions should be repeated. All we.
Fall 2007ACS-1805 Ron McFadyen1 Ch 7 Loops Alice has two control structures for controlling the repeated execution of statements Loop While.
Fall 2009ACS-1805 Ron McFadyen1 Ch 7 Loops Alice has two control structures for controlling the repeated execution of statements Loop While.
While: Indefinite Loops Sec 8-14 Web Design. Objectives The student will: Understand what an Indefinite Loop is Understand how create an indefinite loop.
Repetition: Definite Loops Alice. Repetition In many kinds of animations, especially simulations and games, some actions happen again and again. Example:
Introducing While loops (and random numbers too) Alice.
CS320n –Visual Programming Indefinite Loops (Slides 7-2) Thanks to Wanda Dann, Steve Cooper, and Susan Rodger for slide ideas.
While: Indefinite Loops Alice. Repetition In some situations, we don’t know exactly how many times a block of instructions should be repeated. All we.
Fall 2008ACS-1805 Ron McFadyen1 Ch 8 Recursion Recursion occurs when a method (or function) calls itself.
Programming with Alice Advanced topics – Demo. Overview of topics Recursion Random Numbers Variables Arrays.
Week 11 Recap CSE 115 Spring Want to write a programming language? You’ll need three things: You’ll need three things: –Sequencing –Selection Polymorphism.
New Mexico Computer Science For All More Looping in NetLogo Maureen Psaila-Dombrowski.
Python quick start guide
Lecture 11 Alice Project Deliverable 1: Extension to Midnight! Midterm next class – Midterm review 4-6pm WED Pepper Canyon Hall 106 – Red scantron (1/2.
Events Chapter 7 Part 2. While a Key is Pressed Event Specialized event An event occurs when you press a key and continues until you take your finger.
CompSci 4 Chap 7 Sec 2 Apr 7, 2005 Prof. Susan Rodger Note: thanks to Wanda Dann and Steve Cooper for slide ideas.
CPS120 Introduction to Computer Science Iteration (Looping)
Addison Wesley is an imprint of © 2010 Pearson Addison-Wesley. All rights reserved. Engineering Computation with MATLAB Second Edition by David M. Smith.
Mathematical Expressions, Conditional Statements, Control Structures
CONTROL FLOW The order in which blocks are executed is called the “control flow” of the script So far all our scripts have just executed blocks in the.
CPS120 Introduction to Computer Science Iteration (Looping)
 In computer programming, a loop is a sequence of instruction s that is continually repeated until a certain condition is reached.  PHP Loops :  In.
Repetition Structures Chapter 5 Part The While Instruction  Combines Loop and the condition that is also used in If/else statements  The loop.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
© The McGraw-Hill Companies, 2006 Chapter 3 Iteration.
Program Structures Chapter 5. 5 Branching Allows different code to execute based on a conditional test. if, if-else, and switch statements.
Motion and Forces Unit Chapter 1 Section 2. I1. Position can change at different rates. Can be fast SPEED A measure Can be slow Has a particular distance.
XP Tutorial 3 New Perspectives on JavaScript, Comprehensive1 Working with Arrays, Loops, and Conditional Statements Creating a Monthly Calendar.
Computer Programming 12 Lesson 6 – Loop structure By: Dan Lunney.
26/06/ Iteration Loops For … To … Next. 226/06/2016 Learning Objectives Define a program loop. State when a loop will end. State when the For.
Follow up from lab See Magic8Ball.java Issues that you ran into.
FOP: While Loops.
Chapter 9 Repetition.
Chapter 3: Decisions and Loops
While: Indefinite Loops
Chapter 5: Repetition Structures
Chapter 5 Structures.
all loops initialization – set up the loop
Chapter 5 Repetition.
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
LESSON 11 – WHILE LOOPS UNIT 5 – 1/10/17.
Chapter 9 Control Structures.
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
Chapter 6: Repetition Structures
Chapter 5: Repetition Structures
Algorithms Take a look at the worksheet. What do we already know, and what will we have to learn in this term?
Chapter 8: More on the Repetition Structure
LabVIEW.
3.1 Iteration Loops For … To … Next 18/01/2019.
© 2016 Pearson Education, Ltd. All rights reserved.
Loop Strategies Repetition Playbook.
CSCI N207 Data Analysis Using Spreadsheet
Seating “chart” Front - Screen rows Back DOOR.
Topics Introduction to Repetition Structures
PROGRAM FLOWCHART Iteration Statements.
CSC1401 Manipulating Pictures 2
CprE 185: Intro to Problem Solving (using C)
CMPT 120 Lecture 13 – Unit 2 – Cryptography and Encryption –
G6DICP - Lecture 5 Control Structures.
Presentation transcript:

Chapter 7.2 While – Conditional Loops

What is the BEST description of how many times a while loop executes? A.Some fixed number of times that the programmer can determine before the code runs B.Some fixed number of times that can be calculated with a function C.Some variable number of times that is determined by the distance of one object to another D.Some variable number of times that programmer cannot determine before the code runs

What loop header would keep a shark swimming after a goldfish until he was within 0.5 meters behind the goldfish?

We change the while loop Boolean expression controlling the chase to this What does it do (when will the loop iterate)? A.Chase until the goldfish turns blue B.Chase until the goldfish turns a color other than blue C.Chase until the shark gets close

Section 7.2 While (Indeterminate) Loops While loops execute some number of times that we cannot determine before the code runs

If I have a chase that should keep going on until the goldfish turns blue (pretend he turns blue after fleeing for a certain amount of time) what condition should we use to control the while loop in the chase method? D) A and C E) B and C

Hint to getting while loops right Humans naturally think “until” – I’ll keep spending until I run out of money (balance <= 0) – I’ll keep dancing until I fall asleep Computers use “while” loops – the opposite – I’ll keep spending while I still have money (balance > 0) – I’ll keep dancing while I am not asleep – While I have a dirty dish, I’ll keep washing dishes

Let’s have a race… A – Wind up penguin (he just goes) While loop with “walk and spin” inside it – Jet-pack penguin2 (controlled by <- event) Moves forward.5 meters Race to a stop sign (within 2 meters) Whenever someone gets within 2 meters – Stop looping (going)

When should we keep going? e.g. while loop expression true (P1: wind up, P2: jet pack) CasesABCD P1, P2 outsideTTTT P1 inside, P2 outsideTTTF P2 inside, P1 outsideTTFF P1, P2 insideTFFF

Which while loop header (tile) would you use to control the “going”?

This would STOP the game (evaluate to false) when A.Both penguins must be close to the stop sign B.Either penguin is close to the stop sign C.Neither penguin is close to the stop sign D.I don’t know What does the other one do?

CasesE1E2Evaluates To (keep playing while true) P1, P2 outsideTT P1 inside, P2 outside FT P2 inside, P1 outside TF P1, P2 insideFF Truth Table for OR logical operator

CasesE1E2Evaluates To (keep playing while true) P1, P2 outsideTT P1 inside, P2 outside FT P2 inside, P1 outside TF P1, P2 insideFF Truth Table for AND logical operator

Let’s look at the code:

The jet pack penguin (P2) can move forward on a <- event when A.Neither penguin is close to the stop sign B.The windup penguin is close, but the jet pack penguin isn’t C.The jet pack penguin is close, but the windup penguin isn’t D.Any time (any possible situation of TT, TF, FT, FF)

Both penguins stop moving when someone “close” To fix this we’d need to create how many of the following? 1.New method to be called for <- event handler 2.New method to be called by the windUpAndGo method 3.If statement in <- event handler 4.If statement in penguin move method 5.If statement in windUpAndGo method

Which if statement would you want and why? Allow to move when… C) Both A and B D) Neither A nor B E) I don’t know

2 meters from the stop sign? That seems far! (3-D object representation trickiness) Stop Sign center: in middle SIGN Penguin center: in middle of FEET I want to control stopping by distance of penguin center from BASE of stop sign! Use math – again SQRT( c*c – b*b) SQRT (penguin.distanceTo(stopSign) * penguin.distanceTo(stopSign) – stopSign.height * stopSign.height) C B A

Moral of the Story In your project, take the time to look at how the objects you are using are designed – Before you get too far into the code