Python For Loop & While Loop Control Flow Statements

In this article, we will discuss loop control flow statements in Python, which function similarly to those in other programming languages like C, C++, and Java.

Loops are essential when you need to execute the same block of code multiple times to achieve the desired result. In Python, you can control this repetitive execution with loop control statements.

Python has two primary types of loops:

  1. Python While Loop
  2. Python For Loop

1. Python WHILE Loop

A while loop keeps running as long as a given condition is true. Once the condition becomes false, the loop stops. The syntax for a while loop is similar to if-else statements.

Syntax:

				while condition:
    # Statement(s) to execute while the condition is true
    statement1
    statement2
    ...
    last_statement

			

Example:

				count = 0
while count < 5:
    print(count)
    count += 1

			

In this example, the loop will print the numbers 0 through 4. The loop continues as long as the condition (count < 5) remains true.

Python while loop example

2. Python For Loop

Another loop statement – for loop, is the best for when you determine in advance how many times you need to execute the block of statements placed in the loop.

It allows you to perform an operation on each element in a list or character in a string.

Syntax:

				for variable in sequence:
    # Statement(s) to execute for each item in the sequence
    statement1
    statement2
    ...
    last_statement

			

Example:

				fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
    print(fruit)

			

In this example, the for loop iterates over each item in the fruits list and prints it. The loop runs once for each item in the list, making it a straightforward way to process collections of data.

Python offers two main types of for loops: the List – for loop and the Range – for loop.

2.1 List – for Loop

The List – for loop is straightforward. You provide a list of items, and the loop iterates over each item in that list.

Example:

				for integer in [0, 1, 2]:
    print("Integer :" + str(integer))
    print("Integer Multiplication :" + str(integer * integer))

			

In this example, the loop processes each integer in the list, printing the integer and its square.

Output:

				Integer :0
Integer Multiplication :0
Integer :1
Integer Multiplication :1
Integer :2
Integer Multiplication :4

			
python for Loop - List

2.2 Range – for Loop

The Range – for loop provides more control over the sequence of numbers you want to iterate through. You can specify the start, end, and step values to define the range.

Syntax:

				for var in range(start_pos, end_pos, step):
    # Statement(s)

			
  • start_pos: The starting point of the range (inclusive).
  • end_pos: The end point of the range (exclusive).
  • step: The increment between each value in the range.

Example 1:

				for i in range(1, 20, 2):
    print("Range value :" + str(i))

			

In this example, the loop prints numbers from 1 to 19, incrementing by 2 each time.

 

Example 2: Decrement python loop

				for i in range(20, 0, -2):
    print("Range value :" + str(i))

			

Here, the loop prints numbers from 20 to 2, decrementing by 2 each time.

python for Loop - Range Decremental loop

Summary

In Python, for loops come in two main types: List – for loops and Range – for loops.

  • List – for loops are used to iterate over each item in a list, executing a block of code for each item.
  • Range – for loops provide control over the sequence of numbers by specifying the start, end, and step values, allowing for more flexible iterations.

Both types of loops help simplify repetitive tasks and manage how code is executed across a sequence of values.

Leave a Comment