ELSE IF CONDITION

ELSE IF CONDITION

In Python, the else if statement is represented as elif. The elif statement allows you to check for multiple conditions after an initial if statement. The general syntax is as follows:

syntax:

 if condition1: # Code to execute if condition1 is True elif condition2: # Code to execute if condition1 is False and condition2 is True elif condition3: # Code to execute if condition1 and condition2 are False, and condition3 is True else: # Code to execute if all conditions are False

FLOW CHART:



EXAMPLE CODE:

x = 10 if x > 0: print("x is positive") elif x < 0: print("x is negative") else: print("x is zero")

In this example, if x > 0, it will print "x is positive." If x < 0, it will print "x is negative." If neither condition is satisfied, it will print "x is zero" because of the else statement.

You can have multiple elif statements to check for additional conditions in the order you specify. The code will execute the block associated with the first condition that evaluates to True. If none of the conditions are True, the code within the else block will be executed.





Comments

Popular posts from this blog

Fibonacci Series

COMPARISION EXPRESSION

LOGICAL EXPRESSION