BACS 287 Programming Fundamentals 3
BACS 287 Programming Fundamentals This lecture introduces the following decision control structure topics: – If-Then statements – IIf statements – If-Then-Else statements – Select Case statements
BACS 287 Decision Structures Visual Basic has 4 primary decision structures: – IF-Then – IIF – IF-Then-Else – Select Case These structures work basically the same as was covered in the pseudocode lectures.
BACS 287 If-Then Statements If-Then statements have 2 formats: 1) If condition Then statement 2) If condition Then statements End If
BACS 287 If-Then Examples If datDate = Date then Debug.Writeline “Today” If strColor = “red” then Debug.Writeline “Color = red” End If If shoX = 1 and shoY = 3 then shoZ = shoX + shoY Debug.Writeline “Sum is :”; shoZ End If
BACS 287 IIf Statements The IIF statement is intended to assign a True or False value anywhere a function could be used. result = IIF(condition, truevalue, falsevalue) The IIF does not allow multiple true/false statements. Everything must fit into a single expression.
BACS 287 IIf Examples IIf(intCnt >= 10,Debug.Writeline “over 10”,Debug.Writeline “under 10”) IIf(IsNull(strX), 0, strX.Length) Function Check (intTest as integer) Check = IIf(intTest> 100, “Large”, “Small”) End Function
BACS 287 If-Then-Else Statements The If-Then-Else structure allows the standard two branch IF statement to be defined. IF condition1 Then [statement block-1] Else [statement block-2] End If
BACS 287 If-Then-Else Example If intValue > 100 then intBigValueCnt += 1 Else intSmallValueCnt += 1 End If
BACS 287 If-Then-Else Statements The If-Then-Else structure also allows several statement blocks to be defined. IF condition1 Then [statement block-1] ElseIf condition2 Then [statement block-2]... Else [statement block-n] End If
BACS 287 If-Then-Else Example If decSales > then decComm = Calc_Com(.17) ElseIf decSales > then decComm = Calc_Com(.13) ElseIf decSales > then decComm = Calc_Com(.08) Else decComm = Calc_Com(.05) End If
BACS 287 Select Case Statements Select Case statements are the Visual Basic version of a CASE statement. Select Case test-expression [Case expression-list1 [statement block-1]] [Case expression-list2 [statement block-2]]... [Case Else [statement block-n]] End Select
BACS 287 Select Case Example Select Case strEmpStatus Case “full” strStatus = “Full Time” Case “part” strStatus = “Part Time” Case “contract” strStatus = “Contractor” Case Else strStatus = “Unknown” End Select
BACS 287 In-Class Selection Example 1 Use a selection structure to save a value of “senior” in the strClass variable when the value of the strYear variable is equal to “sr”, “junior” if it is equal to “j”, “sophomore” if it equals “so”, and “freshman” if it equals “f”. If strYear is any other value, strClass should be set to “error”.
BACS 287 In-Class Selection Answer 1 If strYear = “f” then strClass = “freshman” ElseIf strYear = “so” then strClass= “sophomore” ElseIf strYear = “j” then strClass = “junior” ElseIf strYear = “sr” then strClass = “senior” Else strClass = “error” End If Select Case strYear Case “f” strClass = “freshman” Case “so” strClass = “sophomore” Case “j” strClass = “junior” Case “sr” strClass = “senior” Case Else strClass = “error” End Select
In-Class Selection Example 2: Translate to a Selection Statement
In-Class Selection Answer 2 If Q1 = 5 then If Q2 = 4 then ST1 = “a” ST2 = “b” Else ST7 = “g” End If Else ST3 = “c” If Q3 = 1 then ST4 = “d” End If ST5 = “e” End If ST6 = “f”