Problem Solving and Python Programming: UNIT III: Control Flow, Functions, Strings

Conditional Statements

Flow Chart, Syntax, Example Program | Python Programming

There are various types of conditional statements - i) if statements ii) if-else or alternate statements iii) nested if iv) chained conditionals

Conditional Statements

AU : Dec 19,Marks 16

There are various types of conditional statements -

i) if statements

ii) if-else or alternate statements

iii) nested if

iv) chained conditionals

 

1.  if statement

The if statement is used to test particular condition. If the condition is true then it executes the block of statements which is called as if block.


The if statement is the simplest form of the conditional statement.

Syntax :

if condition:

statement

Example

if a < 10:

print("The number is less than 10")

 

Example 3.4.1 Write a Python program to check whether given number is even or odd.

Solution :


 

2. Alternative Statements

• The if-else statement provides an else block combined with the if statement which is executed in the false case of the condition. The flowchart for if-else is


Syntax

if condition :

else :

statement

statement

• If the condition is true, then the if-block is executed. Otherwise, the else-block is executed.

ifelseDemo.py

print("Enter value of n")

n = int(input()) if n%2==0:

print("Even Number")

else:

print("Odd Number")



3. Nested if Statements

When one if condition is present inside another if then it is called nested if conditions. Any number of these statements can be nested inside one another. Indentation is the only way to figure out the level of nesting.

 

Example 3.4.2 Write a python program to compare two numbers using nested conditionals,

Solution :

NestedIfDemo.py

print("Enter value of a")

a = int(input())

print("Enter value of b")

b = int(input())

if a= =b:

print("Both the numbers are equal")

else:

if a<b:

print("a is less than b")

else:

Nested if-else

if a>b:

print("a is greater than b")


Output

Enter value of a

20

Enter value of b

10

a is greater than b

>>> 

 

4.  Chained Conditionals

Sometimes there are more than two possibilities. These possibilities can be expressed using chained conditions. The syntax for this is as follows

if condition:

Statement

elif condition:

Statement

else:

Statement

• The chained conditional execution will be such that each condition is checked in order.

• The elif is basically abbreviation of else if.

• There is no limit on the number of elif statements.

• If there is else clause then it should be at the end.


• In chained execution, each condition is checked in order and if one of the condition is true then corresponding branch runs and then the statement ends. In this case if there are any remaining conditions then those condition won't be tested.

 

Example 3.4.3 Write a python program to display the result such as distinction, first class, second class, pass or fail based on the marks entered by the user.

Solution :

print("Enter your marks")

m = int(input())

if m > = 75:

print("Grade : Distinction")

elif m > =  60:

print("Grade : First Class")

elif m > = 50:

print("Grade : Second Class")

elif m > = 40:

print("Grade: Pass Class")

else:

print("Grade : Fail")

Output

Enter your marks

45

Grade : Pass Class

 

Example 3.4.4 Write a python program to find the largest among the three numbers.

Solution :


Output


Review Question

1. Explain three types of conditional statements.

AU : Dec 19, Marks 16

 

Problem Solving and Python Programming: UNIT III: Control Flow, Functions, Strings : Tag: Engineering Python : Flow Chart, Syntax, Example Program | Python Programming - Conditional Statements