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

Unit-2 Control Flow System

Uploaded by

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

Unit-2 Control Flow System

Uploaded by

sharmaankit30123
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 89

Quality Content for Outcome based Learning

Control Flow System


Day 1

Ver. No.: 1.1 Copyright © 2021, ABES Engineering College


General Guideline
© (2021) ABES Engineering College.

This document contains valuable confidential and proprietary information of ABESEC. Such confidential and
proprietary information includes, amongst others, proprietary intellectual property which can be legally protected and
commercialized. Such information is furnished herein for training purposes only. Except with the express prior
written permission of ABESEC, this document and the information contained herein may not be published,
disclosed, or used for any other purpose.

2
Copyright © 2021, ABES Engineering College
Objective of Control Flow System

To understand the purpose and syntax of decision control statements.

To understand the purpose and syntax of Iterative/looping statements and jump statements.

To develop python programs with conditions and loops.

To apply the knowledge of conditions, loops and jump in solving real time problems.

To create python programs with decision ,looping and jump statements.

3
Copyright © 2021, ABES Engineering College
Topics Covered

Day 1 Day 2 Day 3 Day 4

2.1 Introduction 2.3 Iterative/ Looping 2.3 Iterative/ Looping 2.4 Jump Statements
2.2 Selection / Statements Statements • Break
Decision control • While loop • Range() • Continue
• Nested While Function
statement
•Simple If statement
loop • For loop 2.5 Else with Loop
• Nested for • While with else
•If Else statement
loop • For with else
•If-Elif statement
•Nested If Else
statement

4
Copyright © 2021, ABES Engineering College
Session Plan - Day 1

2.1. Introduction to Decision Control Statements


2.2. Selection / Decision control statement
• Simple If Statements
• If Else Statements
• If ..elif..else statements
• Nested If Else Statements
• Examples
• Review questions
• Summary

5
Copyright © 2021, ABES Engineering College
Introduction

Control flow of the program is:

The order in which the code of the program executes.


Regulated by:

Conditional statements.
Iterative/looping statements.

6
Copyright © 2021, ABES Engineering College
Contd..
 Conditional statement examples: Based on certain conditions, we take decisions
in our daily life.
• Darkness in the room: Turn on lights. • Weather is Cold: Drink Coffee
• No Darkness: Do Nothing • Weather is not Cold: Softdrink

7
Copyright © 2021, ABES Engineering College
Contd..
 Iterative Looping statements examples: For taking decisions we perform various
actions and repeat the same action many times.

Real Life Scenario:

 Suppose you want to buy a new t-shirt, visit


the shop.
 If you do not found a shirt of your choice, visit
to the new shop.
 Until your desired shirt is found, action is
performed again and again.
 This is called looping.

8
Copyright © 2021, ABES Engineering College
Selection/decision control statements
 It is used to control the flow of execution of program depending upon
condition:

 if the condition is true then the code block will execute


 if the condition is false then code block will not execute.

Real Life Scenario:


Consider the grading system for students:
 Excellent : Above 95%.
 Good: Greater than 80% & less than
equal to 95%.
 Average: Greater than 60% & less than
equal to 80%.
 Below average: Less than 60%

9
Copyright © 2021, ABES Engineering College
Revision of Flowchart

Used for start and stop.


Used for decision making.

Used for input/output.

Used for processing statement.

Connector / flow

10
Copyright © 2021, ABES Engineering College
Flowchart of the grading system

On the basis of the conditions, corresponding block of the


code will be executed or not.

Consider the grading system for students:

 Excellent :Above 95%.


 Good :Greater than 80% &less than equal t 95%.
 Average : Greater than 60% & less than equal to 80%.
 Below average: Less than 60%

11
Copyright © 2021, ABES Engineering College
Types of decision/selection control statements:

 Simple if

 if-else

 if..elif..else

 Nested...if…else

12
Copyright © 2021, ABES Engineering College
Simple if statement
 if evaluates whether a condition is:
True
False.

 if block is executed:
if and only if the value of its condition expression is
true.

Note: As we use curly braces in ‘C’ Language to define the scope of conditions and loops; in
the same manner we use colon (:) and indentation in Python.

