14:Decision – II (if)
Decision – II (if)
This is a set of statements that allow us to make decisions. It also creates a set of statements that will execute on the being True or False of a condition. We can create this condition on the base of some Boolean value and this Boolean value can be created by a simple Boolean variable, comparison operator or on the basis of logical operation. There are three statements that are part of it.
if Statement
- The “if” is the most widely used decision making statement.
- This statement checks a bool variable or expression that produces some Boolean result, that variable or expression produces True then all statements under this, will be executed.
if True: print ("This will always execute.")if False: print ("This will never execute.")
Code 1: Using if Statement
- We can see that there is True in front of if. It means that the condition is always true.
- Hence line 2 will be executed.
- Similarly, there is a False in line 3 which makes it impossible to run the statements under this (line 4).
- We can produce the True or False on the base comparison operators like this.
val1 = int (input ("Enter a value: "))condition = val1 > 0if condition: print ("value is positive")condition = val1 < 0if condition: print ("value is negative")condition = val1 == 0if condition: print ("value is zero")
Code 2: Producing Boolean Expression Comparison Operator
- In code 2, the user enters a number either positive, negative or zero.
- On line 2, we apply a comparison operator to check that the given number is more than 0.
- The Boolean result will be stored in condition.
- On line 3, we put the condition variable in the if statement. If the condition variable has True value, then statement on line 4 will be executed.
- Online 6, we again evaluate the value of condition by comparing the val1 with 0.
- If the number we entered is a negative then the condition will produce True and line 7 will execute.
- Finally, the condition variable will be calculated against equality of zero. If the user entered zero then this line 12 will be run.
- For efficiency of the code and less typing efforts, we can use the short-hand method and can put the expression directly in the if statement.
val1 = int (input ("Enter a value: "))if val1 > 0: print ("value is positive")if val1 < 0: print ("value is negative")if val1 == 0: print ("value is zero")
Code 3: Using Short-Hand Method with if Statement
- Using the short-hand method is more efficient and looks professional. We will short-hand all next codes.
- We can use logical operators (not, and, or) in expression because logical operators also produce Boolean results.
item_name = input ("Enter the name of the item: ")item_price = float (input ("Enter the price of the item: "))if item_name == "milk" and item_price > 2.00: sales_tax_amount = item_price * 0.05if item_name == "color": sales_tax_amount = item_price * 0.10net_price = item_price + sales_tax_amountprint ("The net price of the item is: {net_price}")
Code 4: Using Logical Operators in if Statement
- We have seen this kind of problem earlier (in lecture 10. Operators).
- This time we have certain conditions to apply.
- If the item is milk and its price is more than 2.00 then sales tax 5% will be applied on its final price.
- If the item name is color, then the sales tax will be 10%.
- We can multiple comparison and logical operators in a single if statement.
else Statement
- If you carefully analyze the if statement then you can see that if multiple if statements are in a program, then all the statements must examine and if more than one statements are True then statements underneath will execute.
- For instance, we write a program that finds the net price of an item with sales tax 10% but sales tax will not apply on milk because this is a necessary item for children.
- Pseudocode for above required algorithm would be like this
- Input the name of the item.
- Input the price of item
- If item name is milk, then percentage of sales tax is 0%
- If any other item, then it will apply 10% sales tax
- We can easily define the if condition for milk
if item_name == "milk": sales_tax = 0.00
Code 5: if Condition for Milk
- How the condition for all other items can be defined because there might be thousands of items and writing conditions for every item is not possible.
- Here we can use another statement.
- This else statement does not ask for any condition. If the if statement is producing False then it automatically executes. If the if statement produces True then it will not execute.
if item_name == "milk": sales_tax = 0.00else sales_tax = 0.10
Code 6: Using else Statement for all other Items
- The else statement cannot be applied without the if statement. There must be an if statement before else.
- We can add more in the if-else block under else statement if required.
elif Statement
- In many situations, it is required to apply the condition in the else statement too.
- For example, we want to apply 0% sales tax on milk, 1% on eggs and bread, 5% and 10% on hair color.
- Our solution would be like this.
item_name = input ("Enter the name of the item: ")item_price = float (input ("Enter the price of the item: "))if item_name == "milk": sales_tax_amount = item_price * 0.00 net_price = item_price + sales_tax_amountelif item_name == "egg" or item_name == "bread": sales_tax_amount = item_price * 0.10 net_price = item_price + sales_tax_amountelif item_name == "hair color": sales_tax_amount = item_price * 0.10 net_price = item_price + sales_tax_amountprint (f"The net price of the item is: {net_price}")
Code 7: Using elif Statement
- If required then you can add an else statement but it will add at the end of last elif.
- In many situations, match-case and elif seem identical but there are many technical differences.
Example 1: Write a program for a student grading. If student marks less than 50 then grade earned is F, if marks between 50 and 59 then grade will be D, if marks between 60 and 69 then grade will be C, if marks between 70 and 79 then grade will be B but if marks between 70 and 74 then add – (minus) and if marks between 75 and 79 then add + (plus) with B grade. Similarly, if you are more than 80 then the grade will be A and marks are more than 90 then it will be A+.
Solution:
- For better understanding we can write the problem like this
- Student marks will be entered in an integer variable.
- Following conditions can be applied
- Marks < 50 => Grade = F
- Marks ≥ 50 and < 60 => Grade = D
- Marks ≥ 60 and < 70 => Grade = C
- Marks ≥ 70 and < 80 => Grade = B (add – if for 70 to 74 and add + for 75 to 79)
- Marks ≥ 80 and < 90 => Grade A
- Marks ≥ 90 and <= 100 => Grade A+
marks = int (input ("Enter the marks obtained: "))if marks < 50: print ("Grade: F")elif marks < 60: print ("Grade: D")elif marks < 70: print ("Grade: C")elif marks < 80: if marks < 75: print ("Grade: B-") else: print ("Grade: B+")elif marks < 90: print ("Grade: A")elif marks <= 100: print ("Grade: A+")else: print ("Invalid marks entered. ")
Code 8: Using if, elif , else and Nested If Statements
- You can see that there are multiple elif statements.
- There is also an example of nested if with else is there.
- When condition on line 8 will True then code block will execute.
- In the code block, it again encounters another if statement.
- If this statement (line 9) is True, the line 10 will execute and if the condition is False, the line 11 will execute.








