In real life, there are situations where we have to make decisions for a particular circumstance and based on those decisions, and we plan our next move. The same thing happens while writing a code. Whenever we write a code in any programming language, some situations or conditions will arise where we have to decide, and that decision greatly impacts the program’s output. Decision-making is a very important part of our life and is equally important in a programming language. Decision-making helps to write an efficient code. In programming languages, decision-making helps in deciding the flow of the code.
In any programming language, if-else keywords are used for decision-making. If-else statements are used for deciding the flow of the code.
If statements are the basic block of decision-making. Now any code written inside an if statement will only run if the condition or the test expression written along with it will be true. All if statement contains a condition along with it and only when that condition will be true then if statement will run.
The code will not get executed if the condition or the test expression is false.
The code inside the if-statement will be identified based on indentation.
# This is the basic syntax for the if statement if condition: statement 1 statement 2
if 4 < 5 : print(“4 is smaller than 5”)
4 is smaller than 5
In the above code, if statements are written using the if-keyword, we have a condition: if 4 is less than 5 or not. Since the condition is true, only then we would be able to enter inside the conditional statement.
if 4 < 5: print(“4 is less than 5”)
print(“4 is less than 5”) ^ IndentationError
In the above code, we can see code will throw an error while executing. This error occurred because the indentation is wrong in the given code. In python, it is very important to follow the indentation; otherwise, it will throw an indentation error. In python, we use four whitespaces to provide indentation. But we can also give at least one whitespace to provide indentation.
We can also use any numeric value instead of a condition in a python statement. Any non-zero numeric value is treated as True in python, whereas zero and None are treated as False.
if 5: print(“Welcome to”) print(“Tutorials And Examples ”)
Welcome to Tutorials And Example
In the above code, instead of a condition, we have used a non-zero value considered true. Hence we were able to print the statement written inside the if-block.
If 0: print(“Welcome to”) print(“Tutorials And Examples ”)
Tutorials And Example
In the above code, we have used zero instead of a condition, which is considered false. Hence the code inside the if statement won’t get executed.
The else statement is always used with an if-statement. It is like another roadway for the codes. Else statement will always get executed if no other statement or condition gets executed. Else statement will only work when the if-statement condition is false.
Let’s understand with an example.
# This code will explain the difference between the if-else statement x = 20 if x < 10: print (“Condition True”) else: print (“Condition False”)
In the above code, the x is assigned a value of 20. The if-statement condition is if x is less than 10, which is false. Hence code inside the if-block won’t get executed. Then the control will go to the else statement and execute whatever is written inside it.
Along with the if-else statement, we also have the elif-statement, which implies ‘if the previous condition is false, then try this condition’. A program can have any number of elif-statement.
Let’s understand with an example.
# This example is for elif-statement x = 20 y = 20 if x > y: print(“x is greater than y”) elif x == y: print(“x is equal to y”)
X is equal to y
In the above example, the condition in the if-statement is that ‘x is greater than y’, which is false; hence the code written inside it did not get executed. After that, the machine checks the condition inside the elif-statement: ‘ x is equal to y’, and that is true. So the print statement written inside the elif-statement gets printed.
Let’s us understand the concept with an example.
#This is an overall example including all three statement.
#This is an overall example including all three statement. i = 100 if (i == 10): print("i is 10") elif (i == 50): print("i is 15") elif (i == 100): print("i is 20") else: print("i is not present")
i is 100
In the above code, we have assigned i a value of 100. After that, there is an if-statement with the condition ‘ i is equal to 10 ’, which is false. After that, there is an elif-statement with the condition ‘i is equal to 50’, which is also false. After that, there is another elif-statement with the condition ‘i is equal to 100’, which is true. Hence, the control goes inside that elif block and prints the value written inside it.