Counted (For) Loops in Alice

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.
As you come in…  Sign in (in back) and pick up  Badge  Name Card – write your first name LARGELY on back  Any questions? (of any kind)  Put on index.
Execution Control with If/Else and Boolean Questions Example: Multiple Conditions Alice.
Repetition Structures
Fall 2009ACS-1805 Ron McFadyen1 Ch 7 Loops Alice has two control structures for controlling the repeated execution of statements Loop While.
Programming Logic and Design, Third Edition Comprehensive
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.
Repeating Actions While and For Loops
CS320n –Visual Programming Random Numbers and Random Motion (Slides 6-3) Thanks to Wanda Dann, Steve Cooper, and Susan Rodger for slide ideas.
Fall 2007ACS-1805 Ron McFadyen1 Ch 7 Loops Alice has two control structures for controlling the repeated execution of statements Loop While.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
An Object-Oriented Approach to Programming Logic and Design Chapter 6 Looping.
Copyright 2011 Wanda Dann, Steve Cooper, Don Slater Alice Workshop Implementation Algorithm  Code World.my first method Control blocks Statements (methods,
Functions and Conditionals in Alice 1 Stephen Cooper Wanda Dann Barb Ericson September 2009.
01-Intro-Object-Oriented-Prog-Alice1 Barb Ericson Georgia Institute of Technology Aug 2009 Introduction to Object-Oriented Programming in Alice.
Recursion1 Stephen Cooper Wanda Dann Randy Pausch Barb Ericson Jan 2010 Recursion in Alice.
REPETITION STRUCTURES. Topics Introduction to Repetition Structures The while Loop: a Condition- Controlled Loop The for Loop: a Count-Controlled Loop.
Study Guide For Test Chapter 5, 6,& 7 Test is Friday, May 15th.
Getting started with Alice Adapted from presentations by Jenna Hayes, Duke University Donna Gavin, UWP Computer Science and Software Engineering.
CPS120 Introduction to Computer Science Iteration (Looping)
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.
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)
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
Programming: Simple Control Structures MMP 220 Multimedia Programming This adapted material was prepared for students in MMP220 as as part of a curriculum.
ForLoopsInAlice1 Stephen Cooper Wanda Dann Randy Pausch Barb Ericson Oct 2009 Counted (For) Loops in Alice.
Obj: Programming: Simple Control Structures HW: Read section 3 – 2 AC3 D2 Do Now: 1.Log on to Alice. Open the file firstEncounter.a2w located in the folder.
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.
Programming: Simple Control Structures Sec 46 Web Design.
Chapter 7: Repetition Structure (Loop) Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills.
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:
Today… The for loop. Introducing the Turtle! Loops and Drawing. Winter 2016CISC101 - Prof. McLeod1.
Jonathon Kuo Under the Direction of Dr. Susan Rodger
Lists and Iteration in Alice
EasyCode Foundations Vocabulary Terms.
Programming: Simple Control Structures
Repetition Structures Chapter 9
Chapter 3: Decisions and Loops
Topics Introduction to Repetition Structures
Programming: Simple Control Structures
Bunny Eat Broccoli Repetition – Simple loops and Conditional loops
Repeating Actions.
Control Structures
Topics Introduction to Repetition Structures
Programming: Simple Control Structures
Repetition: Definite Loops
Programming: Simple Control Structures
Iteration: Beyond the Basic PERFORM
Design and Implementation
Programming: Simple Control Structures
Introduction to scratch animation
Repetition: Definite Loops
Programming: Simple Control Structures
Repetition: Definite Loops
Introduction to Object-Oriented Programming in Alice
Programming: Simple Control Structures
Topics Introduction to Repetition Structures
The structure of programming
Thinking procedurally
Programming: Simple Control Structures
Presentation transcript:

Counted (For) Loops in Alice Stephen Cooper Wanda Dann Randy Pausch Barb Ericson July 2009 04-ForLoopsInAlice

Learning Goals Introduce the concept of a counted loop A loop that keeps a counter or index Show how a loop leads to easier to read and shorter code Show an infinite for loop Show a nested loop Show a function call inside a loop 04-ForLoopsInAlice

Repetition In many kinds of animations, especially simulations and games, some actions happen again and again. Example: Gallery games where targets appear randomly on screen and then disappear only to appear elsewhere in the scene. Of course, actions are made to happen again and again by running an animation instruction (or a method) more than once 04-ForLoopsInAlice

Example 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. 04-ForLoopsInAlice

bunny.hop 04-ForLoopsInAlice

One solution Creating the same instruction again and again is somewhat tedious and the code gets longer and longer. 04-ForLoopsInAlice

Counted Loop A counted loop is an alternate way to write repetitive code Repeats instructions a counted number of times 04-ForLoopsInAlice

Demo BunnyHop Concepts illustrated in this example The loop instruction executes a definite number of times, specified by a count or index Using a loop instruction saves time is convenient 04-ForLoopsInAlice

Demo Carouselinfinity Concept illustrated in this example If “Infinity times” is selected for a loop, this means the loop will run until the program is shut down This is an infinite loop 04-ForLoopsInAlice

More complicated loops It is also possible to place a loop statement within another loop statement This is called a nested loop 04-ForLoopsInAlice

An example of nested loops The whole Ferris wheel will rotate clockwise, while the two inner wheels will rotate counterclockwise. The inner wheels should perform 2 revolutions for each outer loop revolution. 04-ForLoopsInAlice

Demo FerrisWheel Concept illustrated in this example The inner loop runs completely each time the outer loop runs once. An outer loop that executes 2 times and an inner loop that executes 5 times will actually execute the inner loop 10 times. In an animation, opposite actions that occur simultaneously effectively cancel each other out in terms of the rendered action. To demonstrate, set the outer loop to 1 time and the inner loop to 2 times. The inner wheel will look as though it rotates only once. As another example, you would see the same kind of result if you try to turn a person's head left and right ¼ revolution at the same time. 04-ForLoopsInAlice

Using a function A loop count can be computed by calling a function that returns a number value. The loop instruction automatically rounds the returned value to the nearest whole number. Demo: LoopWithFunctionCall 04-ForLoopsInAlice

Challenges And have the bunny hop in a square using a nested loop Copy BunnyHop So that as the bunny nears the broccoli Papa rabbit (use Hare from the Animals folder) appears at the garden gateway and taps his foot Have the bunny turn and hop out of the garden Using a loop Copy BunnyHop And have the bunny hop in a square using a nested loop Loop 4 times Loop 3 times Bunny hop Bunny turn ¼ revolution 04-ForLoopsInAlice

Summary A for loop is similar to a while loop But is useful when you need a counter or index in the loop An infinite loop never stops Loops can be nested inside of other loops You can call a function inside the test for a loop 04-ForLoopsInAlice