Chap 6 Python
Chap 6 Python
CONTROL STRUCTURES
LEARNING OBJECTIVES
•Sequential
•Alternative Or Branching
•Iterative or Looping
Sequential Statement
Output
Hello! This is Shyam
43, Second Lane, North Car Street, TN
Sequential Statement
Output:
Enter the radius value 8
The area of circle is 200.96
Alternative or
Branching Statement
if….else statement
if….elif statement
Simple if statement
y=int(input(“Enter number”))
if y > 0 : Enter number : - 4
print( “The given number is >>>
positive”)
if..else statement
Output 2:
Enter any number :67
67 is an odd number
if..else statement
• An alternate method to write the if..else program is
also available in Python.
• The complete if..else can also written as
Output 2:
Enter any number :22
22 is even
Nested if..elif...else statement:
Statements-Block1
[else : Optional part
Statements-Block2]
While loop -
Flow diagram
• In the while loop, the condition is any valid Boolean
expression returning True or False.
• The else part of while is optional part of while.
• The statements block1 is kept executed till the
condition is True.
• If the else part is written, it is executed when the
condition is tested False.
• while loop belongs to entry check loop type, that is
it is not executed even once if the condition is
tested False in the beginning.
EXAMPLE –WHILE LOOP
Output:
10 11 12 13 14 15
Value of i when the loop exit 16
PARAMETERS OF print()
Output:
10 11 12 13 14 15
FOR LOOP
• for loop is the most comfortable loop. It is also an
entry check loop.
• The condition is checked in the beginning and the
body of the loop(statements-block 1) is executed if it
is only True otherwise the loop is not executed.
• SYNTAX
for counter_variable in sequence:
Statements-Block1
[else : # optional part
Statements-Block2]
Importance of range()
• SYNTAX
continue
Working of
continue statement
• The working of break statement in for loop and while loop
for var in sequence:
if condition:
continue
#code inside for loop
#code outside for loop
while test expression:
if condition:
continue
#code inside while loop
#code outside while loop
continue - Examples
Output:
Computr
End of the program
pass statement
• pass statement in Python programming is a null
statement.
• pass statement when executed by the interpreter it
is completely ignored.
• Nothing happens when pass is executed, it results in
no operation.
• pass statement can be used in ‘if’ clause as well as
within loop construct, when you do not want any
statements or commands within that block to be
executed
pass statement
• SYNTAX
pass
a=int (input(“Enter any number :”))
if (a==0): Output:
pass Enter any number :3
else: non zero value is accepted
print (“non zero value is accepted”)