Iteration: Lines of code are repeated a set number of times or until a condition is met.
Count Controlled iteration: Code is repeated a number of times
Condition Controlled iteration: Code is repeated until a condition is met
For Loop: A type of Count Controlled loop.
Increment: A value to increase by (e.g. + 1)
for i in range (startnum, endnum, increment):
code to repeat here...
The i is a variable name created for the loop. It can be any name, but most programmers use "i" for integer.
For loops are a type of Count controlled iteration.
Useful if you know how many times you need to loop.
EXAMPLE:
for i in range (0,10,1):
print (i)
will output: 0 1 2 3 4 5 6 7 8 9
But not 10, as it stops there. "I" starts at 0, then goes up by 1 when all the indented code is finished.
Look at the code for each question.
What do you think will be the output?
What is the actual output?
Code in each part of if and else is INDENTED to show it belongs
What will be the output for the following code:
for i in range (0,2):
print("Hi")
Look at the code on the right.
Run it and see what it does.
Click on the pencil icon at the top to see the code again.
FIRST EXAMPLE:
Run the program and see what it does
Change the values of the variables to higher numbers. What happens?
Let's flip it around. Make startNum bigger than endNum. What happens? Is there an error?
Change increment to a negative number. What happens now?
Help:
print ( ) is used to output text
input( ) is used to get the user to type something
Variables store values that can change.
Name = Value here
https://www.w3schools.com/python/python_for_loops.asp
RECAP
Boolean: a data type that can hold one of two values (true or false)
> Symbol: Greater than
< Symbol: Less than
== Symbol: Compares if both values are the same
!= Symbol: Not equal to
>= Symbol: Greater than or equal to
<= Symbol: Less than or equal to
Selection -
if (condition) :
(indent) program here....
Iteration -
for varName in range (start, end, increment):
(indent) program here....
Create a program that counts from the number 1 all the way to 100!
What will be the output for the following code:
for i in range (0,10):
print(i)
What will be the output for the following code:
for number in range (1,5 ):
print(number)
What will be the output for the following code:
for i in range (10,1, -1):
print(i)
What will be the output for the following code:
for i in range (1,11):
print(i, "x 2 =", i * 2 )
myPassword = "abc123"
for counter in range(0,len(myPassword)):
print("Position",counter,"of my password is",myPassword[counter])
Create a program for a rocket launch!
Countdown from 10 to 1.
Then output "Blast off!!!"
Create a program that prints "Hi" five times in total.
Mini Challenge:
Then change it to seven times in total.
Create a program that asks the user to enter five numbers by using a loop. Output the sum of all the five numbers.
e.g.
User enters 10
User enters 5
User enters 5
User enters 9
User enters 1
Output "Total is 30"
Create a program that asks the user for a sentence. Use a for loop that goes through every character and output how many times the letter "e" is in that sentence.
(you need to use "if" and to use the for loop counter to find the positions of the variable: myVariable [ ])
CHALLENGE:
Create a program to output the song "100 bottles of milk on the wall".
Each verse goes:
100 bottles of milk on the wall!
100 bottles of milk!
Take one down,
pass it around!
There are 99 bottles of milk on the wall!
This continues until there are 0 bottles of milk on the wall.
CHALLENGE:
Create a program that asks the user to enter a number.
Output "*" on the first line
Output "**" on the second line
Output "***" on the third line
and so on until you reach the number entered.
e.g.
User inputs 5.
Output:
*
**
***
****
*****
CHALLENGE:
Create a program called Fizz Buzz.
Use a For loop to output numbers from 0 to 100, but:
If the number is divisible by 3: output "Fizz" instead of the number
If the number is divisible by 5: output "Buzz" instead of the number
If it is divisible by BOTH 3 and 5: output "Fizz Buzz" instead of the number
e.g.
1
2
fizz
4
buzz
fizz
7
8
fizz
fizzbuzz
11
...