Introduction

A loop is a control structure that allows a block of code to be executed repeatedly until a specified condition is met. Loops help reduce code duplication and make programs more efficient.

Definition

A loop is a programming construct used to execute a set of statements repeatedly based on a condition or for each item in a sequence.


Types of Loops in Python

Python provides two main types of loops:

  1. for Loop
  2. while Loop

Python also supports:

  • Nested Loops
  • Loop Control Statements (break, continue, and pass)

1. for Loop

The for loop is used to iterate over a sequence such as a list, tuple, string, or range.

Syntax

for variable in sequence:
    # statements

Example 1: Print Numbers from 1 to 5

for i in range(1, 6):
    print(i)

Output

1
2
3
4
5

Example 2: Print Characters of a String

name = "Python"

for ch in name:
    print(ch)

Output

P
y
t
h
o
n

The range() Function

The range() function generates a sequence of numbers.

Syntax

range(start, stop, step)

Examples

for i in range(5):
    print(i)

Output

0
1
2
3
4
for i in range(2, 11, 2):
    print(i)

Output

2
4
6
8
10

2. while Loop

The while loop executes a block of code as long as the condition is True.

Syntax

while condition:
    # statements

Example

count = 1

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

Output

1
2
3
4
5

Infinite Loop

If the condition in a while loop never becomes False, the loop runs forever.

Example

while True:
    print("Hello")

Note: This is an infinite loop and must be stopped manually (e.g., by pressing Ctrl + C in the terminal).


Nested Loops

A nested loop is a loop inside another loop.

Example

for i in range(1, 4):
    for j in range(1, 4):
        print(i, j)

Output

1 1
1 2
1 3
2 1
2 2
2 3
3 1
3 2
3 3

Loop Control Statements

1. break

The break statement immediately terminates the loop.

Example

for i in range(1, 11):
    if i == 6:
        break
    print(i)

Output

1
2
3
4
5

2. continue

The continue statement skips the current iteration and continues with the next iteration.

Example

for i in range(1, 6):
    if i == 3:
        continue
    print(i)

Output

1
2
4
5

3. pass

The pass statement is a placeholder that does nothing. It is used when a statement is required syntactically but no action is needed.

Example

for i in range(5):
    if i == 2:
        pass
    print(i)

Output

0
1
2
3
4

Difference Between for and while Loops

for Loopwhile Loop
Used when the number of iterations is knownUsed when the number of iterations is unknown
Iterates over a sequenceRuns until a condition becomes False
Simpler for sequence traversalMore flexible for condition-based repetition

Advantages of Loops

  • Reduce code repetition.
  • Make programs shorter and easier to maintain.
  • Increase code readability.
  • Automate repetitive tasks.
  • Improve program efficiency.

Applications of Loops

  • Printing tables
  • Searching data
  • Processing arrays and lists
  • Pattern printing
  • File processing
  • Data analysis
  • Repeating calculations

Related Posts