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

Lecture 6

The document discusses various Python control structures including conditional statements and iterative statements. It covers if, if-else, elif, while, for, break, continue and pass statements. The if statement executes a block of code if a condition is true, if-else adds else block for false conditions. elif allows checking multiple expressions. While loops repeat until a condition is false and for loops iterate over an iterable. break exits the current loop, continue skips to next iteration, and pass is a null operation.

Uploaded by

Lakshay Goel
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views

Lecture 6

The document discusses various Python control structures including conditional statements and iterative statements. It covers if, if-else, elif, while, for, break, continue and pass statements. The if statement executes a block of code if a condition is true, if-else adds else block for false conditions. elif allows checking multiple expressions. While loops repeat until a condition is false and for loops iterate over an iterable. break exits the current loop, continue skips to next iteration, and pass is a null operation.

Uploaded by

Lakshay Goel
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 44

PYTHON -

CONTROL STRUCTURE
LECTURE-6
CONTENT

• Python - Decision Making


• Conditional statements
If
If-else
elif
• Iterative Statement
While
for
PYTHON - DECISION MAKING

• 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

• Decision-making statements in Python programming languages decide the


direction of the flow of program execution.
• There comes situations in real life when we need to make some decisions
and based on these decisions, we decide what should we do next.
• Flow control describes the order in which statements will be executed at
runtime
CONDITIONAL STATEMENTS

• 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

Python supports the usual logical conditions from mathematics:

• 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.

An "if statement" is written by using the if keyword.


Example
If statement:
a = 33
b = 200
if b > a:
  print("b is greater than a")

Output: b is greater than a

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")

Output: 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

 break statement in Python is used to bring the control out of the


loop when some external condition is triggered.
 It terminates the current loop
Python break is used to terminate the execution of the
loop.

Python break statement Syntax:


Loop{
Condition:
break
}
Example
Exit the loop when i is 3:
i = 1
while i < 6:
  print(i)
  if i == 3:
    break
  i += 1
Output:
Try it Yourself » 1
2
3
The continue Statement
 With the continue statement we can stop the current iteration, and continue with the
next:
Example- Continue to the next iteration if i is 3:
i = 0
while i < 6:
  i += 1
  if i == 3:
    continue
  print(i)
Try it Yourself »
Output:
1
2
4
5
6
Python pass pass is used when the user doesn’t want any code to execute.
Statement
li =['a', 'b', 'c', 'd']

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.

 The for keyword in other programming languages, and


works more like an iterator method as found in other
object-orientated programming languages.

for var in iterable:


# statements
# Python Program to
# show range() basics
Output:

# printing a number 0123456789


for i in range(10):
print(i, end=" ") Sum of first 10 numbers : 45

# performing sum of first 10 numbers


sum = 0
for i in range(1, 10):
sum = sum + i
print("\nSum of first 10 numbers :", sum)

https://ide.geeksforgeeks.org/eb8cc6fc-31b2-4b84-a4b3-bbe52231325c
THANK YOU

You might also like