Fall 2009ACS-1805 Ron McFadyen1 Ch 7 Loops Alice has two control structures for controlling the repeated execution of statements Loop While.

Slides:



Advertisements
Similar presentations
AliceWhileLoop1 While Loop in Alice Stephen Cooper Wanda Dann Randy Pausch Barb Ericson Oct 2009.
Advertisements

Repetition everywhere – comparing while in a method and as an event Susan Rodger Duke University July 2010 modified July 2011.
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.
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?
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Alice: A Visual Introduction to Programming First Edition.
1 Fall 2009ACS-1805 Techniques for designing code Storyboards Text (aka pseudocode) Diagrams Flowcharts (a procedural technique shown in text) Directed.
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.
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.
Repeating Actions While and For Loops
Fall 2009ACS-1805 Ron McFadyen1 Functions A function is a collection of statement, similar to a method, but a function is defined to return a value to.
Introduction to Computers and Programming Lecture 9: For Loops New York University.
1 Parts of a Loop (reminder) Every loop will always contain three main elements: –Priming: initialize your variables. –Testing: test against some known.
1 Fall 2008ACS-1805 Techniques for designing code Storyboards Text (aka pseudocode) Diagrams Flowcharts (a procedural technique) Text presents one of these.
Introduction to Computers and Programming for Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to.
Fall 2007ACS-1805 Ron McFadyen1 Chapter 7 Repetition.
Fall 2007ACS-1805 Ron McFadyen1 Ch 7 Loops Alice has two control structures for controlling the repeated execution of statements Loop While.
Fall 2007ACS-1805 Ron McFadyen1 Chapter 3 Programming - Putting Together the Pieces.
Fall 2008ACS-1805 Ron McFadyen1 Ch 8 Recursion Recursion occurs when a method (or function) calls itself.
Fall 2009ACS-1805 Ron McFadyen1 Ch 8 Recursion Recursion occurs when a method (or function) calls itself.
Loops Repetition Statements. Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional.
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.
1 Ch. 7 Recursion similar to iteration in that you repeatedly do a little bit of the task and then “loop” again and work on a smaller piece - eventually.
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.
Programming: Simple Control Structures Alice. Control Statements We have been using Do in order and Do together to control the way instructions are executed.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Alice: A Visual Introduction to Programming Third Edition.
AEEE 195 – Repetition Structures: Part B Spring semester 2011.
1 Fall 2007ACS-1903 Techniques for designing code Storyboards Text (aka pseudocode) Diagrams Flowcharts (a procedural technique) Text presents one of these.
Saeed Ghanbartehrani Summer 2015 Lecture Notes #5: Programming Structures IE 212: Computational Methods for Industrial Engineering.
1 Ch. 6 Iteration (Loops) Loops repeat a set of instructions Two types of loops: –Definite loops ( for ) perform instructions explicit number of times.
Mathematical Expressions, Conditional Statements, Control Structures
Bunny Eat Broccoli Repetition – Simple loops and Conditional loops Susan Rodger Duke University July 2011.
Repetition: Definite Loops Alice. Repetition In many kinds of animations, especially simulations and games, some actions happen again and again. Example:
CPS120 Introduction to Computer Science Iteration (Looping)
Fall 2009ACS-1805 Ron McFadyen1 Chapter 2 Program Design & Implementation.
Repetition Structures Chapter 5 Part The While Instruction  Combines Loop and the condition that is also used in If/else statements  The loop.
Programming: Simple Control Structures
ForLoopsInAlice1 Stephen Cooper Wanda Dann Randy Pausch Barb Ericson Oct 2009 Counted (For) Loops in Alice.
Chapter 15: Recursion. Objectives In this chapter, you will: – Learn about recursive definitions – Explore the base case and the general case of a recursive.
CS320n –Visual Programming Definite / Counted Loops (Slides 7-1) Thanks to Wanda Dann, Steve Cooper, and Susan Rodger for slide ideas.
Repetition: Definite Loops Sec 53 Web Design. Objectives The Student will: Understand loops and why they are used Understand definitive loops Know how.
Repetition Repetition allows you to repeat an operation or a series of operations many times. This is called looping and is one of the basic structured.
Repetition everywhere – comparing while in a method and as an event Susan Rodger Duke University July 2010.
Repetition: Definite Loops Alice. Repetition In many kinds of animations, especially simulations and games, some actions happen again and again. Example:
1 Fall 2009ACS-1903 Ch 4 Loops and Files while loop do-while loop for loop Other topics later on …
While ( number
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 4-1 Lecture Objectives To understand: –what values can be stored in a Boolean.
Chapter 15: Recursion. Recursive Definitions Recursion: solving a problem by reducing it to smaller versions of itself – Provides a powerful way to solve.
Chapter 15: Recursion. Objectives In this chapter, you will: – Learn about recursive definitions – Explore the base case and the general case of a recursive.
Chapter 5: Looping. Using the while Loop Loop – A structure that allows repeated execution of a block of statements Loop body – A block of statements.
Jonathon Kuo Under the Direction of Dr. Susan Rodger
Chapter 4 Repetition Statements (loops)
Programming: Simple Control Structures
Chapter 3: Decisions and Loops
While: Indefinite Loops
Bunny Eat Broccoli Repetition – Simple loops and Conditional loops
Repetition Control Structures
Learning About the Loop Structure (continued)
Doing things more than once
Repetition: Definite Loops
Counted (For) Loops in Alice
Repetition: Definite Loops
Repetition: Definite Loops
Presentation transcript:

Fall 2009ACS-1805 Ron McFadyen1 Ch 7 Loops Alice has two control structures for controlling the repeated execution of statements Loop While

Fall 2009ACS-1805 Ron McFadyen2 Loop A definite loop is executed a specified number of times (also referred to as a counted loop) Also discussed back in Ch 3 Text: A bunny sneaks into a garden and wants to eat the broccoli. The bunny will need to hop several times to get to the broccoli.

Fall 2009ACS-1805 Ron McFadyen3 Flowchart for Loop index < limit? Loop action(s) false true Loop action(s) executed as an index variable starts at some initial value is incremented by some increment before a next iteration goes up to some limit Set index to initial value Increment index

Fall 2009ACS-1805 Ron McFadyen4 bunny.hop

Fall 2009ACS-1805 Ron McFadyen5 bunny.hop vs Less code More flexible Easier to comprehend

Fall 2009ACS-1805 Ron McFadyen6 Infinite Loop In an animation we may want for some activity to never stop, unless the world stops playing

Fall 2009ACS-1805 Ron McFadyen7 Nested Loops Each time the ferris wheel makes a revolution the smaller wheels must turn too The inner loop is executed twice (in this example) for each iteration of the outer loop FerrisWheel.a2w

Fall 2009ACS-1805 Ron McFadyen8 While A while loop is executed as long as some condition is true – also called a conditional loop or indefinite loop) Used in situations where we don’t know how many times a loop should execute, but we do know the condition for it to execute. Generally, we expect a while to terminate when something in the world causes the expression to be false. However, it could be coded such that it executes forever… an infinite loop

