Repetition Structures Chapter 5 Part 2 5-1. The While Instruction  Combines Loop and the condition that is also used in If/else statements  The loop.

Slides:



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

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?
Class-level Methods Chapter 6. Class-level Method Is specific to a class of objects We can give a class new abilities/methods Only involves this one class.
Review of Chapter 4 Sections 1 and 2 World-level methods involve two or more objects break a large problem into smaller, logical units follow a design.
The LC-3 – Chapter 6 COMP 2620 Dr. James Money COMP
Decision Structures Chapter 4. Chapter 4 Objectives To understand: o What values can be stored in a Boolean variable o What sequence structures are and.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Alice: A Visual Introduction to Programming First Edition.
Events Chapter 7. Interactivity The real world is interactive User determines order of actions instead of programmer.
Events Chapter 7. Interactive Real world is interactive User determines order of actions instead of programmer.
Execution Control with If/Else and Boolean Questions Example: Multiple Conditions Alice.
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.
Programming: Simple Control Structures Part 1 – Conditional Execution Alice.
Repetition: Definite Loops Alice. Repetition In many kinds of animations, especially simulations and games, some actions happen again and again. Example:
Lights Camera Action! Part 3: BDE Events By Deborah Nelson under the direction of Professor Susan Rodger Duke University July 2008.
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 2007ACS-1805 Ron McFadyen1 Ch 7 Loops Alice has two control structures for controlling the repeated execution of statements Loop While.
Fall 2008ACS-1805 Ron McFadyen1 Ch 8 Recursion Recursion occurs when a method (or function) calls itself.
Functions and Conditionals in Alice 1 Stephen Cooper Wanda Dann Barb Ericson September 2009.
Programming: Simple Control Structures Alice. Control Statements We have been using Do in order and Do together to control the way instructions are executed.
Lights Camera Action! Part 2: Controlling Light in Alice By Elizabeth Liang under the direction of Professor Susan Rodger Duke University June 2009.
Variables and Functions Chapter Variables Named storage location in computer’s memory Programs may need to store data when running o Stored in.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley This week: Whew!!! The last homework was tough! The homework for this week.
Branching and Looping Examples, cont’d. Remember the generic triple jump world…
Class-level Methods Chapter 6 part 1. Classes and Objects Classes o In Alice, classes are predefined as 3D models Objects o An object is an instance of.
Decision Structures Chapter 4 Part 2. Chapter 4 Objectives To understand o What relational operators are and how they are used o Boolean logic o Testing.
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.
Four Fundamental Pieces Instruction Control Structure Function Expression.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Alice: A Visual Introduction to Programming Third Edition.
Mathematical Expressions, Conditional Statements, Control Structures
Variables and Functions Chapter Variables Named storage location in computer’s memory Programs may need to store data when running Types of data.
Repetition: Definite Loops Alice. Repetition In many kinds of animations, especially simulations and games, some actions happen again and again. Example:
Programming: Simple Control Structures
ForLoopsInAlice1 Stephen Cooper Wanda Dann Randy Pausch Barb Ericson Oct 2009 Counted (For) Loops in Alice.
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.
Programming: Simple Control Structures Sec 46 Web Design.
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:
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 4-1 Lecture Objectives To understand: –what values can be stored in a Boolean.
An Introduction to Programming Using Alice 2.2, Second Edition Chapter 7 Recursive Algorithms.
Jonathon Kuo Under the Direction of Dr. Susan Rodger
Programming: Simple Control Structures
Chapter 3: Decisions and Loops
Programming: Simple Control Structures
While: Indefinite Loops
Repeating Actions.
Simple Control Structures
Repetition Control Structures
JavaScript: Control Statements I
Programming: Simple Control Structures
Repetition: Definite Loops
Programming: Simple Control Structures
Counted (For) Loops in Alice
Programming: Simple Control Structures
Repetition: Definite Loops
Programming: Simple Control Structures
Repetition: Definite Loops
Programming: Simple Control Structures
Programming: Simple Control Structures
Presentation transcript:

Repetition Structures Chapter 5 Part 2 5-1

The While Instruction  Combines Loop and the condition that is also used in If/else statements  The loop repeats as long as its Boolean condition is true  Called conditional loop since loop is controlled by a condition  Also called pretest loop since a test has to pass before it does the instructions in the loop o Otherwise it skips those instructions 2

While Instruction Flowchart Loop’s condition is tested before each repetition of loop o If true it performs a set of instructions and loop starts over o If false then the loop terminates 3

Inserting While Statement Drag While tile up to Method Editor Choose True or False as placeholder from popup menu 5

Proximity Example  Tennis racket is rotated 0.06 revolutions until it is next to the ball  Ball moves forward looking like it was hit  Uses  Function call  Relational operation 5

Tennis Racket Flowchart Test loop’s condition: is racket behind ball? o Yes? Turn racket forward 0.06 revolutions o No? Simulate racket hitting ball by moving forward 5 meters 6 Start Racket Distance Behind ball > 0? Racket turns forward 0.06 revolution Ball moves forward 5 meters End Yes No

Object Slowly Disappears Magician says Abracadabra In While loop o Cookie reduces in opacity by 10% until opacity is 0% or cookie disappears Uses o Opacity property o Relational operation o Uses math to change while condition Notice Condition is changed in While 7

Change Size in While In Alice in Wonderland, Alice drinks something which causes her to shrink which allows her to go thru a small doorAlice drinks While loop is used to constantly shrink her by half until she is smaller than door Uses o Two function calls o Relational operation Notice Condition is changed in While 8

Use While to Move an Object Helicopter moves to scuba diver In While loop Helicopter descends to diver ½ meter at time until 1 meter above diver o Notice style is abruptly  Prevents jerky movement After While is finished ladder is lowered to diver Event turns blades with infinite loops 9

Shark Chases Fish Example  Common feature in popular "action films" is an exciting chase scene  Hungry shark chases after and catches a fleeing fish o Shark should not immediately catch goldfish  Otherwise there would be no chase o Goldfish should appear to be fleeing 10

Shark Chases Fish Solution To create a chase scene o Shark will swim short distance toward fish o At same time fish will swim short distance away from shark o Fish will flee to a random location o As long as goldfish is 0.5 meter away from shark repeat above actions 11

Algorithm : Shark Chases Fish 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) shark swim, goldfish flee, and shark eat actions are complex Use stepwise refinement to break them down into simple steps 12

Review Why is the While instruction considered a conditional loop? What causes the While loop to stop repeating? Why is the While instruction called a pretest loop? 13

Nested Loops A nested loop is a loop that is inside of another loop The inner loop executes all its iterations for every single iteration of the outer loop How many times does the bee pace or move forward? 14

Ferris Wheel Example Whole Ferris wheel will rotate clockwise Inner loop runs completely each time outer loop runs once 15

Ferris Wheel Example Outer loop executes 10 times and inner loop executes 2 times How many times does inner wheels rotate? o Inner loop executed 20 times  2 wheel inner rotations * 10 outer rotations 16

Homework Read chapter 5 Do lab assignments Due in one week 17