1 / 23

E0001 Computers in Engineering

E0001 Computers in Engineering. SELECT CASE. Readings. Schneider - Chapter 5 About this lecture SELECT CASE IF THEN ELSE revision. IF ... THEN. used to choose between TWO alternative paths has two general forms single line block. IF THEN ELSE.

Download Presentation

E0001 Computers in Engineering

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. E0001 Computers in Engineering SELECT CASE

  2. Readings • Schneider - Chapter 5 About this lecture • SELECT CASE • IF THEN ELSE revision

  3. IF ... THEN • used to choose between TWO alternative paths • has two general forms • single line • block

  4. IF THEN ELSE SINGLE LINE IF IF cond true THEN action ELSE action BLOCK IF IF conditon true THEN action action ELSE action action END IF

  5. Nested IF THEN ELSE IF (true) THEN IF (true) THEN IF (true) THEN ELSE END IF ELSE ENDIF ELSE END IF

  6. Schneider p 159 # 32 • Write a program that request three scores as input and displays the average of the two highest • flowchart

  7. Problem • Write a program which calculates the area of a circle, square or a rectangle • OUTPUTS - area of shape • INPUTS - user choice of • shape • dimensions of shape • PROCESS - formula for area of shapes

  8. Flowchart

  9. Program PRINT “area of circle input c” PRINT “area of square input s” PRINT “area of rectangle input r” INPUT choice$ SELECT CASE choice$ CASE “c” INPUT radius area = radius ^ 2*3.14

  10. CASE “s” INPUT side area = side ^ 2 CASE “r” INPUT length, width area = length * width CASE ELSE PRINT “error” END SELECT PRINT area

  11. Select Case SELECT CASE testexpression CASE matchexpression1 actions.... CASE matchexpression2 actions... CASE ELSE actions... END SELECT • the textexpression is compared with match1, then match2, etc until a match is found. When match is found corresponding actions are executed. Then jumps to END SELECT

  12. test expression • three forms • variable e.g. x, name$, number# • constant e.g. 32 • expression e.g. 9*C/5+32, SQR(x)

  13. match expression • three forms • list a value, a series of values, or an expression • 2 • 2, 4, 6 • 3, A, B^2 • IS and a relational operator • IS > 50 • IS <= A • TO and a range of values • 10 TO 20 • x TO y

  14. Select Case • used when more than two paths are required • text expression may be numeric or string • test expression is compared to each case test in turn • when test is true actions are executed • then branches to statement after END SELECT • no test match - executes CASE ELSE actions

  15. can be nested, or used in conjunction with any other syntax SELECT CASE x CASE 1 SELECT CASE Y$ CASE “no”: ... CASE ELSE END SELECT CASE 2 etc

  16. Plan & write a program that displays warning messages based on the following pollution level guide lines. The program should accept user input for the pollution level pollution level (ppm) warning to be displayed 0 no pollution - completely safe1 or 2 little pollution - generally safe3, 4, 5 moderate pollution- be cautious>5 significant pollution - dangerousany other no. incorrect input - re run program

  17. Thats all folks!

  18. Program INPUT “enter the number of discs ”; num IF num < 25 THEN cost = num * 1 ELSE cost = num * 0.7 ENDIF PRINT “cost of order is”; cost

  19. Single Line form IF condition is true THEN do one action If the condition is not true control moves to the following line in the program Condition is an expression using relational and/or logical operators

  20. Block form • IF condition true THEN action action END IF • if conditions false control goes to line following END IF

  21. Expression using <, less than >, greater than <=, less than or equal to >=, greater than or equal to =, equal to <>, does not equal Comparison can include NUMBERS STRINGS, based on ASCII order EVALUATES to TRUE or FALSE Relational Operators

  22. Logical Operators • allow combinations of conditions • AND, OR, NOT • IF j = 7 AND e > 5 OR x <>0 THEN ...

  23. Operator Hierarchy • arithmetic operations • relational evaluations • logical operators in order: • NOT • AND • OR • brackets can be used for • readability and to change evaluation order

More Related