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
if
statement checks ifx
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 anotherif
statement that checks ify
is greater than 0. If it is, it prints "y is also positive". Ify
is not greater than 0, it prints "y is not positive". Ifx
is not greater than 0, the program jumps to theelse
block outside the nestedif
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
Post a Comment