13
Copyright © 2021, ABES Engineering College
Syntax of Simple if

14
Copyright © 2021, ABES Engineering College
Examples
 Print whether a number is even or not.
Case-1 : When n is even. Case-2 : When n is not even

15
Copyright © 2021, ABES Engineering College
Examples
 A program to increment a number if it is positive.

Explanation:
 The condition x>0 is True for value of x = 10 so the statements in the block of code will be
executed.
 The statement x = x+1 will increment the value of x by 1 i.e. x = 10 + 1; x = 11 and statement
print (x) will print x value as 11.
16
Copyright © 2021, ABES Engineering College
if else Statement
 It checks whether a condition is true or false.

 If a condition is true,

 The if statement executes


 Otherwise, the else statement executes.

17
Copyright © 2021, ABES Engineering College
Syntax of if else Statement

18
Copyright © 2021, ABES Engineering College
Example
 A program to print “Excellent” if marks is greater than 95 otherwise print “Not
Excellent”

Explanation:
 For input 68, condition marks > 95 is False. Therefore, else statement print (“Not Excellent”) is
executed and output will be “Not Excellent”.

19
Copyright © 2021, ABES Engineering College
if..elif..else statement
Extension of the if-else statement

For a multiple conditional statements


 We use if-elif statement.

Note: The conditional expression of if-elif-else are executed from top to bottom in a sequential manner. An elif
statements are known as elif Ladder.

20
Copyright © 2021, ABES Engineering College
Syntax of if..elif..else statement

21
Copyright © 2021, ABES Engineering College
Example

Explanation:
 For input 88, condition marks > 95 is False & control goes to next elif statement (marks>80 and
marks<=95), which is True and statement print (“Good”) will be executed.
 The output will be “Good”.

22
Copyright © 2021, ABES Engineering College
Nested if-else statement

 When an if-else statement is present inside the body of another “if” or “else” then
this is called nested if else.

23
Copyright © 2021, ABES Engineering College
Syntax of Nested if..else statement

24
Copyright © 2021, ABES Engineering College
Example

 A program to take age as input from user and print “You are less than 18” if age is less
than 18, otherwise print “You are above 18”.Also check that age could not be less than 0.
If so then print “Age could not be less than 0”.

25
Copyright © 2021, ABES Engineering College
Example

 A program to take age as input from user and print

 “You are less than 18” if age is less than 18

 otherwise print “You are above 18”.

 Also check that age could not be less than 0.

 If so then print “Age could not be less than 0”.

26
Copyright © 2021, ABES Engineering College
Can you answer these questions?

1. Which one of the following is a valid Python if statement :

A) if a>=2 :

B) if (a >= 2)

C) if (a => 22)

D) if a >= 22

27
Copyright © 2021, ABES Engineering College
3.Which of following is not a decision-making statement?

A) if-elif statement

B) for statement

C) if -else statement

D) if statement

28
Copyright © 2021, ABES Engineering College
4.Predict the output of the following code:

A) No output

B) okok

C) ok

D) None of above

29
Copyright © 2021, ABES Engineering College
5. What is the output of the following code snippet?

A) Launch a Missile

B) Let's have peace

C) 0.3

D)None

30
Copyright © 2021, ABES Engineering College
6. Which of the following is true about the code below?

A) x will always equal 0 after this code executes for any value of x

B) if x is greater than 2, the value in x will be doubled after this code executes

C) if x is greater than 2, x will equal 0 after this code executes

31
Copyright © 2021, ABES Engineering College
Summary

 Control statement are statements that control the flow of execution of statements so that
they can be executed repeatedly and randomly.

 The if statement executes a group of statements depending upon whether a condition is


true or false.

 The if..else statement executes a group of statements when a condition is true;


Otherwise, it will execute another group of statements.

 The if..elif statement is an extension of the if-else statement. When we have multiple
conditional statements, then we use if-elif statement.

 When an if..else statement is present inside the body of another “if” or “else” then this is
called nested if else.

32
Copyright © 2021, ABES Engineering College
Session Plan - Day 2
2.3 Iterative looping Statements
• While loop
• Nested While loop
• Examples
• Review Questions
• Summary

