0% found this document useful (0 votes)
11 views21 pages

Conditional Execution

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

Conditional Execution

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

Conditional Execution

Decision control structure


Sequence Control vs Decision
control
• In sequence control all the steps are executed line by line – in the
same order as they appear in the program
• But often we want one set of instructions to be followed if the
condition is met and another set of instructions if not. This is called
decision control.
Motivation
• We all need to alter our decisions when circumstances change.

• E.g. If the weather is fine, I’ll go to market.

• If it starts raining, I’d take a cover

• E.g. If the highway is busy, I’d take a diversion

• E.g. If the pitch takes spin, we would win the match


Motivation
• Similarly, we want our program to alter its decisions when conditions
change.

• E.g. if temperature of laptop > 45 then force shutdown

• E.g. if age of person < 14 don’t allow entry to music concert

• Note that the conditions need to be mathematical to be evaluated by


a computer. That’s where the role of Boolean expressions come.
Boolean expressions
• A Boolean expression is an expression which when evaluated produces a Boolean value. A
Boolean value is either true or false

• 5 = 5 is True or False?

• 6 < 4 is True or False?

• No. of students in this class > 50 – True or False?

• Can these expressions have a third value?

• As can be seen a Boolean expression is made up of 2 operands being compared by an operator.


Boolean expressions
• == compares if the operands have same value. Note that we don’t use = to compare two
values, because it is already assignment operator

• x==y # true if x = y, false otherwise


• x!=y # true if x not equal to y
• x>y #true if x greater than y
• x<y # true if x less than y
• x>=y # true if x greater than or equal to y
• x<=y # true if x less than or equal to y
• x is y # true if x is the same as y
• x is not y # true if x is not the same as y

Note: These are all called comparison operators


Modulus Operator
• Symbol: a%b

• It takes two operands a and b and returns the remainder when a is divided by b

• If a%b is equal to 0, then b divides a

• Example:
5%2 = 1 => 5 = 2*2 + 1
8%3 = 2 => 8 = 2*3 + 2
Logical Operators
• There are 3 logical operators
- and
- or
- not

• Python doesn’t use &&, || and !

• Let x and y be two Boolean expressions, then

x and y is true only when both x and y are true, else it is false
x or y is false only when both x and y are false, else it is true
not x is true if x is false, and false if x is true
Logical Operators
• Example:

- x > 0 and x < 10


It is true only if x is greater than 0 and less than 10, just like plain English

-n%2 == 0 or n%3 == 0
It is true if either or both of the conditions is true, that is, if the number is divisible by 2 or 3

-not (x > y)
It is true if x > y is false, that is, if x is less than or equal to y
Conditional Execution
• In python the decision control instruction can be implemented using:

1. if statement

2. if-else statement (Alternative Execution)

3. if-elif-else statement (Chained Conditionals)

4. Nested if statement (Nested Conditionals)


if-statement
• Syntax
if condition_is_true :
execute_this_statement
- The keyword if tells the interpreter that what follows is a decision control instruction

- condition_is_true is a Boolean expression which evaluates to true. Can be within brackets or


opened.

- Colon is placed after the condition to state which instructions to execute once the condition is true

- The instructions to execute are placed after an indent of 4 spaces. Brackets are optional, but indent
is a must. All the instructions within the intent are treated as one chunk and executed together.
if-statement
• Example

if x > 0 :
print('x is positive’)

The indented block is the body of the if statement. There has to be at least 1 statement.
If there is no statement to execute, you can simply type pass. Example:

if x < 0 :
pass
if-statement
• The interpreter will execute the body of if statement until the statements are within the
indent.
• Once the indent ends, the interpreter exits the if-statement and moves to next
statement, which would always be executed whether the condition satisfies or not.
if-else statement
• Syntax:
• if condition_is_true :
execute_this_statement
else :
execute_another_statement
• if statement executes a statement when a given condition is true. If we want to execute different statement
when the condition is false, we use the else block.

• else block is followed by a colon and then in the next line we indent to indicate start of the body or block of
statements to execute
if-else statement
• Example:

if x%2 == 0 :
print('x is even')
else :
print('x is odd')
if-elif-else statement
• if-else is used when there are only 2 possibilities

• When there are >2 possibilities, use if-elif-else statement, elif is short for else if

• Syntax:

if possibility1:
execute_this_statement
elif possibility2
execute_another_statement
elif possibility3
execute_another_statement
.
.
.
else:
execute_a_different_statement
if-elif-else statement
if x < y:
print('x is less than y')
elif x > y:
print('x is greater than y')
else:
print('x and y are equal’)

• There is no limit on number of elif statements


• If there is an else clause, it has to be in the end, but there doesn’t have to be.
Nested-if statements
• Nested means if statement inside of an if-statement

if x == y:
print('x and y are equal')
else:
if x < y:
print('x is less than y’)
else:
print('x is greater than y')
Conditional Expressions (ternary if-
else)
• Syntax
a if condition else b

• This is one line equivalent of:


if condition:
execute_this_statement
else:
execute_another_statement
Practice
Q – Given a point (x,y), write a program to find out if it lies on x-axis, y-
axis or on the origin, viz.(0,0).

Q – If the 3 sides of a triangle are entered though keyboard, write a


program to check whether the triangle is isosceles, equilateral, scalene
or right angled triangle.
Practice
Q - A school has following rules for grading system:
a. Below 25 - F
b. 25 to 45 - E
c. 45 to 50 - D
d. 50 to 60 - C
e. 60 to 80 - B
f. Above 80 - A
Ask user to enter marks and print the corresponding grade.

You might also like