Looping Day Started!
Programming Session 3:
Hey everyone!
Today was Looping Day – the third session of my programming journey, and it felt like unlocking a superpower in coding. Loops help us repeat tasks without rewriting code over and over. So let’s dive into it!
🧠 Rules I Remember Before I Start
Before coding, I always keep these 6 self-made rules in mind to stay confident:
- ✅ Do the known actions – Start with what I know first.
- ❌ Don’t panic or fear the output – Errors are part of the learning.
- 💭 Think and search the very next step – Keep moving forward logically.
- 📦 Introduce variables if necessary – Store values if reused.
- 🔄 Small to big – Start simple, test, and then grow the logic.
- 🧘♂️ Stay calm, stay consistent – Progress takes time.
❓ What is a Loop?
A loop is a block of code that repeats again and again until a condition becomes false.
Instead of writing the same code multiple times, a loop does the job for us!
Example:
i = 0
while i < 3:
print("Hello")
i += 1
This will print "Hello" three times!
🤔 Why Do We Use Loops?
We use loops to:
- Save time and effort
- Avoid repeating code manually
- Perform tasks multiple times
- Make our code clean, short, and powerful
- Handle unknown repetitions like user inputs or real-time data
⏰ When Do We Use Loops?
Use loops when:
- You know or expect repetition in your task
- You need to perform a task until a condition is met
- You want to process items in a list, array, or dataset
- You’re waiting for correct input or a signal
📍 Where Do We Use Loops?
Loops are used in:
- Menu-driven programs
- Games (for continuous actions)
- Web forms (to check user input repeatedly)
- Data processing (looping through records)
- Automation tasks (running background jobs)
🛠️ How to Use a while
Loop?
A while
loop runs as long as the condition is true. Here's the basic structure:
# Step 1: Initialize
count = 0
# Step 2: Loop with condition
while count < 5:
print("Looping...")
count += 1 # Update to avoid infinite loop
⚠️ Important: If you don’t update the condition inside the loop, it will run forever!
📌 Practice Print (Loopprint)
Let’s print numbers from 1 to 5 using a loop:
num = 1
while num <= 5:
print("Number is:", num)
num += 1
Simple and sweet. That’s the beauty of loops.
✨ Final Thoughts
Today's session made me realize loops are the heart of automation in programming. With the right logic and patience, you can make your program smarter and shorter.
🔁 Don’t repeat yourself – use loops and let the computer do the hard work!