Python Loops & Control Flow: if-elif-else, for, while, break, continue, pass

🤸 Week 2 of 180 — Python finally has a BRAIN now! 🧠 Last session I learned Lists — how to store data. Today? I learned how to control what Python DOES with that data. 💥 Loops & Control Statements — and this is where programming starts feeling like actual logic. 👇 🤔 Why do we even need this? Without control flow, Python just runs every line top to bottom. Boring. 😴 With control flow, Python can: → Make decisions using if-elif-else → Repeat tasks using for or while → Exit early, skip steps, or do nothing using break, continue, pass These are the backbone of logic in every Python program. 🦴 🧭 Part 1 — if-elif-else (Making Decisions) age = 18 if age >= 18: print("You can vote.") # ✅ this runs elif age >= 16: print("Almost there!") # skipped else: print("You cannot vote.") # skipped if checks the first condition. elif checks the next one if if was False. else is the fallback — runs only when everything above fails. 🛡️ 🔁 Part 2 — for Loop (Iterate over sequences) Use for when you know exactly how many times to loop. names = ["Alice", "Bob", "Charlie"] for name in names: print(name) # Alice # Bob # Charlie Works on lists, strings, dictionaries — anything iterable! 🔄 ⏳ Part 3 — while Loop (Run until condition is False) Use while when you DON'T know how many times to loop. i = 1 while i <= 5: print(i) i += 1 # prints 1 2 3 4 5 ⚠️ Always update your variable inside while — or you'll be stuck in an infinite loop forever. 😂 🎮 Part 4 — break, continue, pass # break → EXIT the loop completely for i in range(10): if i == 5: break # stops at 5, never reaches 6,7,8... # continue → SKIP this step, go to next for i in range(5): if i == 2: continue # skips 2, prints 0,1,3,4 # pass → DO NOTHING (placeholder) for i in range(3): pass # valid empty loop, no error! 🎯 Part 5 — range() + else with Loops # range() — generate number sequences for i in range(1, 6): print(i) # 1 2 3 4 5 # else with loop — runs when loop completes WITHOUT break for i in range(3): print(i) else: print("Loop finished successfully!") # 🎉 That else with loops was a surprise — I didn't know Python could do that! 😲 💡 The Big Mental Model I have now: if-elif-else = Python DECIDING 🤔 for = Python REPEATING a known number of times 🔁 while = Python WAITING until something changes ⏳ break/continue/pass = Python CONTROLLING the flow 🎮 📅 Week 2 / 180 — Loops & Control: DONE! ✅ Grinding through my 6-Month GenAI & Agentic AI Certificate Program 🏛️ IIT Patna Certified while still_learning: keep_going() # this is me right now 😂 💬 Which one confused you more when you started — break or continue? 👇 ♻️ Repost if this made control flow click for you! #Python 🐍 #Loops #ControlFlow #IfElse #ForLoop #WhileLoop #Week2 #GenAI 🤖 #IITPatna 🏛️ #LearningInPublic #100DaysOfCode #PythonBasics #CodingJourney #AgenticAI #NeverStopLearning 🚀 #BuildInPublic

  • No alternative text description for this image

To view or add a comment, sign in

Explore content categories