0% found this document useful (0 votes)
32 views6 pages

Chapter 5 Part-1

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views6 pages

Chapter 5 Part-1

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

CHAPTER: 5 CONDITIONAL AND LOOPING

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

Compound statement has:


A header line which begin with keyword and ends with a colon. For example:
if a>b:
A body consisting of one or more python statements each indented inside the
header line. All statements in the python body are at the same level of
indentation.
Example:
In a program, statement may be executed sequentially, selectively or
iteratively. Every programming language provides constructs to support
sequence, selection or iteration.
SEQUENCE: The sequence construct means the statements are being executed
sequentially. This represents the default flow of statements.
Program to calculate the area of triangle.
length=10
height=20
area_tri=0.5*length*height
print("area of triangle is : ", area_tri)
area of triangle is : 100.0
SELECTION:-
The selection construct means the execution of statement depends on a
condition. If a condition evaluates to True, a set of statements will be executed
otherwise another set of statements will be executed. This construct also
known as decision/conditional constructs.
Types of selection/conditional statements:-
1. if
2. if-else
3. if-elif-else
1. if statement:- if statement test a condition and If the condition
evaluates to True , it carries out some instructions and does nothing
in case condition evaluates to false.

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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy