12.2.2. Decisions, loops#

Decision making

var = 100
if var == 100:
    print("value of expression is 100")
value of expression is 100

While loop

count = 0
while count < 9:
    print("The count is: ", count)
    count = count + 1
The count is:  0
The count is:  1
The count is:  2
The count is:  3
The count is:  4
The count is:  5
The count is:  6
The count is:  7
The count is:  8

While loop with else statement

count = 0
while count < 5:
    print(count, " is  less than 5")
    count = count + 1
else:
    print(count, " is not less than 5")
0  is  less than 5
1  is  less than 5
2  is  less than 5
3  is  less than 5
4  is  less than 5
5  is not less than 5

For loop

for letter in "Python":  # First Example
    print("Current Letter :", letter)

fruits = ["banana", "apple", "mango"]
for fruit in fruits:  # Second Example
    print("Current fruit :", fruit)
Current Letter : P
Current Letter : y
Current Letter : t
Current Letter : h
Current Letter : o
Current Letter : n
Current fruit : banana
Current fruit : apple
Current fruit : mango

Iterating by sequence index

fruits = ["banana", "apple", "mango"]
for index in range(len(fruits)):
    print("Current fruit :", fruits[index])
Current fruit : banana
Current fruit : apple
Current fruit : mango

For loop with else

for num in range(10, 20):  # to iterate between 10 to 20
    for idx in range(2, num):  # to iterate on the factors of the number
        if num % idx == 0:  # to determine the first factor
            jdx = num / idx  # to calculate the second factor
            print("%d equals %d * %d" % (num, idx, jdx))
            break  # to move to the next number, the #first FOR
    else:  # else part of the loop
        print(num, "is a prime number")
        break
10 equals 2 * 5
11 is a prime number

Nested loops

val = 2
while val < 100:
    val2 = 2
    while val2 <= (val / val2):
        if not (val % val2):
            break
        val2 = val2 + 1
    if val2 > val / val2:
        print(val, " is prime")
    val = val + 1
2  is prime
3  is prime
5  is prime
7  is prime
11  is prime
13  is prime
17  is prime
19  is prime
23  is prime
29  is prime
31  is prime
37  is prime
41  is prime
43  is prime
47  is prime
53  is prime
59  is prime
61  is prime
67  is prime
71  is prime
73  is prime
79  is prime
83  is prime
89  is prime
97  is prime

Break statement

for letter in "Python":  # First Example
    if letter == "h":
        break
    print("Current Letter :", letter)

var = 10  # Second Example
while var > 0:
    print("Current variable value :", var)
    var = var - 1
    if var == 5:
        break
Current Letter : P
Current Letter : y
Current Letter : t
Current variable value : 10
Current variable value : 9
Current variable value : 8
Current variable value : 7
Current variable value : 6

Continue statement

for letter in "Python":  # First Example
    if letter == "h":
        continue
    print("Current Letter :", letter)

var = 10  # Second Example
while var > 0:
    var = var - 1
    if var == 5:
        continue
    print("Current variable value :", var)
Current Letter : P
Current Letter : y
Current Letter : t
Current Letter : o
Current Letter : n
Current variable value : 9
Current variable value : 8
Current variable value : 7
Current variable value : 6
Current variable value : 4
Current variable value : 3
Current variable value : 2
Current variable value : 1
Current variable value : 0

Pass statement

for letter in "Python":
    if letter == "h":
        pass
        print("This is pass block")
    print("Current Letter :", letter)
Current Letter : P
Current Letter : y
Current Letter : t
This is pass block
Current Letter : h
Current Letter : o
Current Letter : n