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

3.control Flow

The document discusses various Python programming concepts related to flow control, including conditional statements like if, if-else, and elif statements for decision making, as well as looping statements like while and for loops. It provides examples of using these statements to write programs that perform tasks like checking conditions, iterating through ranges of numbers, and controlling loop execution with break and continue. The document also presents example problems involving numbers that can be solved using these flow control concepts.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

3.control Flow

The document discusses various Python programming concepts related to flow control, including conditional statements like if, if-else, and elif statements for decision making, as well as looping statements like while and for loops. It provides examples of using these statements to write programs that perform tasks like checking conditions, iterating through ranges of numbers, and controlling loop execution with break and continue. The document also presents example problems involving numbers that can be solved using these flow control concepts.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

Python Programming

Flow Control

Assistant Professor & Technical Trainer


Madanapalle Institute of Technology & Science
[email protected]
9500600868
Operators

2
1.Conditional Statements
 In python, we can achieve decision making by using the below statements:

 if statements

 if-else statements

 elif statements

 Nested if and if-else statement

3
Decision Making

‘if’ statement
 ‘if’ is single branch decision making statement.
Flowchart of if
 If condition is true then only body will be executed.
 ‘if’ is a keyword.
False
Syntax condition
if (condition) or condition :
// Body of the if True
// true part
Indentation is mandatory for body

4
Decision Making

‘if’ statement
 WAP to print Zero if given number is 0
Program Output
a=int(input(“Enter a number:”)) Enter a number:0
Zero
if (a == 0):
print("Zero");

 WAP to check the given number is positive


 WAP to check the given number is two digit number
 WAP to check the given number is positive or negative number

5
Decision Making
‘if..else’ statement
 ‘if..else’ is two branch decision making statement.
 If condition is true then only body will be executed.
Flowchart of if…else
 ‘else’ does not have condition
Syntax
True False
if(condition): condition
// true part
else:
// false part
… …

6
Decision Making

‘if-else’ statement

 WAP to check the given number is positive or negative or zero


 WAP to check the given number is two digit number or not
 WAP to check the given number is divisible by 3 and 5 or not.
 WAP to test whether a number is divisible by 5 and 10 or by 5 or 10
 WAP to check whether given number is single or double or Above two or
Negative number
 Write a program to check the given number is divisible by 5 or not, if not print
the remainder value.

7
Decision Making
Question: Program to check the first element is greater or lesser or between than other 2
numbers
Input-1:
a=18 b=12 c =15
Output:
‘a is greater than b and c’

Input-2:
a=12 b=10 c=15
Output:
‘a is greater than b and lesser than c’

8
Decision Making

9
Decision Making

10
Decision Making

11
Looping Statements
Life is all about Repetition
- We do same thing everyday
What is loop?
 Loop is used to execute the block of code several times according to the condition
given in the loop.
 It means it executes the same code multiple times.
Output “Hello” 5
printf("Hello\n"); Hello
printf("Hello\n"); Hello loop(condition)
//statements
printf("Hello\n"); Hello
printf("Hello\n"); Hello
printf("Hello\n"); Hello

12
Looping Statement

1.’ While’ loop


 while is an entry controlled loop
 Statements inside the body of while are repeatedly executed till the condition is true
 while is keyword

Syntax
#Initialization
While condition:
// Body of the while
// true part
// Increment/Decrement

Prof. M.Sivakumar #20CS201 (C & DS)  Module 1 – C Programming 13


Looping Statement
2.’ for’ loop
 ‘for’ is an entry controlled loop
 Statements inside the body of for are repeatedly executed till the condition is true
 for is keyword Syntax
for i in range(end value):
// Body of the for
// true part

for x in range(3) for x in range(1,3) for x in range(3,0,-1) for x in range(5,0,-2)


print(x) print(x) print(x) print(x)

for x in range(-4,4) for x in range(1,10,2)


print(x) print(x)

Prof. M.Sivakumar #20CS201 (C & DS)  Module 1 – C Programming 14


Looping Statement

Prof. M.Sivakumar #20CS201 (C & DS)  Module 1 – C Programming 15


Looping Control Statement

1.’ break’ keyword


 ‘break’ will terminate the loop or take out the execution control from the loop
 Always ‘break’ must be linked with loop directly or in-directly

Syntax
numbers = [5, 9, 7, 13, 8, 1, 11]
for i in numbers:
if i%2 == 0:
print(“Terminates")
break
print(i, end=' ')

Prof. M.Sivakumar #20CS201 (C & DS)  Module 1 – C Programming 16


Looping Control Statement

2.’ continue’ keyword


 ‘continue’ will skip the current iteration of the loop by leaving the remaining body
statements
 Always ‘continue’ must be linked with loop directly or in-directly
Ex-1 Ex-2
numbers = [5, 9, 7, 13, 8, 1, 11] country = ['India', 'China', 'UAE', ‘UK']
for i in numbers: for x in country:
if i%2 == 0: if x == 'China':
print(“Terminates") continue
continue
print(x)
print(i, end=' ')

Prof. M.Sivakumar #20CS201 (C & DS)  Module 1 – C Programming 17


‘For Else’ and ‘While Else’ Statement
 ‘Else block’ will be executed only if the loop isn't terminated by a break statement

Prof. M.Sivakumar #20CS201 (C & DS)  Module 1 – C Programming 18


For Else and While Else Statement
For-else Syntax While-else Syntax

Ex-2
Ex-1 country = ['India', 'China', 'UAE', ‘UK']
numbers = [5, 9, 7, 13, 8, 1, 11] i=0
for i in numbers: while i<len(country):
if i%2 == 0: if x == 'China':
print(“Even found") print(“China is found”)
break break
print(i, end=' ') i = i+1
else:
else:
print(“No .even number”)
print(“China is not there”)
Prof. M.Sivakumar #20CS201 (C & DS)  Module 1 – C Programming 19
Problems - Numbers
1. Sum the given number {Input: 89789 Output: 41}
2. Sum the given number upto single digit. {Output: 5}
3. Find the next prime number. {Input:16 Output: 17}
4. Swap the first and last digit of the given number. {Input:23546 Output:63542}
5. Odd Sequence first and Even Sequence next. {Input:658134 Output:513684}
6. Odd Sequence first with its count and Even Sequence next with count .
{O/P:51336843}
7. Step Number – Yes or No {Input:54567 Output: Yes}
8. Reverse only the even numbers. {Input:325649 Output:345629}
9. Encoding {Input:55553322255 Output:54322352}
10. Reverse Odd Digit {Input:816253 Output:351}
20
Problems - Numbers
11. Convert every number into next sequence number.{Input: 89781 Output: 90892}
12. Persistence of a number.{Input: 715 Output: 5 [715 ---- 35 ---- 15 ---- 5]}
13. Check the given number is Perfect number or not. { Input: 28 Output: Yes [1 + 2 + 4 + 7
+ 14] }
14. Print the total factor count of a given number. {Input: 12 Output: 6}
15. Print the total prime factor count of a given number. {Input: 12 Output: 2}

21

You might also like