0% found this document useful (0 votes)
3 views

Control Structures

The document discusses decision-making statements in Python programming, including if, if-else, nested if, and multiway if-elif-else statements. It provides syntax, examples, and important points to remember for each type of statement. Additionally, it covers the use of conditional expressions to simplify code.

Uploaded by

Ritvik Bansal
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)
3 views

Control Structures

The document discusses decision-making statements in Python programming, including if, if-else, nested if, and multiway if-elif-else statements. It provides syntax, examples, and important points to remember for each type of statement. Additionally, it covers the use of conditional expressions to simplify code.

Uploaded by

Ritvik Bansal
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/ 16

Decision Making

Dr. Kusum Lata


Assistant Professor
USME, East Delhi Campus
Delhi Technological University
Decision making statements
• Some times situations arises in programming also where we need to make some
decisions and based on these decisions we will execute the next block of code.
Python supports various decision making statements.
• Decision making statements in programming languages decides the direction of
flow of program execution.
• These are:
I. if statement
II. if-else statement
III. Nested if statement
IV. multiway if-elif-else statement
if statement
if statement is the most simple decision making statement. It is used to
decide whether a certain statement or block of statements will be
executed or not i.e if a certain condition is true then a block of
statement is executed otherwise not.
Syntax:
if condition:
Statements to execute
if statement…..point to remember
(a) The statement must be indented at least one space right of the if
statement.
(b) A colon(:) must always be followed by the condition.
(c) In case there is more than one statements after the if condition, then
each statement must be indented using the same no. of space to avoid
indentation error.
Program:
Num1=eval(input(‘enter first number’))
Num1=eval(input(‘enter first number’))
if Num1-Num2==0:
print(‘both numbers are equal’)
if statement…..point to remember
Sometimes a program may contain only one statement within the if
block. In this case, the if statement can be written in two different way
as follows:
(a) if number>0:
number=number*number
(b) if number>0: number=number*number
Python if...else Statement
The if statement executes when the condition following the if is true,
and it do nothing when the condition is false. The if-else statement takes
care of a true as well as false condition. The syntax of if-else statement is
given as follows:
Syntax:
Syntax:
if condition: if condition:
Statements to execute if_block
else: else:
Statements to execute else_block
Python if...else Statement

Program:
Num1=eval(input(‘enter first number’))
Num1=eval(input(‘enter first number’))
if Num1>Num2:
print(Num1, ‘is greater than’, Num2)
else:
print(Num2, ‘is greater than’, Num1)
If-else statement…..point to remember
(a) Indentation is very important in Python. The else keyword must be
properly lined up with the if statement.
(b) In case the if and the else keywords are not in same column, then
Python will not know that if and else will go together and it will
show indentation error.
If-else statement-Example
Write a program to test whether a number is divisible by 5 and 10 or
by 5 or 10
Num1=eval(input(‘enter a number’))
if (Num1%5==0 and Num1%10==0):
print(Num1, ‘is divisible by both 5 and 10’)
if(Num1%5==0 or Num1%10==0):
print(Num1, ‘is divisible by 5 or 10’)
else:
print(Num1, ‘is divisible not by 5 and 10’)
Nested If statement
We can have a if-else statement inside another if-else
statement. This is called nesting in computer programming.
Any number of these statements can be nested inside one
another. Indentation is the only way to figure out the level of
nesting.
Syntax: In this syntax, if Boolean
if Boolean_expression1: expression 1 and Boolean expression
if Boolean_expression2: 2 are correct then statement 1 will
execute. If Boolean expression 1 is
Statement1 correct but Boolean expression 2 is
else: incorrect, statement 2 will
Statement2
execute. And if both Boolean
expression 1 and Boolean expression
else: 2 are false then statement 3 will
Statement3 execute.
Program to demonstrate Nested If statement
x = float(input("Enter a number: "))
if x >= 0:
if x== 0:
print("Zero")
else:
print("Positive number")
else:
print("Negative number")
Python Multiway if...elif..else Statement
The elif is short for else if. It allows us to check for multiple expressions. If the
condition for if is false, it checks the condition of the next elif block and so on.
If all the conditions are false, the body of else is executed. Only one block
among the several if...elif...else blocks is executed according to the condition.
The if block can have only one else block. But it can have multiple elif blocks.

Syntax: In multiway if..elif..else, Boolean


if boolean expression1: expressions are checked from top to
Statement1 bottom. When a true condition is
elif boolean expression2: found, the statements associated
Statement2 with it is executed and the rest of
………. the conditional statements are
……….. skipped. If none of the conditions
……….., are found true, then the last else
else: statement is executed. If all other
Statement n conditions are false and the final
else statement is not present then
no action takes place.
Python Multiway if...elif..else Statement
The elif is short for else if. It allows us to check for multiple expressions. If the
condition for if is false, it checks the condition of the next elif block and so on.
If all the conditions are false, the body of else is executed. Only one block
among the several if...elif...else blocks is executed according to the condition.
The if block can have only one else block. But it can have multiple elif blocks.

Syntax: In multiway if..elif..else, Boolean


if boolean expression1: expressions are checked from top to
Statement1 bottom. When a true condition is
elif boolean expression2: found, the statements associated
Statement2 with it is executed and the rest of
………. the conditional statements are
……….. skipped. If none of the conditions
……….., are found true, then the last else
else: statement is executed. If all other
Statement n conditions are false and the final
else statement is not present then
no action takes place.
Python Multiway if...elif..else Program
day =int(input(“enter a day of weekn(1-7)”))
If day==1:
print(“Monday”)
elif day==2:
print(“Monday”)
elif day==3:
print(“Monday”)
elif day==4:
print(“Monday”)
elif day==5:
print(“Monday”)
elif day==7:
print(“Monday”)
elif day==7:
print(“Monday”)
else:
print(“invalid input, please try again”)
Conditional Expression
Consider the following piece of code:
if x%2==0
x=x*x
else:
x=x**3
Using conditional expression, the above code is rewritten as:
x=x*x if x%2==0 else x**3
General form of conditional expression is:
Expression1 if condition else Expression2
Conditional Expression
Program:
Num1=eval(input(‘enter first number’))
Num1=eval(input(‘enter first number’)) print(Num1, ‘is greater than’,
Num2) if Num1>Num2 else print(Num2, ‘is greater than’, Num1)

You might also like