33
Copyright © 2021, ABES Engineering College
Iterative/Looping Statements
Sometimes we need to perform certain operations
again and again.

Real Life Scenario:

 A teacher decide to grade 75 students on the basis of


marks.
 He/she wants to do this for whole class.
 Teacher would repeat grading procedure for each student
in the class.
 This is called iterative/ looping.

34
Copyright © 2021, ABES Engineering College
Types of loops in Python

 While loop

 Nested While loop

 For loop

 Nested for loop

35
Copyright © 2021, ABES Engineering College
While loop

 It is used to repeat a block of code as long as the given condition is true.

36
Copyright © 2021, ABES Engineering College
Syntax of While loop

 Boolean expression is checked first.

 The body of the loop is entered only if the


Boolean expression evaluates to True.

 After one iteration, the Boolean expression


is checked again.

 This process continues until the Boolean


expression evaluates to False.

37
Copyright © 2021, ABES Engineering College
Example

Write a Python Program to print 4 Natural Numbers using i.e. 1,2,3,4 using While
loop.

38
Copyright © 2021, ABES Engineering College
Nested While loop

 Nested while loop is called when we use


while loop inside a while loop.

 We can use any number of while loop inside


a while loop.

 Main while loop as outer while loop and


nested while loop as inner while loop.

39
Copyright © 2021, ABES Engineering College
Syntax of Nested While loop:

 Outer while loop runs m number of times.

 Inner while loop runs n number of times.

 The total no. of iterations would be m*n.

40
Copyright © 2021, ABES Engineering College
Example of Nested While loop:

 Write a program to demonstrate While loop.

41
Copyright © 2021, ABES Engineering College
1. What will be the output of the following code snippet?

Options

A. None

B. Python

C. Ppppp

D. PPPPP

42
Copyright © 2021, ABES Engineering College
2. What will be the output of the following code snippet?

A. 0 1 2

B. 2 3

C. 0 1 2 0

D. None of the above

43
Copyright © 2021, ABES Engineering College
3. What should be the value of the variables num1 and num2 in the code snippet
below if the output expected is 4?

A. 16,6

B. 12,5

C. 8,2

D. 16,2

44
Copyright © 2021, ABES Engineering College
Summary:

 While loop is used to iterate block of codes repeatedly until given condition is
True
 While loop present inside another while loop it is called as nested while loop.
 The nested while loop is while statement inside another while statement.

45
Copyright © 2021, ABES Engineering College
Session Plan - Day 3

2.3 Iterative looping Statements


• Range()
• For loop
• Nested for loop
• Examples
• Review Questions
• Summary

46
Copyright © 2021, ABES Engineering College
Quality Content for Outcome based Learning

Introduction to Iterative/looping
statements:

Ver. No.: 1.1 Copyright © 2021, ABES Engineering College


Range() Function,
Optional
Optional default value =
Required
default value = 0 +1

range (start value, stop value, step value)

Range() is a built in function in Python

It gives the sequence of numbers.

48
Copyright © 2021, ABES Engineering College
Range Function Example:

range (1,5,1)

 It will generate a sequence starting from value 1 up to value 4

 (5 is not included) and step by 1

 So, the output of the above gives us the sequence 1,2,3,4.

Note: Range()Function returns the range object, you must typecast range function into
collections.

49
Copyright © 2021, ABES Engineering College
Range

 Range () Function with one Program to generate sequence


argument. numbers 0 to 4.

 Single value is considered as the


stop value.

 It means the start value is


considered as 0, and step value is
considered as 1.

50
Copyright © 2021, ABES Engineering College
Range

 Range () with two argument:


Program to generate sequence of numbers
1 to 4.
 This means the start value and stop
value is mentioned.

 The step values is considered as 1


by default.

51
Copyright © 2021, ABES Engineering College
Range

 Range() with three arguments: Program to generate a sequence of


alternate natural number starting from 1
 It has start value, stop value and the
to 12.
step value, all three values are given
inside range ().
 Here first start value is 1, stop value 12
and step value is 2. That’s why the
output is [1, 3, 7, 9, 11].

52
Copyright © 2021, ABES Engineering College
Example

 Write a program to generate a sequence of starting from 10 to 2.

