Unit-2 Control Flow System
Unit-2 Control Flow System
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 Iterative/looping statements and jump statements.
To apply the knowledge of conditions, loops and jump in solving real time problems.
3
Copyright © 2021, ABES Engineering College
Topics Covered
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
5
Copyright © 2021, ABES Engineering College
Introduction
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.
8
Copyright © 2021, ABES Engineering College
Selection/decision control statements
It is used to control the flow of execution of program depending upon
condition:
9
Copyright © 2021, ABES Engineering College
Revision of Flowchart
Connector / flow
10
Copyright © 2021, ABES Engineering College
Flowchart of the grading system
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,
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
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
26
Copyright © 2021, ABES Engineering College
Can you answer these questions?
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
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
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..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.
34
Copyright © 2021, ABES Engineering College
Types of loops in Python
While loop
For loop
35
Copyright © 2021, ABES Engineering College
While loop
36
Copyright © 2021, ABES Engineering College
Syntax of While loop
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
39
Copyright © 2021, ABES Engineering College
Syntax of Nested While loop:
40
Copyright © 2021, ABES Engineering College
Example of Nested 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
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
46
Copyright © 2021, ABES Engineering College
Quality Content for Outcome based Learning
Introduction to Iterative/looping
statements:
48
Copyright © 2021, ABES Engineering College
Range Function Example:
range (1,5,1)
Note: Range()Function returns the range object, you must typecast range function into
collections.
49
Copyright © 2021, ABES Engineering College
Range
50
Copyright © 2021, ABES Engineering College
Range
51
Copyright © 2021, ABES Engineering College
Range
52
Copyright © 2021, ABES Engineering College
Example
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.
54
Copyright © 2021, ABES Engineering College
For 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:
57
Copyright © 2021, ABES Engineering College
Flowchart of Nested For loop:
58
Copyright © 2021, ABES Engineering College
Example
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
A. B.
C. D.
61
Copyright © 2021, ABES Engineering College
Summary:
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
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
65
Copyright © 2021, ABES Engineering College
Syntax
66
Copyright © 2021, ABES Engineering College
Example
Explanation :-
67
Copyright © 2021, ABES Engineering College
Continue
It is used to :-
Skip ongoing iteration
68
Copyright © 2021, ABES Engineering College
Syntax
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
71
Copyright © 2021, ABES Engineering College
While with else Statement
72
Copyright © 2021, ABES Engineering College
Flowchart of While with else loop
73
Copyright © 2021, ABES Engineering College
Example
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
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
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
.
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
.
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.
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:
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
82
Copyright © 2021, ABES Engineering College
Note:
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.
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
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