3.control Flow
3.control Flow
Flow Control
2
1.Conditional Statements
In python, we can achieve decision making by using the below statements:
if statements
if-else statements
elif statements
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");
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
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
Syntax
#Initialization
While condition:
// Body of the while
// true part
// Increment/Decrement
Syntax
numbers = [5, 9, 7, 13, 8, 1, 11]
for i in numbers:
if i%2 == 0:
print(“Terminates")
break
print(i, end=' ')
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