![Page 1: For Loops. Challenge: Racer ● Simulate a race that says “Now on lap X” for 10 laps. ● Make X vary, so it says 1, then 2, then 3 ● Use only one output](https://reader036.vdocuments.mx/reader036/viewer/2022062801/56649e665503460f94b6112b/html5/thumbnails/1.jpg)
For Loops
![Page 2: For Loops. Challenge: Racer ● Simulate a race that says “Now on lap X” for 10 laps. ● Make X vary, so it says 1, then 2, then 3 ● Use only one output](https://reader036.vdocuments.mx/reader036/viewer/2022062801/56649e665503460f94b6112b/html5/thumbnails/2.jpg)
Challenge: Racer● Simulate a race that says “Now on lap X” for 10 laps.
● Make X vary, so it says 1, then 2, then 3
● Use only one output statement!
![Page 3: For Loops. Challenge: Racer ● Simulate a race that says “Now on lap X” for 10 laps. ● Make X vary, so it says 1, then 2, then 3 ● Use only one output](https://reader036.vdocuments.mx/reader036/viewer/2022062801/56649e665503460f94b6112b/html5/thumbnails/3.jpg)
Loops● A loop is used for repetitive behaviour
● A set of commands is placed in a code block
● A condition determines when the block stops repeating
![Page 4: For Loops. Challenge: Racer ● Simulate a race that says “Now on lap X” for 10 laps. ● Make X vary, so it says 1, then 2, then 3 ● Use only one output](https://reader036.vdocuments.mx/reader036/viewer/2022062801/56649e665503460f94b6112b/html5/thumbnails/4.jpg)
The For Loop
● Used when you know how many times something will happen
● Great for counting● Also often used with lists
![Page 5: For Loops. Challenge: Racer ● Simulate a race that says “Now on lap X” for 10 laps. ● Make X vary, so it says 1, then 2, then 3 ● Use only one output](https://reader036.vdocuments.mx/reader036/viewer/2022062801/56649e665503460f94b6112b/html5/thumbnails/5.jpg)
Parts of a For Loop
● Sentry Variable● Initial Value● Looping Condition● Increment Statement
![Page 6: For Loops. Challenge: Racer ● Simulate a race that says “Now on lap X” for 10 laps. ● Make X vary, so it says 1, then 2, then 3 ● Use only one output](https://reader036.vdocuments.mx/reader036/viewer/2022062801/56649e665503460f94b6112b/html5/thumbnails/6.jpg)
Sentry Variable● A special variable designed to control the loop
● In for loops, usually an integer
● Use a meaningful name when practical
● Traditionally use i if no other name makes sense
![Page 7: For Loops. Challenge: Racer ● Simulate a race that says “Now on lap X” for 10 laps. ● Make X vary, so it says 1, then 2, then 3 ● Use only one output](https://reader036.vdocuments.mx/reader036/viewer/2022062801/56649e665503460f94b6112b/html5/thumbnails/7.jpg)
Initial Value
● Give sentry some meaningful initial value
● Usually 0 or 1
![Page 8: For Loops. Challenge: Racer ● Simulate a race that says “Now on lap X” for 10 laps. ● Make X vary, so it says 1, then 2, then 3 ● Use only one output](https://reader036.vdocuments.mx/reader036/viewer/2022062801/56649e665503460f94b6112b/html5/thumbnails/8.jpg)
Looping Condition● A condition● Always involves sentry variable● Indicates when loop should continue
● If condition is true, loop will repeat
● When condition becomes false, loop will end
![Page 9: For Loops. Challenge: Racer ● Simulate a race that says “Now on lap X” for 10 laps. ● Make X vary, so it says 1, then 2, then 3 ● Use only one output](https://reader036.vdocuments.mx/reader036/viewer/2022062801/56649e665503460f94b6112b/html5/thumbnails/9.jpg)
Increment Statement● A line of code● Always involves sentry variable● Changes value of sentry● Usually i++, i--, I += something
● Must make it possible for condition to become false eventually
![Page 10: For Loops. Challenge: Racer ● Simulate a race that says “Now on lap X” for 10 laps. ● Make X vary, so it says 1, then 2, then 3 ● Use only one output](https://reader036.vdocuments.mx/reader036/viewer/2022062801/56649e665503460f94b6112b/html5/thumbnails/10.jpg)
Racer AlgorithmNew Program Racer by me
New integer variable lap starts at 1
For loop with lap going from 1 to 10 by 1
Output “Now on lap: “ + lap
End For loop
End racer
![Page 11: For Loops. Challenge: Racer ● Simulate a race that says “Now on lap X” for 10 laps. ● Make X vary, so it says 1, then 2, then 3 ● Use only one output](https://reader036.vdocuments.mx/reader036/viewer/2022062801/56649e665503460f94b6112b/html5/thumbnails/11.jpg)
Challenge: Backwards Racer
● Simulate a race that says “Now on lap X” for 10 laps.
● This time go backwards!● Make X vary, so it says 10, then 9, then 8…
● Use only one output statement!
![Page 12: For Loops. Challenge: Racer ● Simulate a race that says “Now on lap X” for 10 laps. ● Make X vary, so it says 1, then 2, then 3 ● Use only one output](https://reader036.vdocuments.mx/reader036/viewer/2022062801/56649e665503460f94b6112b/html5/thumbnails/12.jpg)
Counting Backwards● Use same for loop elements● Use larger value for initial sentry value
● Decrement variable each time through
● Check for smaller result
![Page 13: For Loops. Challenge: Racer ● Simulate a race that says “Now on lap X” for 10 laps. ● Make X vary, so it says 1, then 2, then 3 ● Use only one output](https://reader036.vdocuments.mx/reader036/viewer/2022062801/56649e665503460f94b6112b/html5/thumbnails/13.jpg)
Backwards Racer Algorithm
New Program backRacer by me
New integer variable lap starts at 10
For loop with lap going from 10 to 1 by -1
Output “Now on lap: “ + lap
End For loop
End racer
![Page 14: For Loops. Challenge: Racer ● Simulate a race that says “Now on lap X” for 10 laps. ● Make X vary, so it says 1, then 2, then 3 ● Use only one output](https://reader036.vdocuments.mx/reader036/viewer/2022062801/56649e665503460f94b6112b/html5/thumbnails/14.jpg)
Counting by 5 Algorithm
New Program byFive by me
New integer variable lap starts at 0
For loop with lap going from 0 to 50 by 5
Output “Now on lap: “ + lap
End For loop
End racer
![Page 15: For Loops. Challenge: Racer ● Simulate a race that says “Now on lap X” for 10 laps. ● Make X vary, so it says 1, then 2, then 3 ● Use only one output](https://reader036.vdocuments.mx/reader036/viewer/2022062801/56649e665503460f94b6112b/html5/thumbnails/15.jpg)
Code Tracing● Make a chart● Make each variable a column head
● Make a column for each condition
● Make a column for output
![Page 16: For Loops. Challenge: Racer ● Simulate a race that says “Now on lap X” for 10 laps. ● Make X vary, so it says 1, then 2, then 3 ● Use only one output](https://reader036.vdocuments.mx/reader036/viewer/2022062801/56649e665503460f94b6112b/html5/thumbnails/16.jpg)
Code Tracing Cont’d● Walk through code one line at a time
● Each time a variable changes, change it on the chart
● Each time you get to a condition statement, evaluate the condition
![Page 17: For Loops. Challenge: Racer ● Simulate a race that says “Now on lap X” for 10 laps. ● Make X vary, so it says 1, then 2, then 3 ● Use only one output](https://reader036.vdocuments.mx/reader036/viewer/2022062801/56649e665503460f94b6112b/html5/thumbnails/17.jpg)
Code Tracing III
● For conditions, write TRUE or FALSE
● Write any output
![Page 18: For Loops. Challenge: Racer ● Simulate a race that says “Now on lap X” for 10 laps. ● Make X vary, so it says 1, then 2, then 3 ● Use only one output](https://reader036.vdocuments.mx/reader036/viewer/2022062801/56649e665503460f94b6112b/html5/thumbnails/18.jpg)
Code Tracing Example
![Page 19: For Loops. Challenge: Racer ● Simulate a race that says “Now on lap X” for 10 laps. ● Make X vary, so it says 1, then 2, then 3 ● Use only one output](https://reader036.vdocuments.mx/reader036/viewer/2022062801/56649e665503460f94b6112b/html5/thumbnails/19.jpg)
![Page 20: For Loops. Challenge: Racer ● Simulate a race that says “Now on lap X” for 10 laps. ● Make X vary, so it says 1, then 2, then 3 ● Use only one output](https://reader036.vdocuments.mx/reader036/viewer/2022062801/56649e665503460f94b6112b/html5/thumbnails/20.jpg)
Ordinary Slide
● Level 1–Level 2
● Level 3
![Page 21: For Loops. Challenge: Racer ● Simulate a race that says “Now on lap X” for 10 laps. ● Make X vary, so it says 1, then 2, then 3 ● Use only one output](https://reader036.vdocuments.mx/reader036/viewer/2022062801/56649e665503460f94b6112b/html5/thumbnails/21.jpg)
CodeMy code
More Code
More Code