Loops
Loops
While Loops - 1
#Increment
x=5;
x=x+1;
print(x); # 6 (Incrementing the value x by 1)
y=6;
y=y+4;
print(y); # 10 (Incrementing the value of y by 4)
#Decrement
x=5;
x=x-1;
print(x); # 4 (Decrementing the value x by 1)
y=6;
y=y-4;
print(y); # 2 (Decrementing the value of y by 4)
x=10;
x+=1;
x=x+5;
x=x+10;
print(x); # 26
While Loops - 2
x-=10;
x=x-5;
print(x); # 11
a=10;
c=a+1;
b=10;
d=b+1;
print(a); # 10
print(b); # 10
print(c); # 11
print(d); # 11
a=6;
b=a-1;
c=10;
d=c-1;
print(a); # 6
print(b); # 5
print(c); # 10
print(d); # 9
Loops
Take the example of SPOTIFY or GANA.com, there is a thing called a loop, that
plays something over and over again.
While Loops - 3
Guests Coming ➖ There are 10 guests coming to my home, After 2-3 days they
decided to leave their home. They all have the train on the same day and at the
same time. I need to drop them off at the railway station but I have one bike which
can only take one person at a time.
In this case, I need to drop each guest one by one. Taking the First guest to the
railway station, dropping them off, and arriving back and following the same
procedure again and again till the end.
While Loops - 4
Loop Examples ➖
Days of the week ➖
While Loops - 5
# Sunday->Monday->Tuesday->Wednesday->Thrusday->Friday->Saturday->Sunday
Alarm clocks give an alarm only when they match the time or condition by
a person else clock hand moving in the loop without failing.
While Loops - 6
Python has two primitive loop commands:
while loops
for loops
While Loop
The while loop begins with a condition and it is written like an if statement. The inner
parenthesis is the condition.
Each time the loop is executed, the variable will change and eventually become
larger or less than the number in the condition, stopping the loop.
Give an example of a 1m jump till the person is reaching the 100m mark
While Loops - 7
SYNTAX ➖
starting_point # called as initialization
how_many_jump_at_a_time # Increment/decrement
While Loops - 8
Table for DRY RUNNING.
While Loops - 9
Dry run again after removing the increment statement to make them understand the
importance of it.
Show it on REPLIT.
i=1;
while(i<=10):
print("Hello World");
i=i+1;
i=1;
while(i<=5):
print(i);
i+=1;
While Loops - 10
Do a dry run by making a table.
jump=1;
while(jump<=100):
print("jump", jump); #will print jump 50 times
jump=jump+2;
jump=1;
while(jump<=100):
print("jump", jump);
jump=jump+5;
jump=1;
while(jump<=100):
print("jump", jump);
jump=jump+15;
➖
Note You can start from any point not necessarily
from 1 or 0.
Talk about Reverse Loops also, using the decrement operation,
taking the same marathon example, make them think about the condition as well.
While Loops - 11
position = 100;
while(position>=0):
print("Current Position",position);
position = position - 1
While Loops - 12
Break & Continue
Break
Break means to come out of the loop and stop the execution.
Guest Analogy
There are 10 guests coming to my home, After 2-3 days they decided to leave
their home.
They all have the train on the same day and at the same time.
I need to drop them off at the railway station but I have one bike which can only
take one person at a time.
Taking the First guest to the railway station, dropping them off, and arriving back
and following the same procedure again and again till the end.
Suppose I took the First Guest and dropped him to the Railway station and
come back.
While Loops - 13
Now, Next, I took the third guest to the Railway station and found that Train has
gone.
guest=1;
while(guest<=10):
if(guest==4):
break;
print("Guest", guest);
guest+=1;
'''
Output
Guest 1
Guest 2
Guest 3
'''
While Loops - 14
Do the dry run using a table.
Continue
Continue is basically saying go back to the condition.
Guest Analogy
There are 10 guests coming to my home, After 2-3 days they decided to leave
their home.
They all have the train on the same day and at the same time.
I need to drop them at the railway station but I have one bike which can only
take one person at a time.
Taking the First guest to the railway station, dropping them off, and arriving back
and following the same procedure again and again till the end.
Suppose I took the First Guest and dropped him to the Railway station and
come back.
Suppose the third guest is Sick, In that case, I will skip him.
and I will continue with the fourth guest and follow the same procedure.
guest=0;
while(guest<=10):
guest+=1;
if(guest==3):
continue;
print("Guest", guest);
'''Problem:
Sum of 1 to 10
1 + 2 + 3....... + 10 '''
While Loops - 15
var i = 1;
var sum = 0;
while(i<=10)
{
sum = sum+i;
i++;
}
console.log(sum);
i = 1
while i < 6:
print(i)
i += 1
else:
print("i is no longer less than 6")
Code 12: Problem 1: Print the numbers from the given starting
point till the ending point (including both start and end)
starting = 5;
ending = 25;
while(starting<=ending):
print(starting);
starting+=1;
Code 13: Problem 2: Print the odd numbers from 0 till the given
limit
start = 2;
limit = 50;
while(start<limit):
if(start % 2 == 1):
While Loops - 16
print(start);
start = start + 1;
Code 14: Problem 3: Print the sum of all the multiples of 3 from 0
to the given limit
start = 0;
limit = 20;
sum = 0
while(start<=limit):
if(start % 3 == 0):
sum=sum+start;
start=start+1
print(sum);
starting = 1;
ending = 100;
sum_of_even = 0;
count_even_numbers = 0;
while(starting<=ending):
if(starting%2 == 0):
sum_of_even = sum_of_even + starting;
count_even_numbers+=1;
starting+=1;
average = sum_of_even/count_even_numbers
print(average);
Happy Coding!
While Loops - 17