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!
Top comments (0)