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

Module2-Decision Structure

Uploaded by

Pra Nav
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)
10 views

Module2-Decision Structure

Uploaded by

Pra Nav
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/ 8

Module-2

Decision Structures

Need for decision structures

Algorithm: sequence of actions to solve given problem.


 Sequence: control flow of statements are executed
 Selection/decision: decision is made like condition
 Iteration or loop: one or more instructions repeatedly performs.

Forming Conditions

In Python , the conditions are test expressions. The outcome of the


test expression maybe two or more outcomes. The decision to be taken
depends upon the value of variables or test expression. Test expression
can be Boolean value(True or False), relational operator or logical
operator.

1. Conditions using a Boolean variable:


The Boolean type variable has only two possible values: True and
False. A Boolean expression returns a value - True and False, Boolean
variable that can hold Boolean values.

Example1: Illustration of Boolean Variable


>>>leapYear = True
>>>leapYear
True
>>>
Tn above code, the ython statement creats a variable called
leapYear and sets its value to true. True and False are built-in python
values.

Example2: Illustration of bool function in Interactive shell


>>>bool(0)
False
>>>bool(‘This is string’)
True
>>>x=5
>>>bool(x)
True
>>>
There is a python function called bool that can convert values to Boolean
also. The zero or empty string value is False, and rest of the values are
True. Any values greater than 0 is taken as True by python.

2.Decisions using relational operators


We can forming a condition is by using relational operators. Some
of the relational or comparison operators are <, <=,>, >=,==,and !=. Any
expression that contains a comparison operator returns a Boolean value.

Example 3:Illustration of Relational Operators


>>>x=5
>>>bool(x)
True
>>>x>=9
False
>>>
3.Chained conditions using Logical operations
There are three types of Boolean operators that Python supports.
These operators are not, and, or. Using logical operators, one can
combine operations.
Example 4:
>>>not False
True
>>>True and False
False
>>>True or 0
True

if statements:
if - statement is one of the simplest forms of the decision control
statement. It executes a set of statements conditionally, based on the
value of a logical expression.
 Syntax:
if expression :
statement_1
statement_2
....
statement_n
If the condition is True, then the statement or code of block
corresponding to it is executed. The flowchart is given as shown below:

Example 1:if -statement in interactive mode


>>>a = 33
>>>b = 200
>>>if b > a:
print("b is greater than a")

In above example if the condition fails and does not print anything.
Example2:
>>>a = 20
>>>b = 20
>>>if ( a == b ):
print( “a and b are equal”)
print(“If block ended”)
In example 2, we see that the condition a==b evaluates to True.
Example 3:
num = 5
if ( num >= 10):
print(“num is greater than 10”)
print(“if block ended”)
If- else statement:
In Python if .. else statement, has two blocks. If the condition is not
satisfied, an alternative statement specified by else is executed.

Syntax:
if expression :
statement_1
statement_2
....
else :
statement_3
statement_4
.…

The equivalent flow chart of the if-else statement is given below

Example1:
number1 = 20
number2 = 30
if(number1 >= number2 ):
print(“number 1 is greater than number 2”)
else:
print(“number 2 is greater than number 1”)

Write program using if -else statement.


1. Python program to find largest of two numbers x and y. if x >y , then x
and y should swap.
2. Python program to check whether a number as perfect square or not

Inline-if-Statement:
An inline statement is a convenient form of representing a if-else
statement. This is known as ternary operator.this statement allows one to
execute conditional if statements in a single line.
Syntax: expression1 if condition else expression2
If the condition is True, then expression1 is executed, otherwise
expression2 is executed
Syntax:
expression1 if condition1 else expression2 if condition2 else
expression3
If the condition1 is True, then expression1 is executed. If the
condition1 is False, then condition 2 is executed. If the condition2 is True,
then expression2 is carried out, otherwise if condition False, expression3
is returned.
Example:
scoreMark = int(input(“Enter entrance exam mark:”))
Cutoff= 400
Admission = False if (scoreMark < Cutoff) else True
Print(admission)
Output:
Enter entrance exam mark: 325
False

If - elif statement:

In Python, the if-elif-else condition statement has an elif blocks to


chain multiple conditions one after another. This is useful when you need
to check multiple conditions.
With the help of if-elif-else we can make a tricky decision.
The elif statement checks multiple conditions one by one and if the
condition fulfills, then executes that code.

Syntax:
if condition-1:
statement 1
elif condition-2:
statement 2

elif condition-n:
statement n
else:
else-statement
Example:

def user_check(choice):
if choice == 1:
print("Admin")
elif choice == 2:
print("Editor")
elif choice == 3:
print("Guest")
else:
print("Wrong entry")

user_check(1)
user_check(2)
user_check(3)
user_check(4)

Output:

Admin
Editor
Guest
Wrong entry

Example:
1. Python program for grade allocation can be done using if-elif
statement
2. Python program should start with obtaining the height and weight of
the person and should obtain BMI of the person.

Nested if-else statement

In Python, the nested if-else statement is an if statement inside another if-


else statement. It is allowed in Python to put any number of if statements
in another if statement.

Indentation is the only way to differentiate the level of nesting. The


nested if-else is useful when we want to make a series of decisions.

Syntax:
if condition_outer:
if condition_inner:
statement of inner if
else:
statement of inner else:
statement of outer if
else:
Outer else
statement outside if block

The flow chart for nested if conditions

Example1:
num1 = int( input())
num2 = int( input())
if( num1>= num2):
if(num1 == num2):
print(f'{num1} and {num2} are equal')
else:
print(f'{num1} is greater than {num2}')
else:
print(f'{num1} is smaller than {num2}')

You might also like