Summary

This document is a programming lesson on for loops, focusing on the concept of loops in programming. It shows examples of the range function and how to control loops, including setting start and step values.

Full Transcript

For Loops Unit 3 - Section 2 - Lesson 1 Learning Targets At the end of this lesson, you should be able to: Use basic for loops Use different range functions for loops Here is a basic for loop i is the index value range(4) contains 4 values in tells the for function to use i to count th...

For Loops Unit 3 - Section 2 - Lesson 1 Learning Targets At the end of this lesson, you should be able to: Use basic for loops Use different range functions for loops Here is a basic for loop i is the index value range(4) contains 4 values in tells the for function to use i to count the values in range As a result, Hello! is printed 4 times for loops Here we print the value of i instead Notice how range(4) contains 4 values, but starts at 0 ○ Counting starts at 0 ○ …except on the AP exam where it starts at 1 for some reason… for loops for loops This means that i will go up to but not including the number in range Codehs Practice View Example 4.6.3 For Loop Complete Exercise 4.6.4 Meme Text Generator Complete Exercise 4.6.5 The Worm Complete Exercise 4.6.6 Caterpillar More for loops More for loops We can tell the for loop to start at a value other than 0 by defining a range More for loops We can tell the for loop to start at a value other than 0 by defining a range You can also tell range to change i by a different step size than 1 More for loops You can set range to look at a variable value More for loops You can set range to look at a variable value This means you can set a variable to take an integer input and use that for the range Codehs Practice View Example 4.7.3 Countdown View Example 4.7.3 Count by Twos Complete Exercise 4.7.5 Count by Sevens Complete Exercise 4.7.6 Powers of Two

Use Quizgecko on...
Browser
Browser