What are if conditionals in python? if | if-else | if-elif

Generally in a program, the statements executed sequentially, selectively, and iteratively.

A statement when executed, on the basis of the result of a condition given in the program code known as the conditional statement.

A statement when executed repeatedly, until the end of the given condition satisfied called a loop statement.

let us see briefly, what are conditional statements? what do they do?

firstly In python, there are different forms of if statement they are

  • if
  • if-else
  • if-elif

If condition

An if statement checks for a particular condition and

if the condition results to be true,

then a bunch of statements or set-of-statements executed.

If the condition is false, it won’t do anything

Since it’s a simple if conditional statement

Syntax

if <conditional expression>:
    statement
    [statements]

Explanation

If keyword should be given followed by the condition and ending with a colon :

Firstly the if condition will check for the condition given to it and if the given condition satisfied then it will execute the statements given inside

EXAMPLE

a=90
b=45
if a>b:
    print('a is greater than b')
    print('difference between them is ',(a-b))

Output

a is greater than b
difference between them is  45

Explanation

Firstly we are checking if the condition is true or not

yes a is greater than b so it will come inside the loop and starts executing the statements given

in this example, we have given the statements to print which is greater either a or b So it printed a

and print the difference between them So it printed 90-45= 45 as output

If-else condition

An if-else statement checks for a particular condition

if the condition results to be true,

then a bunch of statements or set-of-statements are executed.

If the condition is false, it will be carried to the statements given below the else

Syntax

if (conditional expression>:
    statement
    (statements]
else:
    statement
    [statements]

Explanation

If keyword should be given followed by the condition and ending with a colon :

Firstly the if condition will check the given condition satisfied then it will execute the statements given inside

if the condition fails it will be carried to the statements given below else

EXAMPLE

a=5
if a >0:
    print(a, "is a positive number")
else:
    print(a, "is a negative number")

Output

5 is a positive number

Explanation

Firstly we are checking if the condition is true or not

yes a is greater than 0 so it will come inside the loop and starts executing the statements given

in this example, we have given the statements to print when a is greater than 0 it is positive. So it printed a

and if not the statements given below the else part will be executed here we’ve given print a is a negative number So it will print in case we provided a negative value

We have seen

  1. simple if
  2. if-else

There is also one more type of if conditional statement called if-elif and if-elif-else

Can we give a condition inside another condition?

yes we can give a condition inside another condition

this method is known as NESTED IF

in this method we will give a if condition inside the another condition

We will be happy to hear your thoughts

Leave a reply

Edusera
Logo
Open chat
1
Scan the code
Hello!๐Ÿ‘‹
Can we help you?