Lab 09
Lab 09
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)
Example#08:
Output:
1
Programming Fundamentals (CT-153) Practical Workbook
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)
2
Programming Fundamentals (CT-153) Practical Workbook
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.
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.
4
Programming Fundamentals (CT-153) Practical Workbook
*****
****
***
**
*