53
Copyright © 2021, ABES Engineering College
For loop
Syntax of For loop:
 For loop is used when we want to
run a part of our program multiple
times.

 It is also used to traverse


sequences in python like lists, tuple
etc.

54
Copyright © 2021, ABES Engineering College
For loop:

Flowchart of For loop:

 In this flowchart the statements


which we want to run multiple times
would keep on running till, we have
elements in the sequence.

 As no element left, it will come out


from loop.

55
Copyright © 2021, ABES Engineering College
Example:
Example1: Example2:
For loop with range function: Table of number 5 is printed using for loop

56
Copyright © 2021, ABES Engineering College
Nested For loop:

 Nested for loop is called when we use for loop Syntax of Nested For loop:

inside a for loop.


 In python we can use any number of for loop
inside a for loop.
 Generally, we call main for loop as outer for loop
and nested for loop as inner for loop.
 If outer loop running m times and inner loop
running n times, then total iterations would be
m*n.

57
Copyright © 2021, ABES Engineering College
Flowchart of Nested For loop:

58
Copyright © 2021, ABES Engineering College
Example

 We have used two for loops.

 Outer loop is running for two


values-0 and 1 and inner loop is
running for three values-0,1 and 2
and total print statements we are
getting 2*3=6.

59
Copyright © 2021, ABES Engineering College
Can you answer these questions?
1. What is the output of the following code snippet??

a. b.

A. N
How many time special character print?
B. n A. 11
B. 8
C. H
C. 10
D. h
D. 6

Copyright © 2021, ABES Engineering College 60


3. Which of the below programs will print all integers that aren’t divisible by
either 2 or 3 and lies between 1 and 50?

A. B.

C. D.

61
Copyright © 2021, ABES Engineering College
Summary:

 Range function () returns a sequence of numbers.

 A for loop is used for iterating over a sequence (that is either a list, a
tuple, a dictionary, a set, or a string).

 Nested for loop allows us to create one for loop inside another for loop.

62
Copyright © 2021, ABES Engineering College
Session Plan - Day 5

2.4 Jump statements


• Break
• Continue

2.5 Else With Loop


• While with else statement
• For with else statement
• Examples
• Review questions
• Summary

63
Copyright © 2021, ABES Engineering College
Jump Statements

 It is used to :-
 Jump, skip or terminate the iteration or loop from the running program
 Interrupt loops
 Also known as early exit from loop.
 Jump statements are of two types:-
 Break
 Continue

64
Copyright © 2021, ABES Engineering College
Break

 It terminates the execution of loop immediately

 Execution will jump to the next statements.

65
Copyright © 2021, ABES Engineering College
Syntax

66
Copyright © 2021, ABES Engineering College
Example

Explanation :-

 when value of I would be 3 break


statement will be executed and
loop will be terminated and control
moves outside the while loop to
print “All done”.

 If there would have no break, then


loop would had run for i=1 to 4.

67
Copyright © 2021, ABES Engineering College
Continue

 It is used to :-
 Skip ongoing iteration

 continues remaining iterations of loop.

 Jump to the next iteration by skipping the existing one.

68
Copyright © 2021, ABES Engineering College
Syntax

Note: In break statement,


the whole loop will end; and
in continue statement, only
the specific iteration will end.

69
Copyright © 2021, ABES Engineering College
Example

Explanation:-
 The continue statement will
interrupt the on-going iteration
for i=3 and skip all the
statements of current iteration
which are mentioned after the
continue statement like print(i).
 The control goes back to the
while loop for next iteration and
loop moves on.

70
Copyright © 2021, ABES Engineering College
Else with Loop

 While with else statement

 For with else statement

71
Copyright © 2021, ABES Engineering College
While with else Statement

In python the while statement may have an exceptional else clause.

If the condition in the while loop evaluates to


False, the else portion of the code runs.

 if break statement is used in while loop then


else section is not taken into consideration.

72
Copyright © 2021, ABES Engineering College
Flowchart of While with else loop

The else clause will be executed


when the condition becomes False
and the loop runs normally.

The else clause, on the other hand,


