PYTHON Theory
If...Else Statement
If condition returns true then the statements inside the body of “if” are executed and the statements inside body of “else” are skipped.
If condition returns false then the statements inside the body of “if” are skipped and the statements inside body of “else” are executed.
If Else Statement
If Else Statement
The if-else statement is also used to test the conditions.If the condition is true, then if block is executed.If the condition is false, then the else block is executed.
Syntax of If Else Statement |
---|
if(condition): { #Code to be executed if condition is true } else: { #Code to be executed if condition is flase } |
Nested if else statement
Nested if else statement
The nested if statement means when there is an if statement inside another if statement When an if.. else statement is present inside the body of another “if” or “else” then this is called nested if else.
Syntax of Nested if...else statement |
---|
if (condition1) { #Statement(s) if (condition2): { #Statement(s) } else: { #Statement(s) } } else: { #Statements(s) if (condition3): { #Statement(s) } else: { #Statement(s) } } |
Else - If Statement
Else - If Statement
The else..if statement is useful when you need to check multiple conditions within the program, nesting of if-else blocks can be avoided using else..if statement. If any one of the if condition is true, the statement related with that if is executed, and the remainder of the ladder is avoided. If none of the condition is true, then the final else statement will be executed.
Syntax of else if statement |
---|
if (condition 1): { #Code to be executed if condition 1 is true } elif (condition 2): { #Code to be executed if condition 2 is true } else: { #Code to be executed if all the conditions return false } |