Loops
What is a Loop?
A loop is like giving instructions to a robot. Instead of telling it to do the same thing over and over, you tell it to
repeat the action until you say stop.
Example in Real Life:
Imagine you want to say "Hello" to 5 people. Instead of saying it 5 times:
1. Hello
2. Hello
3. Hello
4. Hello
5. Hello
You can simply tell someone, "Say 'Hello' 5 times." That’s a loop! You give the instruction once, and it repeats
the action for you.
In Programming:
If you want to print numbers from 1 to 5, you don’t have to write:
Figure 1 with loops
Figure 2 wuthout loops
Types of Loops:
While Loop:
Use it when you're not sure how many times to repeat, but you know when to stop.
Example: You keep printing numbers until you reach 5.
see here what you do is first initialize the value of i=1 (i is the loop varaible) then a keyword came
While (jabtak), so this means reapeat whatever code is inside { } these bracket until the value of I become
equal or less than 5.
Here if we see inside currly bracket there is a print condition which prints value of i, so in 1st iteration
the value 1 will be printed then it will encounter a increment operator so the value of i will become 2 for the
second loop iteration
Do-While Loop:
It’s like a "while loop" but guarantees that the code runs at least once.
Example: Print a number, then check if it’s less than or equal to 5.
In this it first do whatever is written is {} then afterwards it check the condition in while
For Loop:
Use it when you know how many times to repeat something.
Example: You know you need to print 5 numbers.
Here for loop can be divided into 3 parts
1. Initialization – means giving the value to loop variable
2. Condition – here we the that condtion till when the loop should run
3. Increment or decrement – this is very important because after everyloop the value of “i” will be
increased by 1. So this helps in satisfying the condition
When you initialize I = 1 then in memory it stores I value to 1, afterwards it checks the condtion if the value of
I is less than 5 or not if it is less, it will increment it by 1 and excecutes whatever is in {} – this is called block
Question
Print numbers from 1 to 10
Write a program using a loop to print numbers from 1 to 10.
2. Sum of first 5 numbers
Write a program to calculate the sum of the first 5 natural numbers (1 + 2 + 3 + 4 + 5).
3. Print even numbers from 1 to 20
Write a loop to print all even numbers between 1 and 20.
4. Count the number of digits in a number
Write a loop to find how many digits are there in a number (e.g., 4567 has 4 digits).
5. Tracking Daily Steps
You’re tracking your steps for a week. Write a loop to enter your daily step count and calculate the total
steps for the week.
[Link] Flyers
Imagine you have 200 flyers to distribute in your neighborhood. Write a loop to calculate how many
houses you visit until all flyers are distributed, assuming each house takes 2 flyers.
[Link] Total Bill in a Supermarket
Write a program that keeps asking for the price of items in a cart and adds them until all items are
entered (use 0 as the signal to stop).
9. countdown from 1000 to 0 using for loop