Session 3
This presentation is the property of its rightful owner.
Sponsored Links
1 / 10

Session 3 PowerPoint PPT Presentation


  • 66 Views
  • Uploaded on
  • Presentation posted in: General

Session 3. Loops. At the heart of their functionality, loops enable blocks of code to be executed more than once. Loops represent a simple concept that can be used to create extremely dynamic and flexible programs. Loops are very important for two reasons: efficiency and flexibility.

Download Presentation

Session 3

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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


Session 3

Session 3


Loops

Loops

  • At the heart of their functionality, loops enable blocks of code to be executed more than once.

  • Loops represent a simple concept that can be used to create extremely dynamic and flexible programs.

  • Loops are very important for two reasons: efficiency and flexibility.

  • In the second part of the project from the previous session, you were instructed to basically run the program twice. In order to accomplish this, you had to copy the existing blocks and append them to the end of the program effectively doubling the number of blocks present. It worked, but just imagine what would happen if you had to go through the program ten, one-hundred, or even a thousand times! Assuming you were able to create a program of that magnitude, what if a change had to be made? You would be required to make that change for each copy! Loops allow the program to contain only one copy of the code and go through it as many times as you need to. This is much more efficient.


The loop block

The Loop Block

  • In Alice, the simplest type of loop is the loop block. It closely resembles the “for” loop found in other programming languages.

  • The loop block expects the programmer to specify a number which will indicate exactly how many times it will repeat.

  • In this example, the move block will be repeated ten times. To find the total distance the cat will move, all we have to do is some quick multiplication to realize it will move 100 meters.

  • This block is used when the number of times to repeat is known before the block is encountered, but sometimes, there is no way of knowing exactly how many times a loop must execute. In cases of uncertainty, a slightly different loop must be used.


The while loop

The While Loop

  • The while loop, as the name implies, keeps repeating while a given Boolean expression is true.

  • It is identical to the while loop found in other programming languages.

  • While loops are perfect when combined with user input.

  • This allows the user to interact with the program indefinitely; loops such as these are found in almost every program in some form.

  • Although the two loops mentioned thus far appear to behave differently, they are the same loop. With the use of variables (discussed in session 4), the while loop can be made to work exactly like a regular loop.


Infinite loops

Infinite Loops

  • In some cases, a loop must continue repeating the entire time the program is running. When this is required an infinite loop is often used.

  • Like the while loop, the infinite loop is commonly used in conjunction with user input.


Nested loops

Nested Loops

  • Like the conditional branch structures in the previous session, loops can, and sometimes must, be nested. The behavior of these loops is often trivial, but sometimes they can be very complex.

  • This script moves the sprite 100 meters (2 * 10 * 5). What does this one do?


For all

For All

  • There are two additional loops in Alice which will not be covered in this session:

  • These loops are for use with lists and arrays which will be covered in session 7.


Project 3

Project 3

  • While the user chooses to continue the program, ask the user how many times to move an object 0.2 meters. Use the number provided by the user in the condition of a for loop which moves the object 0.2 meters that many times.

  • Flow of program:

    Would you like to continue? Yes/No

    (if yes) How many times would you like to move the object 0.2 meters? Answered with a number

    Uses a number with a for loop to move the object that many times.


Day 2 questions

Day 2 Questions

  • What part of an If/Else control structure determines which sequence of blocks is executed?

  • The expression in the predicate part of an If/Else structure evaluates to a or value?

  • What part of the structure would execute if the predicate value was true.

  • If a certain block or number of blocks needed to be executed several times, what control structure would you use?

  • If a loop executes 5 times and there was a block inside that moved the object 3 meters, how many meters would it move in total?

  • The expression in the predicate part of an If/Else structure evaluates to what type of value?

  • If the condition of a while loop was set to “true” how many times would this loop execute?


Day 2 answer key

Day 2 Answer Key

  • What part of an If/Else control structure determines which sequence of blocks is executed? predicate or conditional, Boolean expression, anything similar.

  • The expression in the predicate part of an If/Else structure evaluates to atrueor false value?

  • What part of the structure would execute if the predicate value was true. if part, consequent

  • If a certain block or number of blocks needed to be executed several times, what control structure would you use? loop,while loop

  • If a loop executes 5 times and there was a block inside that moved the object 3 meters, how many meters would it move in total? 15 meters

  • The expression in the predicate part of an If/Else structure evaluates to what type of value? Boolean, true or false

  • If the condition of a while loop was set to “true” how many times would this loop execute? Infinity times, until the user stops the program from running


  • Login