1 / 23

ANALYSIS AND ALGORITHM DESIGN - III

ANALYSIS AND ALGORITHM DESIGN - III. The IF-THEN construct.

cranford
Download Presentation

ANALYSIS AND ALGORITHM DESIGN - III

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. ANALYSIS AND ALGORITHM DESIGN - III

  2. 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

  3. 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.

  4. 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

  5. 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.

  6. 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

  7. 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.

  8. 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.

  9. 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

  10. 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

  11. 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

  12. 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.

  13. 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

  14. 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.

  15. 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.

  16. 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

  17. 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.

  18. 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

  19. 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>

  20. 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.

  21. 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.

  22. 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%

  23. Nested selections Solution 6 (cont’d) ELSE bonus = inc_gen * 3% ENDIF ENDIF ENDIF PRINT “Bonus = ”, bonus

More Related