Download presentation
Presentation is loading. Please wait.
Published byCathleen Barker Modified over 9 years ago
1
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Alice: A Visual Introduction to Programming Third Edition by Tony Gaddis Chapter 5: Repetition Structures
2
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley The Loop Instruction Loop instructions cause one or more other instructions to repeat or loop a certain number of times. 5-2 5.1
3
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley The Loop Instruction The Loop instruction has an empty slot where instructions can be inserted. 5-3 5.1
4
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Task #1 Complete Tutorial 5-1 on pages 187-188 called using the loop instruction Complete Tutorial 5-2 on pages 189-192 called Creating a user-controlled loop. 5-4
5
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Computing the Number of Times to Repeat What if you want the something to loop and NOT have to specify a particular number of repetitions? –For example…getting a ball to roll across the screen, regardless of where the ball is initially placed –Use the soccerBall’s distance to function to calculate the distance –Loop uses integers (whole numbers) and decimal portions are discarded –Objects loop 4 times, not 4.8; 3 times, not 3.4 5-5 5.1
6
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Infinite Loops Infinite Loops…the loop NEVER stops! –Use for objects that shouldn’t stop! If placed in consecutive order…the next instructions will NEVER occur, since the loop NEVER ends! Place an infinite loop into a Do Together structure with other items. 5-6 5.1
7
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Infinite Loops Infinite Loops…the loop NEVER stops! –Use for objects that shouldn’t stop! If placed in consecutive order…the next instructions will NEVER occur, since the loop NEVER ends! Place an infinite loop into a Do Together structure with other items. 5-7 5.1
8
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Flowcharting a Loop Instruction 5-8 Set index to 0 Index < 12? Clock, minute roll left 1 revolution Clock, hour roll left 1/12th revolution Add 1 to index T 5.1
9
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley The While Instruction While instruction is a loop that repeats as long as a Boolean condition is true Second repetition structure Called conditional loop…since the loop is controlled by a condition 5-9 5.2
10
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Flowcharting a While Instruction Loop’s condition is tested before each repetition of the loop First, it tests condition –If true…performs a repetition and starts over –If false…the loop terminates 5-10 5.2 Start Distance > 0? Racket turns forward Ball moves forward End
11
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Task #2 Create Tutorial 5-3 on pages 198 -201 which is called Using a while instruction to make and object vanish. Create Tutorial 5-4 on pages 202-203 which is called Using a While Loop to Make and Object smaller. Create Tutorial 5-5 on pages 204-206 which is called Using the While instruction to move an object 5-11
12
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Nested Loops A nested loop is a loop that is inside of another loop. The inner loop goes through all its iterations for every single iteration of the outer loop. 5-12
13
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley End Of The Chapter Skills Task - Students will then be assigned the following skills programs) valued at 50 points each 1,2,3 The students will then complete the following challenge programs that will be worth 100 points. 4 and 5 5-13
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.