Useful Tips of If-Else Control Statement Using Python



Original Source Here

Useful Tips of If-Else Control Statement Using Python

Basic understanding of control loops with examples

Image Source

An IF statement makes simple decisions based on true or false conditions. This statement maintains the control flow of the statements. The concept of this statement is simple; if the condition is satisfied, it executes the true statements, or else the program stops. The below diagram represents this concept.

Syntax:

if condition:
statements

Program: (simple)

Age = 25if Age >= 18:
print(“Eligible to vote”)
Output:
Eligible to vote

Explanation: In the above program, we have defined the value of ‘Age’ as 25. The condition is if the ‘Age’ value is greater than or equal to 18, it will print the statement. If the condition is not satisfied, the program will not display any output.

IF-ELSE Statement

An IF-ELSE statement also makes simple decisions based on true or false conditions. The concept of this statement is simple; if the condition gets satisfied, it executes the true statements and if it doesn’t, the program prints the ‘else’ statements. The below diagram represents this concept.

Syntax:

if condition:
body of if
else:
body of else

Program: (simple)

Age = 15if Age >= 18:
print(“Eligible to vote”)
else:
print(“Not eligible to vote”)

Explanation: In the above program, we have defined the value of ‘Age’ as 34. The condition is if the ‘Age’ value is greater than or equal to 18, it will print the first statement. Since the ‘Age’ is 15, it does not satisfy the condition. Hence the second statement is printed.

IF-ELIF-ELSE Statement

An IF-ELIF-ELSE statement also makes simple decisions based on true or false conditions. The concept of this statement is simple; if the condition gets satisfied, it executes the statements, and if it doesn’t, the program checks for the second condition. If the second condition is also not true, the ‘else’ statements get printed.

Syntax:

if condition:
body of if
elif:
body if elif
else:
body of else

Program: (simple)

num = 15if num > 0:
print(“The number is positive”)
elif num == 0:
print(“The number is zero”)
else:
print(“The number is negative”)
Output:
The number is positive

Explanation: In the above program, we have defined the value of ‘num’ as 15. The condition is if the ‘num’ value is greater than 0, it will print the statement corresponding to the condition. Else, it would move to the second condition; to check whether the ‘num’ is equal to 0 and print its corresponding statement. If both the conditions are not satisfied, the last statement is printed. Here, the num is 15 (greater than 0). Hence the first statement is displayed as the output.

Nested IF

A Nested-If helps to make complex decisions based on the input. This is an advanced concept of IF-ELSE statements. The logic of execution is simple; if the condition is false, then statement 3 gets executed. If the first condition is true, it continues to execute the second test. And If this second condition is true, ‘statement1’ is displayed. If not, statement2 is displayed as the output.

Syntax:

if condition:
if condition:
statement1
else:
statements2
else:
statements3

Program: (simple)

a = 3
b = 4
c = 5
if a>b:
if a>c:
print(a, “is the greatest of all”)
else:
print(c, is the greatest of all”)
else:
if b>c:
print(b, “is the greatest of all”)
else:
print(c, “is the greatest of all”)
Output:
5 is the greatest of all

Explanation: The above program is to check which of the following numbers are the greatest. The 1st condition checks whether the value of ‘a’ is greater than ‘b’. Since it is false, it goes to the ‘else’ condition and checks the condition; b>c. Since the condition is false again, it prints the body of its else statement.

Let us look at different ways on how to execute if-else statements using data structures:

1. if-else statements using lists

Program:

thing = [“cap”,”bag”,”top”]for a in thing:
if a==”top”:
print(“It is present”)
else:
print(“It is not present”)
Output:
It is not present
It is not present
It is present

Explanation: Here, the program states to check whether the word “top” is present in the list. The true statement is printed if the condition is satisfied. Or else the false statement is displayed as the output.

2. if-else statements using dictionaries

Program:

item = 0
p = {1: “cp”, 2: “rt”, 3: “re”}
for item in p:
if item % 2 == 0:
print(p[item])
else:
print(“false”)
Output:
false
rt
false

Explanation: Here, the program states to print the item only if the key is even. Therefore, there is only one key-value pair in the dictionary that satisfies the condition.

Conclusion

Hey readers!!, we have covered most of the basics of control statements of “if-else”. We have also covered how to use data structures using all the control flow statements. I encourage you to explore more on this topic by executing programs on your own and hope you all had fun reading this article.

AI/ML

Trending AI/ML Article Identified & Digested via Granola by Ramsey Elbasheer; a Machine-Driven RSS Bot

%d bloggers like this: