Loops

Sections

  • What is meant by Loop
  • For Loops
  • While" loops
  • break" and "continue" statements
  • Can we use "else" clause for loops?
  • Looping control statements

Section1- What is meant by Loop

Def:Loops are another essential aspect of control flow in Python. They allow you to repeat a block of code multiple times until a certain condition is met.  DefWe use loops in programming to repeat the execution of a block of code instead of repeating the entire code

Python provides two primary loop structures: for and while. The for loop is used when you know the number of iterations in advance, such as iterating over a list or a range of numbers. On the other hand, the while loop continues executing as long as a specified condition remains true.

There are two types of loops in PythonFor and While.

Section 2- For Loops

For loops iterate over a given sequence. Here is an example [1]. if we know the exact number of times to repeat the loop for, we use a "for" loop. The syntax has 4 parts, the initialization, test expression, modifying expression, and the code block to be executed in a loop[1].

For Loop Example:
fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
print(fruit)
primes = [2, 3, 5, 7]
for prime in primes:
    print(prime)

For loops can iterate over a sequence of numbers using the "range" and "xrange" functions. The difference between range and xrange is that the range function returns a new list with numbers of that specified range, whereas xrange returns an iterator, which is more efficient. (Python 3 uses the range function, which acts like xrange). Note that the range function is zero based[1].

# Prints out the numbers 0,1,2,3,4
for x in range(5):
    print(x)
# Prints out 3,4,5
for x in range(3, 6):
    print(x)
# Prints out 3,5,7
for x in range(3, 8, 2):
    print(x)

Section 2-"While" loops

While loops repeat as long as a certain boolean condition is met. For example:

# Prints out 0,1,2,3,4
count = 0
while count < 5:
    print(count)
    count += 1  # This is the same as count = count + 1
# While loop
count = 1
while count<5:
    print(count)
    count += 1
1
2
3
4
# While loop with else block
count = 1
while count<5:
    print(count)
    count += 1
else:
    print('End of else')
1
2
3
4
End of else
# Infinite While Loop
count = 1
while True:
    print(count, end=' ')
    count += 1

Section3- break" and "continue" statements

The execution of a loop can be controlled using the following keywords - break - terminates the loop (or switch) -statement and transfers execution to the statement directly after the loop - continue - will cause the loop to process to the next element skipping the current one - goto LABEL - transfers control to the specified label. IT IS NOT ADVISED TO USE THIS STATEMENT!

the break is used to exit a for loop or a while loop, whereas continue is used to skip the current block, and return to the "for" or "while" statement. A few examples:

# Prints out 0,1,2,3,4
count = 0
while True:
    print(count)
    count += 1
    if count >= 5:
        break
# Prints out only odd numbers - 1,3,5,7,9
for x in range(10):
    # Check if x is even
    if x % 2 == 0:
        continue
    print(x)

Section 4-Can we use "else" clause for loops?

unlike languages like C,CPP.. we can use else for loops. When the loop condition of "for" or "while" statement fails then code part in "else" is executed. If break statement is executed inside for loop then the "else" part is skipped. Note that "else" part is executed even if there is a continue statement.

# Prints out 0,1,2,3,4 and then it prints "count value reached 5"

count=0
while(count<5):
    print(count)
    count +=1
else:
    print("count value reached %d" %(count))

# Prints out 1,2,3,4
for i in range(1, 10):
    if(i%5==0):
        break
    print(i)
else:
    print("this is not printed because for loop is terminated because of break but not due to fail in condition")

Section 5-Looping control statements

for i in range(1,11):
    if i%2==1:
        continue
    print(i)
2
4
6
8
10
for i in range(1,11):
    if i%5==0:
        break
    print(i)
1
2
3
4
for i in range(5):
    pass

print('Nothing done')
Nothing done

References

1-learn-cpp

Last modified: Thursday, 26 October 2023, 12:08 PM