Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
14 views

Decision Control Statements - Python

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)
14 views

Decision Control Statements - Python

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

Decision Control Statements - Python

Decision Control statements are of two types:

- Conditional statements (If - else)


- Iterative statements (loop constructs)

Indentation is very important with decision control statements!

Conditional Statements:

Syntax: if-else statement:

if (test expression):
statement block 1
else:
statement block 2

Example:

x=8
if (x==10):
print(x+1)
else:
print(x-1)

Exercise – 1: Write a program to find if a given number is even or odd.

Syntax: if-elif-else statement:

if (test expression - 1):


statement block 1
elif (test expression – 2):
statement block 2
.........
.........
else:
statement bock x

Example:

num = int(input("Enter any number "))


if(num==0):
print("Zero entered")
elif (num >0):
print("Positive number entered")
else:
print("Negative number entered ")

Exercise – 2: Write a program that prompts the user to input any number from 1 to 7 and then
displays the corresponding day of the week.

Syntax of nested if-else statements:

if (test expression - 1):


if(test expression – 2):
statement block 1
else # optional
statement block 2
elif / else(test expression – 3):
statement block 3

Exercise – 3: Write a program to find greatest number from three numbers.

NOTE: If we have complex test expression, then we should appropriately use logical
operators (AND, OR , NOT). In Python, the following expression is invalid:

if (50 <= num <= 100):

The correct way to represent the above expression in Python is:

if ((num >= 50) and (num <= 100)):

Exercise – 4: Write a program that prompts the user to enter any number and then print its
interval to be one of these:

i) Between 0 and 10 : check 0 <= num < 10


ii) Between 10 and 20 : check 10 <= num < 20
iii) Between 20 and 30 : check 20 <= num < 30

Iterative Statements:

Syntax of while statement:

statement block X
while (conditional expression):
statement block
statement block Y
Example:

i=0
while(i<10)
print(i)
i+=1

In this example, value of i is printed from 0 to 9 because the condition checked within while()
remains true until i is less than 10 – counting from 0 to 9, we can say that the while loop has
executed 10 times. Or, we say there are 10 iterations of while loop in this program.

However, if the condition within while() were i<=10 – then the loop would have
executed one more time printing the value of i from 0 to 10 – and then, we would say while
loop has executed 11 times. Or, there are 11 iterations of while loop in this program.

Exercise – 5: Write a program that prompts the user to enter a number and then calculates the
sum of its digits.

NOTE: This program requires us to be very careful with the data types.

Syntax of for statement:

statement block X
for loop_variable in sequence:
statement block
statement block Y

We can iterate using ‘for’ loop over a sequence of numbers, such as present in list, sets,
tuples, or we can make use range() function to allow ‘for’ loop to execute for a pre-declared
number of times.

Range() – is a built-in function to iterate over a sequence! It can take any of the following
three forms:

i) range(end) – here ‘end’ parameter indicates that the loop_variable should execute
from 0 till (end-1). An example:

for i in range(10):
print(i)

This loop will print values of i from 0 to 9 (end-1), i.e. incrementing value of i by
1 in each iteration.

ii) range(beg, end) – here ‘beg’ indicates the start value of loop_variable and ‘end’
indicates that loop_variable should execute till (end-1). An example:
for i in range(1, 10):
print(i)
This loop will print values of i from 1 (start value) to 9 (end-1), i.e. incrementing
value of i by 1 in each iteration.

iii) range(beg, end, step) - here, ‘beg’ and ‘end’ have same interpretation as above
(i.e. incrementing the loop control variable stops at end - 1), ‘step’ indicates the
increment value in loop_variable. An example:

for i in range(1, 10, 2):


print(i)

Here, step of 2 indicates that value of i should be incremented by 2 in each of the


next iteration. Consequently, the output printed this time would be:

1
3
5
7
9

iv) Printing list elements and strings using range function - List and string characters
can be accessed using subscript operator, [] - to print list or string elements, we
will find the length of list or string using len() function. and use range function
with the length of list/string. An example:

list1 = [1,2,3,4,5,6,7,8]
for i in range(len(list1)):
print(list1[i])

str1 = "LOCK HAVEN"


for i in range(len(str1)):
print(str1[i])

Exercise – 6: Write a program to find average of first ‘n’ natural numbers (that start from 1).
For example – say ‘n’ is 5, then the program should compute sum as: 1+2+3+4+5 = 15, and
compute average as 15/5 = 3.0

Exercise – 7: Write a program to compute exponent, i.e. program that replicates the exponent
operator **.

Exercise – 8: Write a program to find sum of squares of even numbers.

Exercise – 9: Nested For loop: Print these pattern:

*
**
***
****
*****
*
**
***
****
*****

NOTE: Listed below are few more useful statements that can appear within the body of the
loop only:

1. Break statement – allows breaking from the current loop!

Scenario 1:
while (conditional expression):
…..
if condition:
break
…..
Transfers control out of while
…..
….. loop

Scenario 2:
for conditional expression:
…..
if condition:
break
…..
Transfers control out of for
…..
loop
…..
Scenario 3:

for conditional expression:


…..
for conditional expression
…..
if condition:
break
….. Transfers control out of inner
…..
for loop
…..

2. Continue statement – when compiler encounters this statement, then rest of the
statements in the loop are skipped and the control is unconditionally transferred to
loop-continuation part of nearest loop.

Following are the possible scenarios for continue statement:

Scenario 1:
while (conditional expression):
…..
if condition:
continue
…..
….. Transfers control to the conditional
….. expression of while loop
Scenario 2:

for conditional expression:


…..
if condition:
continue
…..
….. Transfers control to the conditional
….. expression of for loop

Scenario 3:

for conditional expression:


…..
for conditional expression
…..
if condition:
continue
…..
….. Transfers control to the conditional
….. expression of inner for loop

You might also like