ب ٨
ب ٨
Introduction to Programming
1
PYTHON – FLOW CONTROL STATEMENTS - if
Lecture Goals
1. Flow control 4. Sequential statements
2. Types of the execution 5. Conditional or Decision-making
2.1 Sequential statements
2.2 Conditional 5.1 if statement
2.3 Looping
5.2 if else statement
3. Indentation
5.3 if elif else statement
1. IndentationError
2
Dr. Quadri Noorulhasan Naveed 2
1. Flow control
The order of statements execution is called as flow of control.
4
Dr. Quadri Noorulhasan Naveed 4
Types of the execution
2.1 Sequential
Statements execute from top to bottom, means one by one sequentially.
By using sequential statement, we can develop only simple programs.
2.2 Conditional
Based on conditions, statements used to execute.
Conditional statements are useful to develop better and complex programs.
2.3 Looping
Based on conditions, statements used to execute randomly and repeatedly.
Looping execution is useful to develop better and complex programs.
5
Dr. Quadri Noorulhasan Naveed 5
3. Indentation
Python uses indentation to indicate a block of code.
Indentation refers to adding white space before a statement to a
particular block of code.
Python uses 4 spaces as indentation by default.
oHowever, the number of spaces is up to you, but a minimum of 1
space has to be used.
1. IndentationError
If we didn’t follow the proper indentation then we will get error as
7
Dr. Quadri Noorulhasan Naveed 7
Sequential statements Control Flow
8
Dr. Quadri Noorulhasan Naveed 8
5. Conditional or Decision-making statements
5.1 if statement
syntax
if condition (or boolean expression):
if block statements
9
Dr. Quadri Noorulhasan Naveed 9
if statement
10
Dr. Quadri Noorulhasan Naveed 10
if statement
11
Dr. Quadri Noorulhasan Naveed 11
if statement
12
Dr. Quadri Noorulhasan Naveed 12
if statement
13
Dr. Quadri Noorulhasan Naveed 13
if statement
if is a keyword in python
if statement contains an guard expression/condition/value.
As per the syntax colon (:) is mandatory otherwise it throws syntax error.
After if statement we need to follow indentation otherwise it throws
IndentationError.
Condition gives the result as a bool type, means either True or False.
If the condition result is True, then if block statements will be executed
If the condition result is False, then if block statements won’t execute.
14
Dr. Quadri Noorulhasan Naveed 14
if statement execution path
15
Dr. Quadri Noorulhasan Naveed 15
if statement example - 1
16
Dr. Quadri Noorulhasan Naveed 16
if statement example - 2
17
Dr. Quadri Noorulhasan Naveed 17
5.2. if else statement
syntax
if condition:
if block statements1
else:
else block statements2
if and else are keywords in python
if statement contains an expression/condition.
As per the syntax colon (:) is mandatory otherwise it throws syntax error.
After if and else statements we need to follow indentation otherwise it throws
IndentationError.
Condition gives the result as bool type, means either True or False
If the condition result is True, then if block statements will be executed
If the condition result is False, then else block statements will be executed.
18
Dr. Quadri Noorulhasan Naveed 18
if else statement
19
Dr. Quadri Noorulhasan Naveed 19
if else statement
20
Dr. Quadri Noorulhasan Naveed 20
if else statement
21
Dr. Quadri Noorulhasan Naveed 21
if else statement execution path
22
Dr. Quadri Noorulhasan Naveed 22
if else statement execution path
23
Dr. Quadri Noorulhasan Naveed 23
if else example - 1
24
Dr. Quadri Noorulhasan Naveed 24
if else example - 2
25
Dr. Quadri Noorulhasan Naveed 25
5.3 if elif else statement
Syntax
if condition1:
if block statements
elif condition2:
elif block1statements
elif condition3:
elif block2statements
else:
else block statements
26
Dr. Quadri Noorulhasan Naveed 26
if elif else statement
27
Dr. Quadri Noorulhasan Naveed 27
1312 / 121 CCS-3
Introduction to Programming
Questions?
28