Loops
There are two types of loops.
1. Count Controlled Loop
2. Conditional Loop
a. Pre-Conditional Loop
b. Post-Conditional Loop
Count Controlled
FOR Loop is a count-controlled loop, which means it will increment the value of the variable
automatically for the set number of times before it ends.
Syntax for count-controlled loop (for loop)
for x in range(50):
print(x)
This loop will run for 50 times when executed. Again indentation is very important when
applying loops. The instructions which are to run for a set number of times should be
indented inside the loop.
Practice Question:
Take input of 5 student names and print “hello” at the start of each student name.
Solution:
for x in range(5):
stdname=str(input("Please enter the student name: "))
print("Hello",stdname)
Output:
Performing sum using count-controlled loop (for loop)
When performing sum or addition, remember to set the initial value to a variable to zero.
Define your count-controlled loop and within the loop instruction set, be sure to add the value
in the same variable
e.g.
if we are to find total of three numbers which are to be taken as an input from the user. We
will first define our total to be zero and then initiate the loop to executed the instructions of
taking the input and adding it to our total. Keep in mind that variable being set to zero should
not be a part of the loop, however, the added total should be a part of the loop.
Practice Question:
Take input of 5 numbers from the user and present its total at the end.
Solution:
sum = 0
for x in range(5):
number = float(input("Enter your number: "))
sum = sum + number
print (sum)
Output:
Largest Number Technique using count-controlled loop (for loop)
Same technique to be used as of sum, as you need to set the largest number to zero before
initiating the loop. Take the input as per said number of times and also include the
comparison of the numbers with the largest number. Finally setting the largest number to the
entered number and take the output.
Practice Question:
Take input of 5 numbers from the user and print the largest number.
Solution:
l_num = 0
for x in range(5):
number = float(input("Enter Number: "))
if number > l_num:
l_num = number
print(l_num)
Output:
Counting Technique using count-controlled loop (for loop)
Same technique to be used, as you need to set the variable to be counted as zero before
initiating the loop. Take the input as per said number of times and also include the
comparison of the values with the condition and increment the count if satisfies the condition.
Output the result by counting all entries which satisfies the condition.
Practice Question:
Take input of five numbers from the user and count how many are positive and negative
numbers being entered
Solution:
pos_num=0
neg_num=0
for x in range(5):
number=float(input("Please enter the number of your choice: "))
if number>0:
pos_num = pos_num+1
else:
neg_num = neg_num+1
print("There are",pos_num,"positive numbers")
print("There are",neg_num,"negative numbers")
Output:
Smallest Number Technique using count-controlled loop (for loop)
Same technique to be used as of sum, as you need to set the smallest number to maximum
before initiating the loop. Take the input as per said number of times and also include the
comparison of the numbers with the largest number. Finally assigning the smallest number to
the entered number and take the output.
Practice Question
Take input of 5 numbers from the user and print the smallest number.
Solution:
s_num = 9999
for x in range(5):
number = float(input("Enter Number: "))
if number < s_num:
s_num = number
print(s_num)
Output:
Conditional Loops
Repeat…..Until
While…..Do…..Endwhile
Conditional loops keeps on running till the condition is met/satisfied/True.
Syntax of While Loop:
while (condition):
print(“Hello world”)
Example:
Print “Hello World” 5 times using while loop
Code:
x=0
while x<5:
print("Hello World")
x=x+1
Output:
Practice Question:
Take input of 5 numbers from the user and print the sum of those numbers using while loop
and print the total at the end.
Solution:
x=0
total=0
while x<5:
number=float(input("Please enter your number: "))
total=total+number
x=x+1
print(total)
Output: