Decision making. If Then Else.. Endif (dual alternative) If..Then.. Endif

Decision making • Make decisions based on certain conditions • Set of alternative paths dependent on conditions • IF…THEN…END IF – Relational Operator...
Author: Margery Lamb
6 downloads 2 Views 36KB Size
Decision making • Make decisions based on certain conditions • Set of alternative paths dependent on conditions • IF…THEN…END IF – Relational Operators • > , < etc..

– Boolean operators • IF combined with AND, OR, NOT

• SELECT CASE – Select an alternative based on certain set of conditions Dr. Hugh Melvin, Dept. of IT, NUI,G

If…..Then.. Endif • If a certain condition is true, a block of code is executed, otherwise it is skipped If condition Then . . Statement block . Endif Next statement

Start

YES

Condition true?

Statement block

next statement

End Dr. Hugh Melvin, Dept. of IT, NUI,G

If…Then …Else.. Endif (dual alternative) • If a certain condition is true, one block of code is executed, otherwise an alternative block is executed If .condition Then Start .. Statement block 1

Statement block 1

Else

YES

Condition true?

NO

Statement block 2

Statement block 2

. next statement Endif Next statement End • Can also have multiple alternatives (several Else clauses) – may be better to use CASE Dr. Hugh Melvin, Dept. of IT, NUI,G

1

Examples IF..THEN..ENDIF IF Mark >= 40 THEN Msgbox( “Congratulations !!”)

‘statement block ‘executed only if TRUE

ENDIF Msgbox( “Goodbye”) ‘output in all cases IF..THEN..ELSE..ENDIF IF Mark>= 40 THEN Msgbox( “Congratulations !!”) ‘executed if condition TRUE ELSE Msgbox( “Tough Luck.. Enjoy the year? “) ‘statement block executed if FALSE ENDIF Msgbox( “Goodbye”) Dr. Hugh Melvin, Dept. of IT, NUI,G

ELSEIF clause • Check for several conditions IF Mark >=70 THEN Msgbox(“Excellent mark…Grade 1”) ELSEIF Mark >=50 THEN Msgbox(“Very Good..Grade 2”) ELSEIF Mark >=40 THEN Msgbox( “At least you passed !”) ELSE Msgbox(“Sorry…you failed”) ENDIF Msgbox( “Goodbye”) NOTE: Use indentation Dr. Hugh Melvin, Dept. of IT, NUI,G

Nested IF statements • IF statements within one another • Statement block consists of IF..THEN..ENDIF • You must match IF.ELSE…ENDIF IF PIN = 0456 THEN Amt = Val (Inputbox(“Enter amount you require?”)) IF Balance >= Amt THEN Msgbox(”Cash on its way“) ELSE Msgbox( ”Amount Exceeds Balance.. bye“) END IF ELSE Msgbox( “PIN number is incorrect.. bye”) ENDIF Dr. Hugh Melvin, Dept. of IT, NUI,G

2

Relational operators • Relational operators – equal to … = not equal to … – less than … < less than or equal to… greater than or equal to .. >= • Relational operators can also be used with strings – "Chase" is < "Chaz" ..i.e. alphabetical – "Pat" is < "Patricia" ..i.e. second string is longer – "Pat" is < "Pat " ..spaces make second string longer

Dr. Hugh Melvin, Dept. of IT, NUI,G

Relational Operators • String comparisons based on how computer represents characters – ASCII system (American Standard Code for Information Interchange) • Uppercase lower than lowercase A < a (65 < 97) – a to z (97 to 122) – A to Z (65 to 90) – “ A” < “A” ‘space < A

Dr. Hugh Melvin, Dept. of IT, NUI,G

Dr. Hugh Melvin, Dept. of IT, NUI,G

3

Option Explicit Private Sub cmdOk_Click() Dim mark As Single mark = Val(txtMark.Text) Rem Decision structure If mark < 40 Then txtMark.Text = "Sorry you failed" ElseIf mark < 55 Then txtMark.Text = "Well done, you passed" ElseIf mark < 70 Then txtMark.Text = "Very Well Done.. 2nd Class" Else txtMark.Text = "Excellent.. 1st Class Honours" End If End Sub Private Sub cmdClear_Click() Rem Clear text box txtMark.Text = ""Dr. Hugh Melvin, Dept. of IT, NUI,G End Sub

