Download presentation
Presentation is loading. Please wait.
1
Conditional or Decision Logic
INT213 Conditional or Decision Logic
2
Simple IF statement IF true THEN run_this
true is a resolved logical expression run_this is a single command IF mark >= 55 THEN grade = "PASS" When the logical expression resolves to false, THEN is not done.
3
Simple IF statement IF true THEN run_this run_that END IF
runs multiple commands easily readable even for single commands IF mark >= 55 THEN grade = "PASS" semester = semster + 1 END IF ' mark >= 55
4
Either / Or Logic IF true THEN run_this ELSE ' above IF is false run_that END IF IF mark >= 55 THEN grade = "PASS" semester = semster + 1 ELSE ' mark is less than a pass grade = "FAIL" END IF ' mark >= 55 (or not)
5
Multiple Exclusive Conditions IF ... ELSEIF ... ELSEIF ... ELSE ... END IF
IF mark >= 80 THEN grade = 'A' gpa = ELSEIF mark >= 70 THEN grade = 'B' gpa = ELSEIF mark >= 60 THEN grade = 'C' gpa = 2.0 ELSEIF mark >= 55 THEN grade = 'D' gpa = ELSE grade = 'F' gpa = END IF ' marks, grades
6
SELECT CASE instead of IF...ELSEIF for a list of equality tests
SELECT CASE grade ' match Grade to GPA CASE "A+", "A" gpa = 4.0 CASE "B" gpa = 3.0 CASE "C" gpa = CASE "D" gpa = CASE ELSE ' all other grades: F, DNC, ***, gpa = 0.0 END SELECT
7
Boolean Logic AND / OR used to evaluate multiple logical conditions
AND means everything joined by AND must be true – a logical conjunction OR is a new set of logical condition(s) – a logical disjunction IF school="ICT" AND program="CNS" _ OR school="ICT" AND program="CTY" THEN
8
Boolean Logic use (parenthesis) to change evaluation order
IF school="ICT" AND _ (program="CNS" OR program="CTY") THEN evaluates as IF school="ICT" AND true THEN IF true AND true THEN IF true THEN
9
Boolean Logic – AND vs OR
check for one of a set of allowed values program = "CNS" OR program = "CTY" OR … check if NOT in a set of allowed values program<>"CNS" AND program<>"CTY" AND … this will NEVER be true: program = "CNS" AND program = "CTY" this will ALWAYS be true: program <> "CNS" OR program <> "CTY"
10
Nested IFs IF school = "ICT" THEN required = "APC100,EAC150,ICA002"
IF program = "CTY" then isINT400required = TRUE ELSE ' programs other than CTY isINT400required = FALSE END IF ' program is CNS | CTY END IF ' school = "ICT"
11
Gotchas ELSEIF vs ELSE IF watch for message: Expected 'End'
do not add END IFs to make it stop complaining IF – ELSEIF – ELSEIF – END IF cascades from one to the other only one will happen IF x = y THEN ELSE IF a = b THEN ' evaluated only when x <> y END IF END IF nested IFs. More than one or none may happen.
12
Gotchas ALWAYS use a logical expression.
Say what you mean. Do not use "trick" of assuming Zero means False and non-Zero means True. VBscript does not do this well. IF number THEN 0 is FALSE as expected NOT 0 is TRUE -- as expected 1 is TRUE as expected NOT 1 is TRUE ** not as expected
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.