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

ب ٩

This document covers flow control in Python, focusing on loops, including for and while loops, and their syntax. It also explains the use of break, continue, and pass statements within loops. Examples are provided to illustrate each concept effectively.

Uploaded by

rbd45585h5
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)
4 views

ب ٩

This document covers flow control in Python, focusing on loops, including for and while loops, and their syntax. It also explains the use of break, continue, and pass statements within loops. Examples are provided to illustrate each concept effectively.

Uploaded by

rbd45585h5
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/ 18

1312 / 121 CCS-3

Introduction to Programming

Lecture 9, PYTHON – FLOW CONTROL


(Loops)

1
PYTHON – FLOW CONTROL (Loops)
Lecture Goals
1. Looping
1.1 for loop
1.2 while loop
2. break statement
3. continue statement
4. pass statement
2
Dr. Quadri Noorulhasan Naveed 2
1. Looping
If we want to execute a group of statements in multiple times, then we
should go for looping kind of execution.
1.1 for loop
1.2 while loop
1.1 for loop
for is a keyword in python
Basically, for loop is used to get or iterate elements one by one from sequence like
string, list, tuple, etc…
While iterating elements from sequence we can perform operations on every element.
Syntax
for variable in
sequence:
statements
3
Dr. Quadri Noorulhasan Naveed 3
for…in loop

4
Dr. Quadri Noorulhasan Naveed 4
for loop example - 1

Output

1
2
3
4
5

5
Dr. Quadri Noorulhasan Naveed 5
for loop example - 2

6
Dr. Quadri Noorulhasan Naveed 6
1.2 while loop
while is a keyword in python
If we want to execute a group of statements repeatedly until the condition
reaches to False, then we should go for while loop.
Syntax
Initialization
while condition:
while block statements
increment/decrement
while loop contains an expression/condition.
As per the syntax colon (:) is mandatory otherwise it throws syntax error.
After while loop we need to follow indentation otherwise it throws
IndentationError.
Condition gives the result as bool type, means either True or False
7
Dr. Quadri Noorulhasan Naveed 7
while loop (cont.)
If the condition result is True, then while loop executes till the condition
reaches to False.
If the condition result is False, then while loop execution terminates.

Conclusion
Till condition is True the while loop statements will be executed.
If the condition reaches to False, then while loop terminate the
execution.

8
Dr. Quadri Noorulhasan Naveed 8
while loop example

9
Dr. Quadri Noorulhasan Naveed 9
2. break statement
break is a keyword in python
The break statement can be used inside the loops.
By using break we can break the execution based on some condition.
Generally, break statement is used to terminate for and while loops.
Example -1

10
Dr. Quadri Noorulhasan Naveed 10
break statement example - 2

11
Dr. Quadri Noorulhasan Naveed 11
break statement example - 3

12
Dr. Quadri Noorulhasan Naveed 12
3. continue statement
continue is a keyword in python
We can use continue statement to skip current iteration and continue
next iteration.

Example -1

13
Dr. Quadri Noorulhasan Naveed 13
continue statement example - 2

14
Dr. Quadri Noorulhasan Naveed 14
3. pass statement
pass is keyword in python.
The pass statement is used as a placeholder for future code.
It is useful as a placeholder when a statement is required syntactically,
but no code needs to be executed.
pass is a null operation, when it is executed, nothing happens.
We can define an empty function, class, method with pass statement.

15
Dr. Quadri Noorulhasan Naveed 15
pass statement example -1

16
Dr. Quadri Noorulhasan Naveed 16
pass statement example -2

17
Dr. Quadri Noorulhasan Naveed 17
1312 / 121 CCS-3
Introduction to Programming

Questions?
18

You might also like