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

Lab 09

Uploaded by

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

Lab 09

Uploaded by

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

Programming Fundamentals (CT-153) Practical Workbook

LAB # 09
OBJECTIVE
Implement while loop and implement break and continues statement by using loop.

THEORY
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.
While Loop:
The for loop takes a collection of items and executes a block of code once for each
item in the collection. In contrast, 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.


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.

Example#08:

Output:

1
Programming Fundamentals (CT-153) Practical Workbook

How does this program work?

‘a’ now equals 0


As long as ‘a’ is less than 10, do the following:
Make ‘a’ one larger than what it already is.
Print on-screen what ‘a’ is now worth.

What does this do? Let’s go through what the computer would be thinking when it is
in the’while loop
Is ‘a’ less than 10? YES (its 0)
Make ‘a’ one larger (now 1)
Print on-screen what ‘a’ is (1)

Is ‘a’ less than 10? YES (its 1)


Make ‘a’ one larger (now 2)
Print on-screen what ‘a’ is (2)

Is ‘a’ less than 10? YES (its 2)


Make ‘a’ one larger (now 3)
Print on-screen what ‘a’ is (3)

Is ‘a’ less than 10? YES (its 3)


Make ‘a’ one larger (now 4)
Print on-screen what ‘a’ is (4)

Is ‘a’ less than 10? YES (its 4)


Make ‘a’ one larger (now 5)
Print on-screen what ‘a’ is (5)

Is ‘a’ less than 10? YES (its 5)


Make ‘a’ one larger (now 6)
Print on-screen what ‘a’ is (6)

Is ‘a’ less than 10? YES (its 6)


Make ‘a’ one larger (now 7)
Print on-screen what ‘a’ is (7)

Is ‘a’ less than 10? YES (its 7)


Make ‘a’ one larger (now 8)
Print on-screen what ‘a’ is (8)

Is ‘a’ less than 10? YES (its 8)


Make ‘a’ one larger (now 9)
Print on-screen what ‘a’ is (9)

2
Programming Fundamentals (CT-153) Practical Workbook

Is ‘a’ less than 10? YES (its 9)


Make ‘a’ one larger (now 10)
Print on-screen what ‘a’ is (10)

Is ‘a’ less than 10? NO (its 10, therefore isn’t less than 10 )
Don’t do the loop , there’s no code left to do , so the program ends.

Infinite Loop and Using break to Exit a Loop


The most common use for break is when some external condition is triggered
requiring a hasty exit from a loop. The break statement can be used in
both while and for loops.

Example#08:

Output:

Continue Statement:
It returns the control to the beginning of the while loop.. The continue statement
rejects all the remaining statements in the current iteration of the loop and moves
the control back to the top of the loop. The continue statement can be used in
both while and for loops.

Example#09:

Output:

3
Programming Fundamentals (CT-153) Practical Workbook

Lab Exercise:
1. Write a program that take a user input and to create the multiplication table
(from 1 to 10) of that number.

2. Write a program that generates fibonicca series i.e. 1, 2, 3, 5, 8, 13, 21….

4
Programming Fundamentals (CT-153) Practical Workbook

3. Write a program that generates the following pattern (using loop):


1
12
123
1234
12345

*****
****
***
**
*

You might also like