Lecture 6
Lecture 6
CONTROL STRUCTURE
LECTURE-6
CONTENT
• Decision making is conditions occurring while execution of the program and specifying
actions taken according to the conditions.
• Decision structures evaluate multiple expressions which produce TRUE or FALSE as
outcome.
• we need to determine which action to take and which statements to execute if outcome is
TRUE or FALSE otherwise
FOLLOWING IS THE GENERAL FORM OF A
TYPICAL DECISION MAKING STRUCTURE
CONTROL FLOW IN PYTHON
• Computer programs are said to be shaped into the control structures or the control flow
that are nothing but a block of decisions that analyze the flow through instructions.
• All the decisions are made based on two categories, the data and instructions to
provide a particular response.
• When you deal with data, you try to understand the nature of it (data type), and
when you deal with instructions you need to understand on what basis are, they
are being made on.
THERE ARE THREE TYPES OF PYTHON CONTROL
STRUCTURES.
• Selection
• Sequential
• Repetition (iteration).
PYTHON SEQUENTIAL
• Flow of a program that executes in an order, without skipping, jumping or switching to
another block of code.
CONDITIONAL STATEMENTS
• if statement
if statement is the most simple decision-making statement.
if a certain condition is true then a block of statement is executed otherwise not.
• Syntax:
if condition:
# Statements to execute if
# condition is true
Flowchart of Python if statement
Example: Python if Statement # python program to illustrate If statement
i = 10
if (i > 15):
print("10 is less than 15")
print("I am Not in if")
Output:
I am Not in if
https://www.geeksforgeeks.org/python-if-else/
Control Flow in Python – GeeksforGeeks
Python Conditions and If statements
• Equals: a == b
• Not Equals: a != b
• Less than: a < b
• Less than or equal to: a <= b
• Greater than: a > b
• Greater than or equal to: a >= b
These conditions can be used in several ways, most commonly in "if
statements" and loops.
Try it Yourself »
if-else
The if statement alone tells us that if a condition is true it will execute a block of
statements and if the condition is false it won’t.
If we want to do something else if the condition is false.
We can use the else statement with if statement to execute a block of code when the
condition is false.
Syntax:
if (condition):
# Executes this block if
# condition is true
else:
# Executes this block if
# condition is false
FlowChart of Python if-else statement
Example 1: Python if-else statement
# python program to illustrate If else statement
i = 20
if (i < 15):
print("i is smaller than 15")
print("i'm in if Block")
else:
Output:
print("i is greater than 15")
print("i'm in else Block")
i is greater than 15
i'm in else Block print("i'm not in if and not in else
i'm not in if and not in else Block Block")
https://ide.geeksforgeeks.org/cd342068-bbfe-4561-8626-d2a7
275580c1
nested-if
A nested if is an if statement that is the target of another if statement.
Nested if statements mean an if statement inside another if statement.
Python allows us to nest if statements within if statements. i.e, we can place 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
Flowchart of Python
Nested if Statement
Example - python program to illustrate nested If statement
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")
Output:
i is smaller than 15
i is smaller than 12 too
https://ide.geeksforgeeks.org/070acf41-63ed-4161-b280-8ce98a96f0dd
if-elif-else Here, a user can decide among multiple options.
The if statements are executed from the top down.
If none of the conditions is true, then the final else
statement will be executed.
Syntax:
if (condition):
statement
elif (condition):
statement
.
.
else:
statement
FlowChart of Python
if else elif statements
# Python program to illustrate if-elif-else ladder
i = 20
if (i == 10):
print("i is 10")
elif (i == 15):
print("i is 15")
elif (i == 20):
print("i is 20")
else:
print("i is not present")
Output: i is 20
https://ide.geeksforgeeks.org/4f3f22eb-25d3-457b-ba34-277697694df0
The elif keyword is pythons way of saying "if the previous conditions were not true, then try
this condition".
Example
a = 33
b = 33
if b > a:
print("b is greater than a")
elif a == b:
print("a and b are equal")
Try it Yourself »
Python While Loops
Python Loops
Python has two primitive loop commands:
• while loops
• for loops
Python While Loop is used to execute a block of statements repeatedly until a given
condition is satisfied.
And when the condition becomes false, the line immediately after the loop in the program
is executed.
Syntax:
while expression:
statement(s)
Flowchart of While Loop :
The while Loop
With the while loop we can execute a set of statements as long as a condition is
true.
Example
Print i as long as i is less than 6:
i = 1
while i < 6:
print(i)
i += 1
Output
1
2
Try it Yourself » 3
4
5
Python break statement
for i in li:
if(i =='a'):
pass
else:
print(i) Output:
b
c
d
https://ide.geeksforgeeks.org/53770f01-8b56-46df-8c9f-32f4e819fd81
Python For Loops
Python For loop is used for sequential traversal i.e. it is used for
iterating over an iterable like String, Tuple, List, Set or Dictionary.
https://ide.geeksforgeeks.org/eb8cc6fc-31b2-4b84-a4b3-bbe52231325c
THANK YOU