Logical (Boolean) operators • AND, OR, NOT – NOT negates the logical value of the expression it precedes – AND combines two expressions and produces a value of true only when both of these conditions are true IF PIN=0456 AND CREDITS>0 THEN Msgbox( ”Welcome to Vodafone Online“) ELSE .. ENDIF – OR combines two expressions and produces a value of true when either or both of the conditions are true IF BALANCE >=1000 OR NoOfYears>=3 THEN Msgbox(“ You are entitled to a loan”) ELSE .. ENDIF Dr. Hugh Melvin, Dept. of IT, NUI,G

Logical Operators • Implement the following policy for alerting pilot that he/she needs to do something.... • Engine Fault should arise if ANY of the following 3 conditions occur – Engine Speed > 20000 rpm – Oil Temperature > 70 C – Exhaust Temperature > 500 C

Dr. Hugh Melvin, Dept. of IT, NUI,G

4

Logical Operators • More Refined: Fault should arise if ANY of the following 3 occur – Engine Speed > 20000 rpm and Vibration level HIGH – Oil Temperature > 70 C – Exhaust Temperature > 500 C

Dr. Hugh Melvin, Dept. of IT, NUI,G

Summary of Operators • Arithmetic – ^,*,/,+,• Relational – =,, , = • Logical – NOT, AND, OR – Hierarchy NOT – AND – OR

Dr. Hugh Melvin, Dept. of IT, NUI,G

Hierarchy of operators..revised 1.

Anything in parentheses

2. 3.

Exponentiation (^) Unary plus or minus sign (a sign used alone in front of a number) Multiplication and division (*,/) Addition and subtraction(+,-) Relational operators (=,,,=) NOT AND OR

4. 5. 6. 7. 8. 9.

Dr. Hugh Melvin, Dept. of IT, NUI,G

5

Logical Operators IF (name$=“guest”) OR (Age > 18) AND (NOT(status$ = “student”)) THEN Msgbox( “Enjoy your evening”) ELSE Msgbox(“Sorry but go home”) ENDIF Eg. status$ = “student”, Age = 21, name$ = “Maoldearg Óg” (name$=“guest”)OR(Age > 18)AND(NOT(status$ = “student”))

Steps • FALSE OR TRUE AND (NOT(TRUE)) – NOT(TRUE) = FALSE

• FALSE OR TRUE AND FALSE – FALSE OR FALSE

• FALSE …”Sorry but go home”…. Dr. Hugh Melvin, Dept. of IT, NUI,G

SELECT CASE…..END SELECT • SELECT CASE allows an action to be selected from a list of alternatives SELECT CASE test-expression CASE valuelist1 action1 CASE valuelist2 action2 . . .

CASE ELSE action of last resort END SELECT

• The test-expression can be a variable or expression • valuelist 1 to n.. a list of values for which the corresponding action is to occur. • Action is a statement block Dr. Hugh Melvin, Dept. of IT, NUI,G

SELECT CASE….END SELECT •

CASE ELSE is optional but useful



Each valuelist can contain one or more of the following types:

» relational operator preceded by IS and followed by a constant, variable or expression » Case Is > 50

» a range expressed in the form a TO b » where a and b are either constants, variables or expressions » Case 3 To 50

» Value or list of values (separated by comma) » Case 1,2 Dr. Hugh Melvin, Dept. of IT, NUI,G

6

SELECT CASE …..END SELECT Form

SELECT CASE test expression CASE list1 block1

CASE list 2 block2

CASE ELSE blockn

END SELECT

Action Example

test expression is evaluated and if the value is found in one of the CASE lists, the corresponding block of code is executed. Otherwise CASE ELSE block is executed. Cf h/o

Dr. Hugh Melvin, Dept. of IT, NUI,G

Dr. Hugh Melvin, Dept. of IT, NUI,G

Option Explicit Private Sub cmdClear_Click() txtMark.Text = "" End Sub Private Sub cmdOk_Click() Dim mark As Double mark = Val(txtMark.Text) Select Case mark Case Is < 40 txtMark.Text = "Sorry, you failed" Case 40 To 100 txtMark.Text = "Well done.. you passed" Case Is > 100 txtMark.Text = "Incorrect..must be 0-100" End Select Dr. Hugh Melvin, Dept. of IT, NUI,G End Sub

7

Suggest Documents