230 likes | 247 Views
Learn about IF-THEN and IF-THEN-ELSE constructs in programming, with syntax, examples, and solutions. Explore boolean operators for decision-making in coding.
E N D
The IF-THEN construct The IF-THEN construct contains a condition which is tested before an action is undertaken. If the condition holds true, then the action is taken. Otherwise, the instruction statements between IF-THEN and ENDIF are not taken, but are ignored. Syntax: IF <condition> THEN <Action to be taken if condition is true> (that is, instruction statements that would be performed if the conditions are met) ENDIF
The IF-THEN construct Example 1 A company gives out bonuses based on the amount of income generated by their sales representatives per month. Once the income is greater than $5000.00, a bonus of 10% of the generated income is given to the employees. Read the income generated and print the bonus.
The IF-THEN construct Solution 1: Case 2 In this case, the PRINT statement is placed outside the IF-THEN-ENDIF construct. So the bonus is printed regardless of whether the bonus is actually calculated or not. PRINT “Enter the income generated” READ inc_gen IF inc_gen > $5000.00 THEN bonus = inc_gen * 10% ENDIF PRINT “Bonus = ”, bonus
The IF-THEN construct Solution 1 The decision to give a bonus is based on the amount of income the sales representative generates. Input/read the Income Generated. The action to be taken is the calculation and printing of the bonus. Only if the Income Generated is greater than $5000.00 is the bonus calculated.
The IF-THEN construct Solution 1: Case 1 We must be aware of where the PRINT statement is placed. If it is placed within the IF-THEN-ENDIF construct, only if the income generated is greater than $5000.00 is the bonus printed. PRINT “Enter the income generated” READ inc_gen IF inc_gen > $5000.00 THEN bonus = inc_gen * 10% PRINT “Bonus = ”, bonus ENDIF
The IF-THEN construct Example 2 A car rental firm leases its cars for $250.00 per day. The manager gives a discount based on the number of days that the car is rented. If the rental period is greater than or equal to 7 days, then a 25% discount is given. Read the rental period and print the discount given.
The IF-THEN construct Solution 2 Input the rental period. The decision is based on the length of time the car is rented. The action is to calculate the discount to be given for the rental period. Only if the car is rented for 7 days or more is the discount calculated.
The IF-THEN construct Solution 2 PRINT “Enter car rental period” READ rental_period IF rental_period >= 7 THEN discount = ($250.00 * rental_period) * 25% ENDIF PRINT “Discount = ”, discount
The IF-THEN-ELSE construct The IF-THEN-ELSE construct contains two parts: the THEN part and and ELSE part. The condition is tested before an action can be undertaken. If the condition holds TRUE, the THEN action is taken, otherwise the ELSE action is taken if the condition is FALSE
The IF-THEN-ELSE construct Indentation is used for readability, so that you can see at a glance the structure of the construct - especially which statements belong to the THEN part, and which belong to the ELSE part. Syntax: IF<Condition> THEN <THEN part: Action to be taken if condition is TRUE> ELSE <ELSE part: Action to be taken if condition is FALSE> ENDIF
The IF-THEN-ELSE construct Example 3 A company gives out bonuses based on the amount of income generated by their sales representatives per month. Once the income is greater than $5000.00 then a bonus of 10% of the generated income is payable; otherwise the bonus is 3% of the generated income. Read the income generated and print the bonus.
The IF-THEN-ELSE construct Solution 3 PRINT “Enter income generated” READ inc_gen IF inc_gen > $5000.00 THEN bonus = inc_gen * 10% ELSE bonus = inc_gen * 3% ENDIF Print “Bonus = ”, bonus
Boolean Operators • When selection is based upon one or more expressions/decisions being TRUE/FALSE. It is possible to combine the expressions/decisions together using the Boolean operators AND or OR. • IF the AND operator is used both conditions must be met, in order for the entire expression to be true or false. • IF the OR operator is used, either condition must be met, in order for the total expression to be true or false.
Boolean Operators Example 4 A club plays cricket only on Sundays, and only if it is not raining. Read the day and the weather and print ‘Game on’ if it is a suitable day for playing.
Boolean Operators Solution 4 – With the AND operator Both conditions must be met for the expression to be true and ‘Game on’ is printed. If either condition is not met, such as the day is not ‘Sunday’ or the weather is ‘Rain’, then the action (printing ‘Game on’) is not taken. PRINT “Enter the day and the weather” READ day, weather IF day = “Sunday” AND weather = “No Rain” THEN PRINT “Game on” ENDIF
Boolean Operators Example 5 A company gives out bonuses based on the amount of income generated by their sales representatives per month. Once the income is greater than $5000.00 and less than or equal to $8000.00 then a bonus of 10% of the generated income is given to employee. Read the generated income and print the bonus.
Boolean Operators Solution 5 PRINT “Enter income generated” READ inc_gen IF (inc_gen >= $5000.00) AND (inc_gen<=$8000.00) THEN bonus = inc_gen*10% ENDIF PRINT “Bonus = ”, bonus
Nested selections IF statements embedded one within another are said to be nested. For every IF-THEN statement there must be an ENDIF. Syntax: IF<Condition 1> THEN <Action to be taken if Condition 1 is true> ELSE IF <Condition 2> THEN <Action to be taken if Condition 2 is true> ELSE IF <Condition 3> THEN <Action to be taken if Condition 3 is true>
Nested selections Syntax (cont’d): ELSE <Action to be taken if Conditions 1-3 are false> ENDIF ENDIF ENDIF If the first condition is not met, the second condition is checked, if the second is not met, then the third condition is checked, and so on.
Nested selections Example 6 A company gives out bonuses based on the amount of income generated by their sales representatives per month. Once the income is greater than or equal to $10,000.00, a bonus of 20% is given. If the income generated is greater than or equal to $8000.00 but less than $10,000.00, a bonus of 15% is given. If the income generated is greater than or equal to $5000.00 but less than $8,000.00, a bonus of 10% is given. If the income generated is less than $5000.00, a bonus of 3% is given. Read the generated income and print the bonus.
Nested selections Solution 6 PRINT “Enter income generated” READ inc_gen IF (inc_gen >= $10,000.00) THEN bonus = inc_gen * 20% ELSE IF (inc_gen >= $8,000.00) AND (inc_gen < $10,000.00) THEN bonus = inc_gen * 15% ELSE IF (inc_gen >= $5,000.00) AND (inc_gen < $8,000.00) THEN bonus = inc_gen * 10%
Nested selections Solution 6 (cont’d) ELSE bonus = inc_gen * 3% ENDIF ENDIF ENDIF PRINT “Bonus = ”, bonus