Fall 2009ACS-1805 Ron McFadyen9 While

Fall 2009ACS-1805 Ron McFadyen10 Text Example The Shark/Goldfish Chase Scene.

Fall 2009ACS-1805 Ron McFadyen11 Problem The problem is how do we get the shark to chase the goldfish in a chase-like action? The shark should not immediately catch the goldfish (otherwise, there would be no chase). The goldfish (assuming self-preservation instincts) should appear to be fleeing.

Fall 2009ACS-1805 Ron McFadyen12 Solution To create a chase scene, At the same time, the shark will swim a short distance toward the fish and the fish will swim a short distance away from the shark. The fish will flee. As long as the goldfish is still 0.5 meters away from the shark, repeat the actions.

Fall 2009ACS-1805 Ron McFadyen13 Storyboard chase While the goldfish is more than 0.5 meters away from the shark Do in order shark point at the goldfish Do together shark swim (toward the goldfish) goldfish flee (away from the shark) shark eat (the goldfish) Complex actions – become procedures themselves

Fall 2009ACS-1805 Ron McFadyen14 chase While the goldfish is more than 0.5 meters from the shark Do in order Point the shark at the goldfish Do together shark swim goldfish flee shark eat (goldfish) swim tDo in order urn torso left and move forward turn torso right and move forward turn torso left and move forward flee Do together wiggle tail move to random location Eat Parameter: what Do in order shark points at what shark opens jaw and what disappears shark closes jaw SharkGoldfishChase.a2w