1 / 15

Repetition: Definite Loops

Repetition: Definite Loops. Sec 53 Web Design. Objectives. The Student will: Understand loops and why they are used Understand definitive loops Know how to add loops to an Alice Program. Repetition.

nasim-wong
Download Presentation

Repetition: Definite Loops

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Repetition: Definite Loops Sec 53 Web Design

  2. Objectives • The Student will: • Understand loops and why they are used • Understand definitive loops • Know how to add loops to an Alice Program

  3. 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

  4. 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.

  5. bunny.hop

  6. One solution • Creating the same instruction again and again is somewhat tedious and the code gets longer and longer.

  7. Counted Loop • A counted loop is an alternate way to write repetitive code • Repeats instructions a counted number of times

  8. Demo • Ch07Lec1BunnyHop • Concepts illustrated in this example • The loop instruction executes a definite number of times, specified by a count • Using a loop instruction • saves time • is convenient

  9. Demo • Ch07Lec1Carouselinfinity • 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

  10. More complicated loops • It is also possible to place a loop statement within another loop statement • This is called nested loops

  11. 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.

  12. Demo • Ch07Lec1FerrisWheel • 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.

  13. 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: Ch07Lec1LoopWithFunctionCall

  14. Summary • Definitive Loops are used to repeat something a certain number of times. • Use the Loop control structure add loops to Alice code. • Loops can be nested • Loops can depend on function calls to set the number of times the code will be executed.

  15. Assignment • Read Chapter 7-1 • Loops • Download Chapter 7 sec 1 questions • Do one of the two the chapter 7-1 exercises • Complete the questions

More Related