The Wait-Until-Event pattern Has it happened  Yes, it happened!

Slides:



Advertisements
Similar presentations
NXTG Workshop Day 2 Programming with Touch Sensor Light Sensor Ultrasonic Sensor Repeat CJ Chung Associate Professor of Computer Science Lawrence Technological.
Advertisements

While loops.
CS0007: Introduction to Computer Programming
CHAPTER 5: Repetition Control Structure. Objectives  To develop algorithms that use DOWHILE and REPEAT.. UNTIL structures  Introduce a pseudocode for.
CS0004: Introduction to Programming Repetition – Do Loops.
1 Loops. 2 Often we want to execute a block of code multiple times. Something is always different each time through the block. Typically a variable is.
1 9/28/07CS150 Introduction to Computer Science 1 Loops section 5.2, 5.4, 5.7.
1 10/20/08CS150 Introduction to Computer Science 1 do/while and Nested Loops Section 5.5 & 5.11.
Intro to Java while loops pseudocode. 1 A “Loop” A simple but powerful mechanism for “making lots of things happen!” Performs a statement (or block) over.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
Introduction to Sensors
Sentinel Logic Assumes while loops and input statements.
1 Chapter 5 File Objects and Looping Statements (Some slides have been modified from their original format)
Chapter 4: Looping CSCI-UA 0002 – Introduction to Computer Programming Mr. Joel Kemp.
Simple Python Loops Sec 9-7 Web Design.
Building Java Programs Chapter 5 Program Logic and Indefinite Loops Copyright (c) Pearson All rights reserved.
Python – Part 4 Conditionals and Recursion. Modulus Operator Yields the remainder when first operand is divided by the second. >>>remainder=7%3 >>>print.
Your Python program uses the create library. It supplies functions that send commands: to your laptop’s Bluetooth radio, then to the BAM on the Create,
Institute for Personal Robots in Education (IPRE)‏ CSC 170 Computing: Science and Creativity.
Computer Science 111 Fundamentals of Programming I The while Loop and Indefinite Loops.
Repetition & Loops. One of the BIG advantages of a computer: ­It can perform tasks over and over again, without getting bored or making mistakes (assuming.
Python Repetition. We use repetition to prevent typing the same code out many times and to make our code more efficient. FOR is used when you know how.
Q and A for Chapter 7 Students of CS104, and me, your benevolent professor, Victor T. Norman, PhD.
Control Structures II Repetition (Loops). Why Is Repetition Needed? How can you solve the following problem: What is the sum of all the numbers from 1.
How to link the robot and the computer (Bluetooth) How to turn on and off How to connect the adaptor Fluke card connection Sec Getting Started How.
Control Structures Repetition or Iteration or Looping Part II.
A loop is a repetition control structure. body - statements to be repeated control statement - decides whether another repetition needs to be made leading.
CS 100 Introduction to Computing Seminar October 7, 2015.
Control Structures RepetitionorIterationorLooping Part I.
Pascal Programming Pascal Loops and Debugging. Pascal Programming Pascal Loops In our first brush with the while do loops, simple comparisons were used.
Repetition. Loops Allows the same set of instructions to be used over and over again Starts with the keyword loop and ends with end loop. This will create.
Variables and Functions ROBOTC Software © 2012 Project Lead The Way, Inc.Principles of Engineering.
Controlling Program Flow with Looping Structures
9. ITERATIONS AND LOOP STRUCTURES Rocky K. C. Chang October 18, 2015 (Adapted from John Zelle’s slides)
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 5 Looping.
CS0007: Introduction to Computer Programming The for Loop, Accumulator Variables, Seninel Values, and The Random Class.
While loops. Iteration We’ve seen many places where repetition is necessary in a problem. We’ve been using the for loop for that purpose For loops are.
Round 1 Rules: Each person says the message only once No one can ask questions Do not write anything down.
PYTHON WHILE LOOPS. What you know While something is true, repeat your action(s) Example: While you are not facing a wall, walk forward While you are.
Python – Part 4 Conditionals and Recursion. Conditional execution If statement if x>0:# CONDITION print (‘x is positive’) Same structure as function definition.
Functions. What is a Function?  We have already used a few functions. Can you give some examples?  Some functions take a comma-separated list of arguments.
Infinite for Loop If you omit the test condition, the value is assumed to be TRUE so the loop will continue indefinitely unless you provide some other.
Control Structures Repetition or Iteration or Looping Part II.
What you asked me to teach…
Lesson 05: Iterations Class Chat: Attendance: Participation
Karel J Robot Chapter 6.
Review If you want to display a floating-point number in a particular format use The DecimalFormat Class printf A loop is… a control structure that causes.
While Loops in Python.
Topics Introduction to Repetition Structures
Iterations Programming Condition Controlled Loops (WHILE Loop)
While Loops (Iteration 2)
Sentinel logic, flags, break Taken from notes by Dr. Neil Moore
Control Structure Senior Lecturer
While Loops.
Iteration with While You can say that again.
Control Structure Senior Lecturer
Conditional Loops.
Sentinel logic, flags, break Taken from notes by Dr. Neil Moore
Introduction to pseudocode
Lesson 05: Iterations Topic: Introduction to Programming, Zybook Ch 4, P4E Ch 5. Slides on website.
More Loops.
Programming Fundamentals
Loop Strategies Repetition Playbook.
Python programming exercise
A LESSON IN LOOPING What is a loop?
CHAPTER 6: Control Flow Tools (for and while loops)
Python While Loops.
GCSE Computing:: While Loops
While Loops in Python.
Iteration – While Loops
Presentation transcript:

The Wait-Until-Event pattern Has it happened  Yes, it happened!

The Definite Loop pattern for k in range(...):... Determines how many times the loop iterates The body of the loop window = zg.GraphWin('Animation', 650, 430) x = 20 y = 50 radius = 5 for k in range(150): circle = zg.Circle(zg.Point(x, y), radius) circle.setFill('purple') circle.draw(window) x = x + 2 y = y + 1 radius = radius + k / 100 time.sleep(0.01)

The Definite Loop pattern for k in range(...):... window = zg.GraphWin('Animation', 650, 430) x = 20 y = 50 radius = 5 for k in range(150): circle = zg.Circle(zg.Point(x, y), radius) circle.setFill('purple') circle.draw(window) x = x + 2 y = y + 1 radius = radius + k / 100 time.sleep(0.01)

The Definite Loop pattern Run n times:... The Wait-Until-Event pattern Repeatedly:... Has the event occurred? If so, break out of the loop....

The Wait-Until-Event pattern Repeatedly:... Has the event of interest occurred? If so, break out of the loop.... Repeatedly: x = something input from the user if x is the “ sentinel ” value: break out of the loop Process x. Robot: Start moving. Repeatedly: Robot: Have you bumped into anything? If so, break out of the loop. Robot: Stop

The Wait-Until-Event pattern Expressed in Python using while True and break while True:... if the event of interest occurred: break... while True: msg = 'Enter int, or -1 to stop' x = int(input(msg)) if x == -1: break Process x. robot.go(..., 0) while True: sensor = 'BUMPS_AND_WHEEL_DROPS' bump = robot.getSensor(sensor) if bump[3] == 1 or bump[4] == 1: break robot.stop()

The Definite Loop pattern for k in range(...):... The Wait-Until-Event pattern while True:... if the event of interest occurred: break... Do the following 150 times: Draw a circle. Move and grow the circle. Repeatedly draw, move and grow a circle, stopping when the circle grows beyond the border of the window. Do blah until the user asks you to stop. Robot, go until you hit a wall. Robot, go until your sensors say that you have gone 10 centimeters.

Repeatedly draw, move and grow a circle, stopping when the circle grows beyond the border of the window. Do blah until the user asks you to stop. Robot, go until you hit a wall. Robot, go until your sensors say that you have gone 10 centimeters. The Wait-Until-Event pattern while True:... if the event of interest occurred: break...