Example HTML page

# if else statement

if else statement

When an if condition is true then the statements in if section are executed. When the condition is evaluated to false the statements in the else section will be executed

The syntax of if else statement is as follows

if(condition):
statements
else:
statements

Some of the example programs on if else statement are shown in below:

Ex 1: A sample program on if else statement.

if x>=25:
print("Pass")
else:
print("Fail")

Output

Ex 2: A program on finding climate condition.

t=float(input("Temperature : "))
if t>30:
print("Normal")
else:
print("Too hot")

Output

 Temperature : 43 Too hot

Ex 3 Another example program on if else statement.

str=input("Who is this : ")
if str=="Sudhakar":
print("Oh!Hi",str)
else:
print("ok")

Output

 Who is this : Sudhakar Oh!Hi Sudhakar

Ex 4 A program on if else statement using while loop

i=1
while(i<=10):
if(i==6):
print("Hello")
pass
else:
print(i)
i=i+1

Output

 1 2 3 4 5 Hello 7 8 9 10

Ex 5: A program on evaluation of marks

marks=int(input("enter marks"))
if marks<35:
marks+15
print("total marks",marks+15)
else:
marks+10
print("total marks",marks+10)

Output

 enter marks 21                                             #Test case 1 total marks 36   enter marks 76                                             #Test case 2 total marks 86

Ex 6 A program to calculate the discount and print net payable

amount=int(input("Enter amount: "))
if amount<1000:
discount=amount*0.05
print ("Discount",discount)
else:
discount=amount*0.10
print ("Discount",discount)
print ("Net payable:",amount-discount)

Output

 Enter amount: 4589 Discount 458.90000000000003 Net payable: 4130.1

Ex 7: A program to find whether the given sides form a triangle or not.

x=float(input("x= "))
y=float(input("y= "))
z=float(input("z= "))
if x+y>z and x+z>y and x+z>x:
print("Yes it forms a triangle")
else:
print("impossible to form a triangle")

Output

 x= 45                                                              #Test case 1 y= 31 z= 20 Yes it forms a triangle   x= 24                                                               #Test case 2 y= 25 z= 56 impossible to form a triangle

Example HTML page