# Loops, part 2
# FOR loops
# First, recall WHILE loops
# When to use WHILE loops?
# Can use it whenever you need a loop
# 3 parts to construct a WHILE loop
# 1. Initialize the loop variable(s)
# 2. Loop condition using loop variable(s)
# 3. Change the loop variable(s)
# FOR loop created for convenience
# "syntactic sugar"
# ANY FOR loop can be replaced with a WHILE loop
# Sometimes we can convert a WHILE loop to a FOR loop
# FOR loop structure
# for in :
# ```
for i in range(0, 10):
print('i is %d' % i)
# The above loops will have i start at 0
# and repeat until i is 10 <-- 10 is not part of it!
# range() function creates a "list" of values
# range(a, b, c) -> [a, a+c, a+2c, ... b-c]
range(1, 10, 3) -> [1, 4, 7]
# range() function does not include
# the last value in its list
# So now: Why can we not always replace
# WHILE loops with FOR loops?
# Suppose user gives us a number of repetitions
# How do we write that as a FOR loop?
N = raw_input('How many times? ')
N = int(N)
for cnt in range(0, N):
# A FOR loop is a counting loop
# That's what it does
# Is a WHILE a counting loop?
# Can be - but also much more
# Can a FOR loop do this?
a = 0
while (a<3):
a = raw_input('A value: ')
a = int(a)
# No - can't know how many times to repeat
# 3 parts of WHILE loop:
# 1. initialize loop variable
# 2. Condition with loop variable
# 3. change loop variable
for x in range(0, 10):
# comparable to:
x = 0
while x<10:
x = x + 1
# For loops does not require us to
# explicitly state the initialization,
# test, or increment steps
# Create a FOR loop which adds the
# ODD integers from 0 to 100
total = 0
for n in range(1, 100, 2):
total = total + n
# Same loop but start range at 0
total = 0
for n in range(0, 100, 2):
total = total + (n+1)
# Same loop but step size of 1
total = 0
for n in range(0, 100):
if n%2==1:
total = total + n
# Add up the values which are divisible by
# 17 and lie in the range 3 to 97,
# inclusive
total = 0
for n in range(3, 98):
if n%17==0:
total = total + n
total = 0
for n in range(17, 98, 17):
total = total + n
```