Professional Documents
Culture Documents
Sequential
Selectional (conditional-branching)
Iterational (Loops)
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
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
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