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
if statements:In this example, the outer
ifstatement checks ifxis 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 anotherifstatement that checks ifyis greater than 0. If it is, it prints "y is also positive". Ifyis not greater than 0, it prints "y is not positive". Ifxis not greater than 0, the program jumps to theelseblock outside the nestedifstatements and prints "x is not positive".You can have multiple levels of nested
ifstatements to handle more complex conditions and execute specific code blocks based on those conditions.

Comments
Post a Comment