will not execute if the loop is
terminated early by a break or
return statement.

73
Copyright © 2021, ABES Engineering College
Example

 Without Break Statement

 While loop will execute normally up to a given condition. No early exit is there so else block
will be executed.

74
Copyright © 2021, ABES Engineering College
Example

 With Break Statement

 While loop will get terminated when value of condition variable = 3, So else
block will not be executed.

75
Copyright © 2021, ABES Engineering College
For with else statement

Python allows else with for loop.

 When all the iteration of for loop


are finished normally, then control
goes to else part.

 If loop has a break statement, then


.
else part would not execute.

76
Copyright © 2021, ABES Engineering College
Syntax of for with else

77
Copyright © 2021, ABES Engineering College
Flow chart of for with else

78
Copyright © 2021, ABES Engineering College
Example

 Program to demonstrate for loop with else.

.
 For loop has been used with else, we can clearly see that, first all the elements
of sequence get executed and when all values from 0 to 4 gets printed, control
goes to else part of the program.
79
Copyright © 2021, ABES Engineering College
Example

 Program to demonstrate for loop with break and else.

.
 We have used break statement with the condition for i=5, so in the output we
are getting values from 0 to 4 and then no else part executed.

80
Copyright © 2021, ABES Engineering College
Example

 Write a program to search a given color name in list of colors. Use Linear/Sequential Search
Techniques.

Test case - 1 For the input value ‘Blue’, output will be –

Enter the color name to be search Blue


Color name Blue found in list

Test case - 2 For the input value ‘Yellow’, output will be –

Enter the color name to be search Yellow


Color name not found in list

81
Copyright © 2021, ABES Engineering College
Example

 Write a program to find a given number is prime or not. A number is prime number if it is
divisible only by itself and 1.
Approach:

 Every number is divisible by 1 and itself, so


in the below code we check that a given
number is divisible by 2 to n-1 or not.

 If divisible means not a prime number Test case - 1 For the input value 11, output will be –
otherwise a prime number. Enter any positive number11
11 is a prime number

. Test case - 2 For the input value 12, output will be –


Enter any positive number12
12 is not a prime number

82
Copyright © 2021, ABES Engineering College
Note:

expression f"{n}" is used to create a formatted string, often referred to as an f-string.


It is a way to embed the value of the variable n within a string. Here's how it works:
•The f character before the opening double-quote indicates that this is an f-string.
•Inside the string, you can place curly braces {} that enclose a
• Python expression or variable name.
•it's {n}, which means you want to
• insert the value of the variable n into the string.
For example, if n is equal to 42, then
f"{n}" would result in the string "42".
It effectively converts the value of n to a string and includes it within the string.

83
Copyright © 2021, ABES Engineering College
Can you answer these questions?
1:-what will be the output of following code:-

A) 0 B) 0 C) 0 D) NONE
1 1 1
2 2 2
3 3 3
4 4 4
5 5 5
6 6 6
7 7
BREAK

84
Copyright © 2021, ABES Engineering College
2:- What will be the output of following code:-

A) 0 B) 0 C) 0 D) NONE
1 1
1 2
2
2 3
4
3 4
5
4 5
5 6
continue continue

85
Copyright © 2021, ABES Engineering College
Summary

 Jump statements in python are used to alter the flow of a loop like you want to
skip a part of a loop or terminate a loop.

 Break Statement in Python is used to terminate the loop.

 Continue Statement in Python is used to skip all the remaining statements in the
loop and move controls back to the top of the loop.

86
Copyright © 2021, ABES Engineering College
References

 https://docs.python.org/3/tutorial/controlflow.html

 Think Python: An Introduction to Software Design, Book by Allen B. Downey

 Head First Python, 2nd Edition, by Paul Barry

 Python Basics: A Practical Introduction to Python, by David Amos, Dan Bader,


Joanna Jablonski, Fletcher Heisler

 https://fresh2refresh.com/python-tutorial/python-jump-statements/

 https://tutorialsclass.com/python-jump-statements/

87
Copyright © 2021, ABES Engineering College
Thank You

88
Copyright © 2021, ABES Engineering College
89
Copyright © 2021, ABES Engineering College

You might also like