Iteration / repetition refers to the execution of the same code multiple times in succession.
Repetition of a set of statements in a program is made possible using looping constructs.
- Looping constructs provide the facility to execute a set of statements in a program repetitively, based on a condition.
- The statements in a loop are executed again and again as long as the particular logical condition remains true.
- This condition is checked based on the value of a variable called the loop’s control variable.
- When the condition becomes false, the loop terminates.
1. For loop
The for statement is used to iterate over a range of values or a sequence . The for loop is executed for each of the items in the range. These values can be either numeric, or they can be elements of a data type like a string, list, or tuple.
- With every iteration of the loop, the control variable checks whether each of the values in the range have been traversed or not.
- When all the items in the range are exhausted, the statements within loop are not executed; the control is then transferred to the statement immediately following the for loop.
- While using a for loop, it is known in advance the number of times the loop will execute.
Syntax of for loop in python:
for <control-variable> in <sequence / items in range>:
<statements inside body of the loop>
2. Range( ) function
range() is a built-in function in Python. It is used to create a list containing a sequence of integers from the given start value up to stop value (excluding stop value), with a difference of the given step value . In function range(), start, stop and step are parameters.
- The start and step parameters are optional.
- If the start value is not specified, by default the list starts from 0.
- If step is also not specified, by default the value increases by 1 in each iteration.
- All parameters of range() function must be integers.
- The step parameter can be a positive or a negative integer excluding zero.
The function range() is often used in for loops for generating a sequence of numbers.
Syntax of range function python:
range([start], stop[, step])
3. While loop
The while statement executes a block of code repeatedly as long as the control condition of the loop is true.
- The control condition of the while loop is executed before any statement inside the loop is executed.
- After each iteration, the control condition is tested again and the loop continues as long as the condition remains true.
- When this condition becomes false, the statements in the body of loop are not executed and the control is transferred to the statement immediately following the body of the while loop.
- If the condition of the while loop is initially false, the body is not executed even once.
- The statements within the body of the while loop must ensure that the condition eventually becomes false; otherwise the loop will become an infinite loop, leading to a logical error in the program.
Syntax of while loop in python:
body of while
4. Break and continue statement
In certain situations, when some particular condition occurs, we may want to exit from a loop (come out of the loop forever) or skip some statements of the loop before continuing further in the loop. These requirements can be achieved by using break and continue statements, respectively.
The break statement alters the normal flow of execution as it terminates the current loop and resumes execution of the statement following that loop.
When a continue statement is encountered, the control skips the execution of remaining statements inside the body of the loop for the current iteration and jumps to the beginning of the loop for the next iteration. If the loop’s condition is still true, the loop is entered again, else the control is transferred to the statement immediately following the loop.
5. Nested loops
A loop inside a loop is called a nested loop.
Python does not impose any restriction on how many loops can be nested inside a loop or on the levels of nesting. Any type of loop (for/while) may be nested within another loop (for/while).