0% found this document useful (0 votes)
20 views

Lab 11

cf

Uploaded by

aliza
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

Lab 11

cf

Uploaded by

aliza
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Lab # 12: WHILE Loop SSUET/QR/114

LAB # 11
WHILE LOOP

OBJECTIVE

To get familiar with the while loop.

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.

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:

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.

CE-119 : Computing Fundamentals 89


Lab # 12: WHILE Loop SSUET/QR/114

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!

The while loop in Python is used to iterate over a block of code as long as the test
expression (condition) is true.

We generally use this loop when we don't know the number of times to iterate
beforehand.

Syntax of while Loop in Python

while test_expression:
Body of while

In the while loop, test expression is checked first. The body of the loop is entered only if
the test_expression evaluates to True. After one iteration, the test expression is checked
again. This process continues until the test_expression evaluates to False.

In Python, the body of the while loop is determined through indentation.


The body starts with indentation and the first unindented line marks the end.
Python interprets any non-zero value as True. None and 0 are interpreted as False

CE-119 : Computing Fundamentals 90


Lab # 12: WHILE Loop SSUET/QR/114

Flowchart of while Loop

Sample Program - 1
# Program to add natural
# numbers up to
# sum = 1+2+3+...+n
# To take input from the user,
# n = int(input("Enter n: "))
n = 10
# initialize sum and counter
sum = 0
i=1
while i <= n:
sum = sum + i
i = i+1 # update counter

# print the sum


print("The sum is", sum)

CE-119 : Computing Fundamentals 91


Lab # 12: WHILE Loop SSUET/QR/114

Output:
When you run the program, the output will be:
Enter n: 10
The sum is 55

In the above program, the test expression will be True as long as our counter variable i is
less than or equal to n (10 in our program). We need to increase the value of the counter
variable in the body of the loop. This is very important (and mostly forgotten). Failing to
do so will result in an infinite loop (never-ending loop).

While loop with else


Same as with for loops, while loops can also have an optional else block.
The else part is executed if the condition in the while loop evaluates to False.
The while loop can be terminated with a break statement. In such cases, the else part is
ignored. Hence, a while loop's else part runs if no break occurs and the condition is false.

Here is an example to illustrate this.


'''Example to illustrate the use of else statement with the while loop'''
counter = 0
while counter < 3:
print("Inside loop")
counter = counter + 1
else:
print("Inside else")

Output:
Inside loop
Inside loop
Inside loop
Inside else
Here, we use a counter variable to print the string Inside loop three times.

On the fourth iteration, the condition in while becomes False. Hence, the else part is
executed.
CE-119 : Computing Fundamentals 92
Lab # 12: WHILE Loop SSUET/QR/114

EXERCISE

A. Point out the errors, if any, in the following Python programs.

1. Code
count = 4
while n < 8
count = count + 3:
print(count)

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:

2. Code

i = 1
while i>0:
print(i)
i = i + 1

Output:

CE-119 : Computing Fundamentals 93


Lab # 12: WHILE Loop SSUET/QR/114

Lab Task:

1. Write a program that prints the first 10 natural numbers and their sum using
‘while loop’.

Sample output:
The first 10 natural number are:
1 2 3 4 5 6 7 8 9 10
The Sum is : 55

2. Write a program to print the multiplication table of the number entered by the user
using while loop.The table should get displayed in the following form.

29 x 1 = 29
29 x 2 = 58

29 x 10 = 290

3. Write a program which takes 10 integers from keyboard using while loop and print
their average value on the screen.

4. Write a program which finds the average of 10 numbers using while loop.

CE-119 : Computing Fundamentals 94

You might also like