Chapter 5 Part-1
Chapter 5 Part-1
CONSTRUCTS
INTRODUCTION: Generally a program executes from beginning to end, but
not many program execute all their statements in strict order from beginning to
end , programs depending upon the need, can choose to execute one of the
available alternative or even repeat a set of statement.
Such statements are called program control statements.
Types of statements: statements are the instructions given to the computer to
perform any kind of action. Python statement can belong to one of the
following three types.
1. Empty statements
2. Simple statements
3. Compound statement.
1. Empty statement: The simplest statement is the empty statement, which
does nothing. In python empty statement is pass statement.
For example:
A=-10
B=5
If a>b:
Pass
Here pass is empty statement.
2. Simple statement :- any single executable is a simple
Syntax:
If<condition expression>:
Statements …………
Example:
# program to evaluates the voter eleigibility .
age=20
if(age>=18):
print("voter is permitted for voting")
print("Thank you for comming")
output:
voter is permitted for voting
Thank you for comming
2. if else statement:- this form of if statement tests a condition
and if the condition evaluates to True. It carries out statements
indented below if. And in case condition evaluates to false, it
carries out statements indented below else.
Syntax:-
If<conditional expression>:
Statement……
Statement……..
else:
Statement………..
Statement…………
Example: 1
# program to evaluates the voter eleigibility .
age=17
if(age>=18):
print("voter is permitted for voting")
print("Thank you for comming")
else:
print("voter is not up to that age")
Example: 2
num=int(input("enter the number"))
if(num%2==0):
print("the number is even")
else:
print("the number is odd")
3. if-elif-else:- sometimes we need to work with
multiple conditions. In this case, only using if-else
construct does not serve the purpose. The if-elif-
else statements provide a compact way to perform
tests on a condition.
For example:- when you visit a bank , you go to the
counter according to the service you want to avail.
If you want to deposit cash, you go to the counter 2,
if you have to enquire for a saving plan, you go to
counter 3 and so on.
Syntax:
if<conditional expression>:
Statement 1
elif<conditional expression>:
statement
elif<conditional expression>:
statement 3
else:
statement 4