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.
Instead of manually repeating print or calculation statements, loops allow executing instructions N times cleanly.
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 i in range(1, 10):
print(i)Example 2: Iterating over a String
for g in "gagan":
print(g)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).
a = int(input("enter your number = "))
while a <= 100:
print(a)
a = a + 1Nested 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.
for i in range(1, 10):
for j in range(11, 20):
print(i, j)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 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.
i = 1000
while True:
print(i)
i = i + 1
if i > 5:
breaka = 500
while True:
print(a)
a += 50
if a > 1000:
breakBreak & 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.
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.
for i in range(1, 6):
if i == 3:
continue
print(i)3. Combining break and continue
for i in range(1, 6):
if i == 2:
continue
if i == 4:
break
print(i)Registration & Auth Systems
1. User Registration & Login Verification
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
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.")Summary & Comparison
| Loop Type | Condition Check Timing | Minimum Iterations | Common Use Case |
|---|---|---|---|
while loop | Pehle check hoti hai (Entry-controlled) | 0 times | Unknown number of iterations based on condition |
do-while loop (simulated) | Baad me check hoti hai (Exit-controlled) | At least 1 time | Menus or initial prompt requiring at least one run |
for loop | Sequence length / range based | 0 or N times | Known number of steps or iterating over collections |