Logic & Functions · Lesson 3 of 6
Python Conditions and Loops: if, for and while with Real Examples
Make decisions with if / elif / else, repeat work with for and while, and avoid the infinite loop. With programs you will actually write in lab exams.
Programs become useful when they can decide (conditions) and repeat (loops). Master these two and you can solve most beginner lab and placement coding questions.
if, elif, else
percentage = 76
if percentage >= 75:
grade = "Distinction"
elif percentage >= 60:
grade = "First class"
elif percentage >= 40:
grade = "Pass"
else:
grade = "Fail"
print(grade) # Distinction
Python checks conditions top to bottom and runs only the first block that is true. Order matters — if you checked >= 40 first, everyone above 40 would be "Pass".
Combine conditions with and, or, not:
if cgpa >= 7 and backlogs == 0:
print("Eligible for the drive")
for loops: when you know what to loop over
for i in range(1, 6): # 1, 2, 3, 4, 5 (the end value is excluded)
print(i, "x 7 =", i * 7)
subjects = ["DBMS", "OS", "CN"]
for subject in subjects:
print("Revise", subject)
range(start, stop, step) stops before stop. range(5) gives 0–4. range(10, 0, -2) gives 10, 8, 6, 4, 2.
while loops: when you do not know how many times
pin = ""
attempts = 0
while pin != "4321" and attempts < 3:
pin = input("Enter PIN: ")
attempts += 1
print("Unlocked" if pin == "4321" else "Card blocked")
A while loop must change something that eventually makes the condition false. If it does not, you get an infinite loop — press Ctrl + C to stop it.
break and continue
for n in range(2, 50):
if n % 7 == 0:
print("First multiple of 7:", n)
break # stop the loop completely
for n in range(1, 11):
if n % 2 == 0:
continue # skip the rest of this iteration
print(n) # prints only odd numbers
A classic interview program: is it prime?
n = int(input("Number: "))
if n < 2:
print("Not prime")
else:
for d in range(2, int(n ** 0.5) + 1):
if n % d == 0:
print("Not prime")
break
else:
print("Prime")
Two things worth explaining in an interview: we only check divisors up to the square root of n (anything larger would pair with a smaller divisor we have already checked), and the for ... else block runs only if the loop finished without hitting break.
Try it yourself: Print a multiplication table for any number the user enters; then print the sum of all even numbers from 1 to 100; then reverse a number (1234 → 4321) using a while loop and %.
Common interview questions
Try answering each one aloud before you open it.
1What is the difference between for and while loops?
for iterates over a sequence or range when the number of iterations is known; while repeats as long as a condition is true, useful when the number of iterations is not known in advance.
2What do break and continue do?
break exits the loop immediately; continue skips the rest of the current iteration and moves to the next one.
3What is the else clause on a loop?
It runs when the loop completes normally — that is, without a break. It is handy for search loops like checking whether a number is prime.
Practise this
Build something with it
FAQs
Does Python have a switch statement?
Python 3.10+ has match-case for pattern matching. For simple choices, if / elif / else is still the most common.
Is there a do-while loop?
No. Use while True: with a break when the condition is met.
References & further reading
Finished this lesson?
Mark it complete. Create a free account to save it on every device.