← Back to Home

Python Loops

By Instructor

A loop is a programming structure that is used to repeat a block of code multiple times until a specific condition is met.

This comprehensive guide covers for loops, while loops, nested loops, do-while loop simulation, break and continue control statements, and authentication application flows.

By Instructor8 chaptersPython 3.xBeginner to Advanced
loops_demo.py
for i in range(1, 6):
    if i == 3:
        continue
    print(i)
python loops_demo.py → 1, 2, 4, 5
Chapter 1

What is a Loop

A loop is a programming structure that is used to repeat a block of code multiple times until a specific condition is met.

Loops eliminate code duplication by automating repetitive iterations across ranges, sequences, or condition-driven tasks.

Key Purpose

Instead of manually repeating print or calculation statements, loops allow executing instructions N times cleanly.

Chapter 2

The for Loop

The for loop is used to repeat a block of code a fixed number of times or to iterate over a sequence like a list, string, or range.

For loop tab use hota hai jab hume koi kaam fixed number of times repeat karna ho ya kisi list, string ya range ke elements ko ek-ek karke access karna ho.

Example 1: Iterating over range()

range(1, 10) generates numbers from 1 to 9 (upper bound non-inclusive).

for_range.py
for i in range(1, 10):
    print(i)

Example 2: Iterating over a String

for_string.py
for g in "gagan":
    print(g)
Chapter 3

The while Loop

A while loop repeats a block of code as long as the condition remains true.

While loop tab tak chalta hai jab tak condition true rehti hai. It evaluates conditions before executing the body (Entry-controlled loop).

while_counter.py
a = int(input("enter your number = "))

while a <= 100:
    print(a)
    a = a + 1
Chapter 4

Nested Loops

A nested loop is a loop inside another loop. Jab ek loop ke andar doosra loop use hota hai to use nested loop kehte hain.

The inner loop completes all its iterations for every single step of the outer loop.

nested_loops.py
for i in range(1, 10):
    for j in range(11, 20):
        print(i, j)
Chapter 5

Do-While Loop Simulation

A do-while loop is a loop that executes the code block at least once and then checks the condition.

Do-while loop me pehle code run hota hai aur baad me condition check hoti hai, isliye loop kam se kam ek baar zaroor run hota hai.

Python Implementation

Python does not have a native do-while keyword. We simulate exit-controlled loops using an infinite while True: loop with an internal if condition: break statement.

do_while_sim.py
i = 1000

while True:
    print(i)
    i = i + 1
    if i > 5:
        break
step_increment_loop.py
a = 500

while True:
    print(a)
    a += 50
    if a > 1000:
        break
Chapter 6

Break & Continue Statements

Loop control statements alter normal execution flow:

1. break Statement

The break statement is used to immediately terminate the loop. Break statement loop ko turant stop kar deta hai.

break_demo.py
for i in range(1, 6):
    if i == 7:
        break
    print(i)

2. continue Statement

The continue statement skips the current iteration and moves to the next iteration of the loop. Continue statement current step ko skip kar deta hai aur loop next iteration pe chala jata hai.

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

3. Combining break and continue

combined_control.py
for i in range(1, 6):
    if i == 2:
        continue
    if i == 4:
        break
    print(i)
Chapter 7

Registration & Auth Systems

1. User Registration & Login Verification

registration_login.py
name = input("enter your name: ")
age = int(input("enter your age: "))

username = input("create username: ")
pin = int(input("create PIN: "))

print("registration successful")

userlogin = input("Enter username: ")
loginpin = int(input("Enter PIN: "))

if userlogin == username and loginpin == pin:
    print("login successful")
elif userlogin != username and loginpin == pin:
    print("Wrong username")
elif userlogin == username and loginpin != pin:
    print("wrong pin")
else:
    print("wrong username and pin")

2. Admin Portal Multi-tier Nested Control

admin_portal.py
username = input("Enter your username: ")

if username == "admin":
    print("Welcome admin!")
    pin = int(input("Enter your pin: "))
    if pin == 1234:
        print("Access granted.")
        name = input("Enter your name: ")
        print("Hi", name, "welcome to our login system")
        age = int(input("Enter your age: "))
        if age >= 18:
            print("You are eligible to use our services")
        else:
            print("You are not eligible to use our services")
    elif pin != 1234:
        print("Wrong pin")
else:
    print("Error: You are not an admin.")
Chapter 8

Summary & Comparison

Loop TypeCondition Check TimingMinimum IterationsCommon Use Case
while loopPehle check hoti hai (Entry-controlled)0 timesUnknown number of iterations based on condition
do-while loop (simulated)Baad me check hoti hai (Exit-controlled)At least 1 timeMenus or initial prompt requiring at least one run
for loopSequence length / range based0 or N timesKnown number of steps or iterating over collections