Software Development (MCS-203L)                                              SSUET/QR/114
LAB # 05
                          ITERATION WITH LOOPS
OBJECTIVE
To get familiar with the different types of loops.
THEORY
A loop can be used to tell a program to execute statements repeatedly. In other word, to
keep a computer doing useful work we need repetition, looping back over the same block
of code again and again.
Type of Loop Statements in Python:
There are 2 types of loop statements in Python language. They are,
 1. for
 2. while
The for loop:
A Python for loop iterates through each value in a sequence.
Syntax:
In general, the syntax of a for loop is:
 for var in sequence:
      # Loop body
for loop can be used to simplify the preceding loop:
a)      for i in range(endValue):
      #Loop body
b)      for i in range(initialValue, endValue):
     # Loop body
c)      for i in range range(initialValue, endValue,k): #k=step value      # Loop body
How for loop works:
•    A ‘sequence’ holds multiple items of data, stored one after the other. In sequence
     introduce strings and data storing techniques.They are sequence-type objects in
     Python.
•    The variable ‘Var’ takes on each successive value in the sequence, and •     The
     statements in the body of the loop are executed once for each value.
Laraib Shahzad                               Sir Syed University Of Engineering and Technology 1
2020F-BMCS-004
Software Development (MCS-203L)                                          SSUET/QR/114
•   Generate a sequence of numbers using ‘range() function’, range(10) will generate
    numbers from 0 to 9 (10 numbers).
Example program for ‘for loop’:
#simple for loop using sequence(string) for letter
in 'Python':
   print ('Current Letter :', letter)
Output:
>>> %Run task1.py
Current Letter : P
Current Letter : y
Current Letter : t
Current Letter : h
Current Letter : o Current
Letter : n
Example program for ‘for loop using range( )’:
 #Simple for loop using range() for i in
 range(1,6):
    print("Programming is fun")
Output:
 >>> %Run task2.py
 Programming is fun
 Programming is fun
 Programming is fun
 Programming is fun
 Programming is fun
The while loop:
A while loop executes statements repeatedly as long as a condition remains true.
Syntax:
The syntax of a while loop in Python programming language is:
 while loop-continuation-condition:
   # Loop body
     Statement(s)
How while loop works:
Laraib Shahzad                           Sir Syed University Of Engineering and Technology 2
2020F-BMCS-004
Software Development (MCS-203L)                                             SSUET/QR/114
In while loop first the condition (boolean expression) is tested; if it is false the loop is
finished without executing the statement(s). If the condition is true, then the statements
are executed and the loop executes again and again until the condition is false. Each loop
contains a loop-continuation-condition, a Boolean expression that controls the body’s
execution.
Example program for ‘while loop’:
count = 0 while count < 5:
 print("Programming is fun!")                    count
 += 1
Output:
 >>> %Run task3.py
 Programming is fun!
 Programming is fun!
 Programming is fun!
 Programming is fun! Programming is fun!
EXERCISE
A.Point out the errors, if any, in the following Python programs.
1. Code
 for(;;) {
    printf("SSUET") }
Output:
2. Code
Laraib Shahzad                              Sir Syed University Of Engineering and Technology 3
2020F-BMCS-004
Software Development (MCS-203L)                                       SSUET/QR/114
 count = 4 while
 n < 8
      count = count + 3:
 print(count)
Output:
3.Code
  for v in range(4:8) print(v)
Output:
B.What will be the output of the following programs:
 1.Code
   i = 1 while i < 10:
   if i % 2 == 0:
   print(i)
   i += 1
Output
Laraib Shahzad                        Sir Syed University Of Engineering and Technology 4
2020F-BMCS-004
Software Development (MCS-203L)                                     SSUET/QR/114
 2.Code
   i = 1 while
   i>0:
   print(i)
   i = i + 1
   Output
1
2
3
4
5
6
7
8
9
.
.
.
.
.
.
infinity
 3.Code
   for v in range(3, 9, 2):
   print(v)
Output
C. Write Python programs for the following:
Laraib Shahzad                      Sir Syed University Of Engineering and Technology 5
2020F-BMCS-004
Software Development (MCS-203L)                                             SSUET/QR/114
1. Write a program that prints the first 10 natural numbers and their sum using ‘for loop’.
    Sample output:
    The first 10 natural number are :
    1 2 3 4 5 6 7 8 9 10
The Sum is : 55
Source Code:
num = 10 if
num < 0:
  print("Enter a positive number") else:
  sum = 0
  # use while loop to iterate until zero
  while(num > 0):       sum += num       num -= 1
print("The sum of the natural numbers is", sum)
Output:
2. Write a program to print the multiplication table of the number entered by the user.
   The table should get displayed in the following form.
      29 x 1 = 29
      29 x 2 = 58
      …
      29 x 10 = 290
Source Code:
num = int(input("Enter the number: "))
print("Multiplication Table of", num)
for i in range(1, 11):
  print(num,"X",i,"=",num * i)
Output:
Laraib Shahzad                              Sir Syed University Of Engineering and Technology 6
2020F-BMCS-004
Software Development (MCS-203L)                                           SSUET/QR/114
3. Write a program that take vowels character in a variable named “vowels” then
   print each vowels characters in newline using ‘for loop’.
Source Code:
print("Enter the Character: ") c = input() if
c=='a' or c=='e' or c=='i' or c=='o' or c=='u':
  print("\nIt is a Vowel") elif c=='A' or c=='E' or
c=='I' or c=='O' or c=='U':
  print("\nIt is a Vowel") else:
  print("\nIt is a Consonant") Output:
Laraib Shahzad                            Sir Syed University Of Engineering and Technology 7
2020F-BMCS-004