NESTED IF

NESTED IF CONDITION

In Python, you can use nested if statements to create more complex conditional structures. A nested if statement is an if statement within another if statement. This allows you to check for multiple conditions and execute different blocks of code based on those conditions.

SYNTAX:

if condition1:

    # code block to be executed if condition1 is True

    if condition2:

        # code block to be executed if both condition1 and condition2 are True

    else:

        # code block to be executed if condition1 is True and condition2 is False

else:

    # code block to be executed if condition1 is False



Here's an example to illustrate the usage of nested if statements:

EXAMPLE:

x = 10 y = 5 if x > 0: print("x is positive") if y > 0: print("y is also positive") else: print("y is not positive") else: print("x is not positive")

In this example, the outer if statement checks if x is greater than 0. If it is, the code block indented below it is executed, which prints "x is positive". Then, within that block, there's another if statement that checks if y is greater than 0. If it is, it prints "y is also positive". If y is not greater than 0, it prints "y is not positive". If x is not greater than 0, the program jumps to the else block outside the nested if statements and prints "x is not positive".

You can have multiple levels of nested if statements to handle more complex conditions and execute specific code blocks based on those conditions.

Comments

Popular posts from this blog

Fibonacci Series

COMPARISION EXPRESSION

LOGICAL EXPRESSION