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

PF LAB 05 (Control Statements)

Programming Fundamental in python

Uploaded by

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

PF LAB 05 (Control Statements)

Programming Fundamental in python

Uploaded by

Syed Farrukh
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Programming Fundamentals (CS-116L) SSUET/QR/114

LAB # 05

Control Statements
OBJECTIVE:
To get familiar with the concept of control statement for simple controlling and repetition of program
statements.

Theory:
Control Statements (Loops)

In general, statements are executed sequentially: The first statement in a function is


executed first, followed by the second, and so on. There may be a situation when you need
to execute a block of code several number of times.

Programming languages provide various control structures that allow for more
complicated execution paths.

A loop statement allows us to execute a statement or group of statements multiple times.


The following diagram illustrates a loop statement:

Python programming language provides following types of loops to handle looping


requirements.

Loop Type Description


Repeats a statement or group of statements while a given condition is
while loop
TRUE. It tests the condition before executing the loop body.
Executes a sequence of statements multiple times and abbreviates the
for loop
code that manages the loop variable.
nested loops You can use one or more loop inside any another while, for loop.

The while Loop


A while loop statement in Python programming language repeatedly executes a target
statement as long as a given condition is True.

Syntax

The syntax of a while loop in Python programming language is: while expression:

statement(s)

Here, statement(s) may be a single statement or a block of statements.

Electronic Engineering Department 1


Programming Fundamentals (CS-116L) SSUET/QR/114

The condition may be any expression, and true is any non-zero value. The loop iterates
while the condition is true. When the condition becomes false, program control passes to
the line immediately following the loop.

In Python, all the statements indented by the same number of character spaces after a
programming construct are considered to be part of a single block of code. Python uses
indentation as its method of grouping statements.

Example-1: Example-2:
i=1 i=1
while i < 4: while i < 4:
print i print i
i+=1 i+=1
print “END” print “END”

Output-1: Output-2:

1 END 1
2 END 2
3 END 3
END

Example: Write a program to display factorial of a given number. Program:


n=input("Enter the number: ")
f=1
while n>0:
f=f*n
n=n-1
print "Factorial is", f

Output:
Enter the number: 5 Factorial is 120

The For loop:


The for loop is useful to iterate over the elements of a sequence. It means, the for loop
can be used to execute a group of statements repeatedly depending upon the number of
Electronic Engineering Department 2
Programming Fundamentals (CS-116L) SSUET/QR/114

elements in the sequence. The for loop can work with sequence like string, list, tuple, range
etc.
The syntax of the for loop is given below:
for var in sequence:
statement (s)

The first element of the sequence is assigned to the variable written after „for‟ and then the
statements are executed. Next, the second element of the sequence is assigned to the variable
and then the statements are executed second time. In this way, for each element of the sequence,
the statements are executed once. So, the for loop is executed as many times as there are
number of elements in the sequence.

for i range(1,5): for i range(1,5):


print i print i
print “END” print “END”

1 END 1
2 END 2
3 END 3
END

Output-1: Output-2:

Example-3: Example-4:
Name= "python"
for x in range(10,0,-1):
10 9 8 7 6 5 4 3 2 1
for letter in Name:
print x,
print letter

Output-3: Output-4:
py t h o n

Exampe: Write a program to display the factorial of given number.


n=input("Enter the number: ")
f=1
for i in range(1,n+1): f=f*i
print "Factorial is",f

Program:

Output:

Enter the number: 5


Electronic Engineering Department 3
Programming Fundamentals (CS-116L) SSUET/QR/114

Factorial is 12

TASK:
Perform the following tasks using Python source code using loops
1. To generate a random number from 3 to 13.
CODE:
import random
random_number = random.randint(3, 13)
print(random_number)
OUTPUT:

2. To print the odd number from 10 to 100 and even numbers from 200 to 96.
CODE:
# Print odd numbers from 10 to 100
print("Odd numbers from 10 to 100:")
for number in range(10,101):
if number % 2 != 0:
print(number)
OUTPUT:

Electronic Engineering Department 4


Programming Fundamentals (CS-116L) SSUET/QR/114

CODE:
# Print even numbers from 96 to 200
print("even numbers from 96 to 200:")
for number in range(96,201):
if number % 2 == 0:
print(number)
OUTPUT:

Electronic Engineering Department 5


Programming Fundamentals (CS-116L) SSUET/QR/114

3. To take a integer value from a user and print the factorial of that number.
CODE:
n =int(input("Enter a Number:"))
factorial = 1
if n < 0:
print("Factorial does not exist")
elif n==0:
print("The factorial is 1")
else:
for i in range(1, n+1):
factorial = factorial*i
print("The factorial of",n,"is",factorial)

OUTPUT:

Electronic Engineering Department 6


Programming Fundamentals (CS-116L) SSUET/QR/114

4. To take two integer values from the user and print the table. The first indicates the table
number and the second value represents the limit of the table.
CODE:
table_number = int(input("Enter the table number: "))
limit = int(input("Enter the limit of the table: "))

if limit < 1:
print("Limit should be a positive integer.")
else:
print("Table of",table_number,"up to",limit)

for i in range(1, limit + 1):


result = table_number * i
print(table_number, 'x', i, '=', result)
OUTPUT:

5. To take two integer values from the user and print out the highest and lowest value.
CODE:
n_1 = int(input("Enter 1st Integer:"))
n_2 = int(input("Enter 2nd Integer:"))

Electronic Engineering Department 7


Programming Fundamentals (CS-116L) SSUET/QR/114

if n_1 > n_2:


highest = n_1
lowest = n_2
else:
highest = n_2
lowest = n_1

print("Highest Value is:",highest)


print("Lowest Value is:",lowest)
OUTPUT:

6. To calculate the factorial of 3 numbers which will be provided by the user.


CODE:
N =int(input("Enter 1st number:"))
factorial = 1
if N<0:
print("Factorial does not exist")
elif N==0:
print("The factorial is 1")
else:
for i in range(1, N+1):
factorial = factorial*i
print("The factorial of",N,"is",factorial)

O =int(input("Enter 2nd number:"))


factorial = 1
if O<0:
print("Factorial does not exist")
elif O==0:
print("The factorial is 1")
else:
for n in range(1, O+1):
factorial = factorial*n
print("The factorial of",O,"is",factorial)

P =int(input("Enter 3rd number:"))


factorial = 1
if P<0:
Electronic Engineering Department 8
Programming Fundamentals (CS-116L) SSUET/QR/114

print("Factorial does not exist")


elif P==0:
print("The factorial is 1")
else:
for m in range(1, P+1):
factorial = factorial*m
print("The factorial of",P,"is",factorial)
OUTPUT:

Electronic Engineering Department 9

You might also like