0% found this document useful (0 votes)
17 views9 pages

Shariq Lab 6

1) The document discusses nested statements, break and continue statements in Python. It provides examples of nested if statements and nested for loops. 2) It explains that break terminates the current loop immediately, while continue skips the current iteration and continues with the next. Examples are given showing the output of programs using break and continue. 3) The exercises section provides code snippets with errors to correct, programs to write the output for, and programming problems to solve around nested loops, prime numbers, and multiplication tables.

Uploaded by

SHARIQ SHAKEEL
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views9 pages

Shariq Lab 6

1) The document discusses nested statements, break and continue statements in Python. It provides examples of nested if statements and nested for loops. 2) It explains that break terminates the current loop immediately, while continue skips the current iteration and continues with the next. Examples are given showing the output of programs using break and continue. 3) The exercises section provides code snippets with errors to correct, programs to write the output for, and programming problems to solve around nested loops, prime numbers, and multiplication tables.

Uploaded by

SHARIQ SHAKEEL
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

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

LAB # 06
NESTED STATEMENTS,
BREAK AND CONTINUE STATEMENTS

OBJECTIVE
Working on nested statements and control loop iteration using break and continue.

THEORY

Nested Statements:
A Nested statement is a statement that is the target of another statement. Nested if:
A Nested if is an if statement that is the target of another if statement. Nested if
statements means an if statement inside another if statement.
Syntax:
if (condition1):
# Executes when condition1 is true if (condition2):
# Executes when condition2 is true
# if Block is end here
# if Block is end here

Example:
#using nested if
x=int(input("enter number="))
y=int(input("enter 2nd number="))
if x > 2: if y > 2:
z = x + y
print("z is", z) else:
print("x is", x)

Output:
>>> %Run task1.py enter number=3
enter 2nd number=8
z is 11
>>>
Programming Fundamentals (CS-116L) SSUET/QR/114

Nested loops:
Nested loops consist of an outer loop and one or more inner loops. Each time the outer
loop repeats, the inner loops are reinitialized and start again.

Example:
height=int(input("Enter height: ")) for
row in range(1, height): for
column in range(1,height):
print(row, end=" ")
print()

Output:
>>> %Run task2.py
Enter height: 7
111111
222222
333333
444444
555555 666666

Keywords break and continue:


The break and continue keywords provide additional controls to a loop.

The Break Statement:


The keyword break in a loop to immediately terminate a loop. Listing example presents a
program to demonstrate the effect of using break in a loop.

Syntax: break

Example:
# Use of break statement inside loop
for word in "string": if word ==
"i":
break
print(word)
print("The end")
Output:
>>> %Run 'lab1-task1!.py' s t r
The end
>>>
Programming Fundamentals (CS-116L) SSUET/QR/114

The continue Statement:


The continue statement breaks out of the current iteration in the loop.

Syntax: continue

Example:
# Program to show the use of continue statement inside
Loops for val in "string": if val == "i": continue
print(val)
print("The end")

Output:
>>> %Run 'lab1-task1!.py' s t
r n g The end
>>>
Programming Fundamentals (CS-116L) SSUET/QR/114

PROGRAMMING FUNDAMENTALS
LAB 6

EXERCISE

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

1. Code
prompt = "\nPlease enter the name of a city you have
visited:" prompt+="\n(Enter 'quit' when you are
finished.)" while True:
city = str(input(prompt)) if city ==
quit: break; else:
print("I'd love to go to " , city.title() ,
"!")

CORRECT CODE:
prompt = "\nPlease enter the name of a city you have visited:"
prompt += "\n(Enter 'quit' when you are finished.)" while
True: city = input(prompt) if city == 'quit':
break
else:
print("I'd love to go to", city.title() + "!")

OUTPUT:

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

if x>2: if y>2:
z=x+y print(“z is”, y)
else:
print (“x is”, x)

CORRECT CODE:
x=4 y=3 if x > 2:
if y > 2: z=x+
y print("z is",
z) else:
print("x is", x)

OUTPUT:

3. Code
balance = int(input("enter yourbalance1:"))
while true: if balance <=9000:
continue; balance = balance+999.99
print("Balance is", balance)

CORRECT CODE:
balance = int(input("Enter Your Balance : "))
while True: if balance <= 9000:
continue; balance =
balance + 999.99
print("Balance is", balance)

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

B. What will be the output of the following programs:

1. Code

i = 10 if (i==10):
#First if statement
if (i < 15): print ("i is smaller
than 15")
# Nested - if statement
# Will only be executed if statement above
# it is true if (i < 12):
print ("i is smaller than 12 too") else:
print ("i is greater than 15")

CORRECT CODE:
i = 10 if (i==10):
if (i < 15):
print ("i is smaller than 15") if
(i < 12):
print ("i is smaller than 12 too")
else: print ("i is greater than 15")

OUTPUT:

2. Code
i = 1 j = 2 k = i = 1 j = 2 k = 3
3 if i > j: if i > j:
if i > k: if i> k:
print('A') print('A') else:
else: print('B')
print('B')
Programming Fundamentals (CS-116L) SSUET/QR/114

CORRECT CODE:
i=1j=2k=
3
if i > j: if i >
k:
print('A')
else:
print('B')

OUTPUT:

3. Code
# nested for loops for i in
range(0, 5): for
j in range(i):
print(i, end=' ')
print()

CORRECT CODE:
for i in range(0, 5):
for j in range(i):
print(i, end=' ')
print()

OUTPUT:

C. Write Python programs for the following:

1. Write a program to add first seven terms twice of the following series:
Programming Fundamentals (CS-116L) SSUET/QR/114

CODE:
sum = 0 fact = 1
for i in range
(1,8):
fact=fact*i
sum=sum+i/fact
print("Sum of series is", sum)

OUTPUT:

2. Write a program to print all prime numbers from 900 to 1000. [Hint: Use
nested loops, break and continue]

CODE:
for num in range(900, 1001):
if num > 1:
for i in range(2, num):
if (num % i) == 0:
break
else:
print(num)

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

3. Write a program to display multiplication table (1-5) using nested looping


Sampled output: [hint: '{ } ' .format(value)]
02 X 01 = 02

CODE:
i in range(1, 6):
print(f"Multiplication table of {i}:")
for j in range(1, 11):
print(f"{i} x {j} = {i*j}")
print()

OUTPUT:

You might also like