You are on page 1of 36

Different patterns in Algorithm

Sequential

 Sequential structure executes the program in the order in which they


appear in the program

Selectional (conditional-branching)

 Selection structure control the flow of statement execution based on


some condition

Iterational (Loops)

 Iterational structures are used when part of the program is to be


executed several times
Selection pattern

• A selection control statement is a control

statement providing selective execution of

instructions.
Control flow of decision making
If Statement
• An if statement is a selection control
statement based on the value of a given
Boolean expression.
The if statement in Python is
If statement Example use

If condition: If grade >=70:


statements print(‘pass’)
else: else:
statements Print(‘fail’)
Indentation in Python
• One fairly unique aspect of Python is that the amount of
indentation of each program line is significant.
• In Python indentation is used to associate and group
statements
Sample Code
k=3
l=2
if k>l:
print("K is greater")

Output
K is greater
Sample code
k=3
l=4
if k>l:
print("K is greater")
else:
print("l is greater")

Output
l is greater
Nested if Statements
• There are often times
when selection among
more than two sets of
statements (suites) is
needed.
• For such situations, if
statements can be
nested, resulting in
multi-way selection.
Nested if else
input_number = int(input("enter number"))
if input_number==1:
print("one")
else:
if input_number==2:
print("two")
else:
if input_number==3:
print("three")
else:
print("Invalid Input")
Else if Ladder
Use if elif statements

input_number = int(input("enter number"))


if input_number==1:
print("one")
elif input_number==2:
print("two")
elif input_number==3:
print("three")
else:
print("Invalid Input")
Multiple Conditions
• Multiple conditions can be check in a ‘if’
statement using logical operators ‘and’ and
‘or’.
• Python code
To print ‘excellent’ if mark1 and mark2 is
greater than or equal to 90,
Print ‘good’ if mark1 or mark2 is greater
than or equal to 90,
Print ‘need to improve’ if both mark1 and
mark2 are lesser than 90
if mark1>=90 and mark2 >= 90:
print(‘excellent’)
if mark1>=90 or mark2 >= 90:
print(‘good’)
else:
print(‘needs to improve’)
Algorithm for Largest of Three
numbers
Step1: Start
Step2: Read value of a, b and c
Step3: If a is greater than b then
compare a with c and if a is bigger then say
a is biggest else say c is biggest
else Compare b with c , if b is greater than
c say b is biggest else c is biggest
Step 5: Stop
Flowchart
Start

Accept a,b and c

Y
Is Y Is display a
a>b a>c is bigger
N N
Is N display c
b>c is bigger
Y
display b
is bigger

Stop
Test Cases
Input
a = 12, b = 13, c = 14

Output
c is greatest

Processing Involved
B is greater than a but c is greater than b
Test Cases
Input
a = 13, b = 12, c = 14

Output
c is greatest

Processing Involved
a is greater than b but c is greater than a
Test Cases
Input
a = 13, b = 2, c = 4

Output
a is greatest

Processing Involved
a is greater than b and a is greater than c
Test Cases
Input
a = 3, b = 12, c = 4

Output
b is greatest

Processing Involved
b is greater than a and b is greater than c
Greatest of three numbers
number1=int(input("Enter first number"))
number2=int(input("Enter second number"))
number3=int(input("Enter the third number"))
if number1>number2>number3:
print("first number is greater than other two numbers")
elif number2>number1>number3:
print("second number is greater")
else:
print("number 3 is greater")
Even or odd
number=int(input("Enter a number"))
if(number%2==0):
print("The number is even")
else:
print("The number is odd number")
Greatest of two numbers
number1=int(input("Enter first number"))
number2=int(input("Enter second number"))
if(number1>number2):
print("first number is greater")
else:
print("second number is greater")
Leap Year Program
year=2016
if year%4==0 and year%100!=0 or year%400==0:
print("leap year")
else:
print("not a leap year")
Mark Range
mark=int(input("Enter the marks"))
if 0<=mark<=39:
print("failed")
elif 40<mark<=100:
print("passed")
else:
print("Invalid input")
Checking vowels
ch=input("Enter a character")
if ch=='a'or ch=='e'or ch=='i'or ch=='o'or ch=='u':
print("its a vowel")
else:
print("its not a vowel")
Single Statement Suites
If the suite of an if clause consists only of a
single line, it may go on the same line as the
header statement.
Here is an example of a one-line if clause:
#!/usr/bin/python
var = 100
if ( var == 100 ) : print ("Value of expression is 100")
print ("Good bye!")
Need of iterative control
Repeated execution of set of statements
• An iterative control statement is a control
statement providing repeated execution of a
set of instructions
• Because of their repeated execution, iterative
control structures are commonly referred to
as “loops.”
While Loop
• Syntax
The syntax of a while loop in Python
programming language is:

while expression:
statement(s)
Cont...
#!/usr/bin/python
count = 0
while (count < 9):
print ('The count is:', count)
count = count + 1
print ("Good bye!")
Print values from 0 to 9 in a line
a=0; b=10
while a < b: # One way to code counter loops
print(a, end=' ')
a += 1 # Or, a = a + 1

Output:
0123456789

Include end=‘ ‘ in print statement to suppress default


move to new line
Using else Statement with Loops
• Python supports to have an else statement
associated with a loop statement.
• If the else statement is used with a for loop,
the else statement is executed when the loop
has exhausted iterating the list.
• If the else statement is used with a while
loop, the else statement is executed when
the condition becomes false. The while loop
can be terminated with a break statement. In
such cases, the else part is ignored.
EX:
#!/usr/bin/python
count = 0
while count < 5:
print (count, " is less than 5")
count = count + 1
else:
print (count, " is not less than 5")
Sum and Average using While Loop
n = 20
total_numbers = n
sum=0
while (n >= 0):
sum += n
n-=1
print ("sum using while loop ", sum)
average = sum / total_numbers
print("Average using a while loop ", average)
Check if a given number is Prime
Class Average

You might also like