Example HTML page

if statement

In the previous section i have given only the introduction of conditional statements. Let us discuss them in detail.

If statement

When an if condition is true then the statements in if section are executed. Otherwise the statements which are outside of an if section will be executed.

The syntax of if statement is as follows:

if(condition):
     statements    

Ex 1 A sample example of if statement

bool=True
if bool==True:
    print("Welcome")
    print("To")
    print("VMS")

Output

Welcome

To

VMS

Ex 2 The above program can also be written as follows

bool=True
if bool:
    print("Welcome")
    print("To")
    print("VMS")

Output

Welcome

To

VMS

Ex 3 Another example of if statement is shown in below

bool=False
if bool==True:
    print("Welcome")
    print("To")
print("VMS")

Output

VMS

Ex 4 The above program can also be written as follows

bool=False
if bool:
    print("Welcome")
    print("To")
print("VMS")

Output

VMS

if statement examples without Boolean variables

Ex 1: An example program of if without Boolean variables

a=34
b="Hello"
c=3.45
if c==3.45 or y=="Hello":
    a=a+1
    b=b+"World"
print(a)
print(b)

Output

35

HelloWorld

Ex 2 Another example program of if without Boolean variables

x=23
y=25
z=27
if z>=x and z>=y:
    print("nothing is greater than z")
if not(x>=y or x>=z):
    print("x is lower")

Output

nothing is greater than z

x is lower

 

Example HTML page

Leave a Reply

Your email address will not be published. Required fields are marked *

Pin It on Pinterest