240 likes | 336 Views
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.
E N D
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 SINGLE LINE IF IF cond true THEN action ELSE action BLOCK IF IF conditon true THEN action action ELSE action action END IF
Nested IF THEN ELSE IF (true) THEN IF (true) THEN IF (true) THEN ELSE END IF ELSE ENDIF ELSE END IF
Schneider p 159 # 32 • Write a program that request three scores as input and displays the average of the two highest • flowchart
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
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
CASE “s” INPUT side area = side ^ 2 CASE “r” INPUT length, width area = length * width CASE ELSE PRINT “error” END SELECT PRINT area
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
test expression • three forms • variable e.g. x, name$, number# • constant e.g. 32 • expression e.g. 9*C/5+32, SQR(x)
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
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
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
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
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
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
Block form • IF condition true THEN action action END IF • if conditions false control goes to line following END IF
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
Logical Operators • allow combinations of conditions • AND, OR, NOT • IF j = 7 AND e > 5 OR x <>0 THEN ...
Operator Hierarchy • arithmetic operations • relational evaluations • logical operators in order: • NOT • AND • OR • brackets can be used for • readability and to change evaluation order