Control statements are used to control the flow of execution depending upon the specified condition/logic.
An if statement is a programming conditional statement that, if the condition is true, it executes the block of code inside of it.
books = 2 if (books == 2) : print(" You have") print("Two books") print("Outside of if statement")
In the above code, we are checking if books is equal to 2, then execute the given code.
The if-else statement executes some code if the test expression is true (non-zero) and some other code if the test expression is false.
a=10 if a<100: print('less than 100') else: print('more than equal 100')
In the above code, we are checking if a is less than 100, else will print "more than equal 100'.
Nested If-Else Statement
The nested if ... else statement allows you to check for multiple test expressions and executes different codes for more than two conditions.
num = float(input("enter a number:")) if num >=0: if num==0: print("zero") else: print("Negative number")
In the above code, we are first checking if num is greater than or equal to 0 and then if it is zero if both the conditions are met, "zero" is printed, otherwise "Negative number".