Py06: Loop

Loop is another control statement besides the decision statement. Loop statements are used to repeat a block of codes more than one time. Loops are used to process a sequence of data like lists and tuples.

Introduction

Loop statements allow a program to do repetitive actions. Python has a while loop and a for-loop. Python doesn't have a do-while loop structure, we will see an alternative.

while loop

to execute a block of statements repeatedly until the condition is True. While loop is used when we do not know how many times to repeat.



 

The while-loop syntax:

while condition:
    statement(s)

condition is an expression that evaluates to true or false.

count = 0

while count < 3:
    print(count)
    count += 1

Output

0
1
2
> 

for loop

For-loop is used when we know how many times to repeat.

The for-loop syntax:

for index in sequence:
    statement(s)

The range() is a function that generates a sequence. The range(n) generates a sequence of numbers starting from value 0 until n-1. For example, range(10) will generate 0 until 9. The default step is 1, which means the value is incremented by 1.

for x in range(10): 
    print(x)

Output

0
1
2
3
4
5
6
7
8
9
> 

We can specify the starting value by using the range(start, end) to display from the start value to the end value. For example, range(3, 11) will generate a sequence of numbers from 3 until 10, with step value 1.

for x in range(3, 11): 
    print(x)

Output

3
4
5
6
7
8
9
10
> 

To specify step value, range(start, end, step). This creates a sequence starting from the start until the end with the step specified. For example, range(4, 11, 2) generates a sequence starting from 4 until 11 with step 2. (4, 6, 8, 10)

for x in range(4, 11, 2): 
    print(x)

Output

4
6
8
10
> 

Since lists are iterable, we also can use a list in a for loop.

for x in [3, 2, 4, 5, 1]: 
    print(x)

# or
aList = [3, 2, 4, 5, 1]
for y in aList: 
    print(y)

Nested Loop

We can write a loop inside of another loop. Loops written like this are called nested loops. The code below shows a nested loop involving two "for" loops. We can also use a "while" loop or a combination of a "while" and "for" loop to form a nested loop.

for condition_outer:

    for condition_inner:
        statement(s)_inner

    statement(s)_outer

The nested loop has an outer loop and an inner loop. When the outer loop starts, it will run the inner loop. Once the inner loop is complete, the next iteration of the outer loop will be executed. The inner loop will start a new one. The whole nested loop will complete after outer loop finish execution.

The break statement

A break statement is used to terminate a loop immediately. A break statement is written in an if statement within a loop statement.

For example, displaying numbers from 0 to 10 stops printing if the number is 5.

for num in range(0, 10):
    print(num)
    if num == 5:
        print("break...")
        break

Output

0
1
2
3
4
5
break...
> 

The continue statement

A continue statement is used to skip the current iteration and continue the next one. Continue statement is also used in an if statement.

For example to display numbers from 0 to 10 but not the number 5.

for index in range(0, 10):    
    if index == 5:       
        continue

    print(index)

Output

0
1
2
3
4
6
7
8
9
> 

Do while loop

While loop may not execute the statement if the condition is false the first time. The do-while loop executes the statements at least once. This is because the condition is checked last.

While loop is not supported in Python, we can make a while-loop behave like one.

while True:
    statement(s)
    # if statement to continue or stop the loop

This is done by using the True boolean value as the condition, so it at least runs once. A setting like this will create an infinite loop, so we use the if statement to execute the break statement to stop the loop.

while True:
    inputStr = input("Type anything, q for quit: ")
    
    if inputStr != 'q':
        print("You entered: ", inputStr)
    else:
        break

Output

Type anything, q for quit: Hello
You entered:  Hello
Type anything, q for quit: red ball 
You entered:  red ball 
Type anything, q for quit: q
> 

A post by Cuber

Comments

Popular posts from this blog

Drawing Simple Pie Chart

VB.net connecting to SQL Server

VB.net