1 / 17

BACS 287

BACS 287. Programming Fundamentals 3. Programming Fundamentals. This lecture introduces the following decision control structure topics: If-Then statements IIf statements If-Then-Else statements Select Case statements. Decision Structures.

Download Presentation

BACS 287

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. BACS 287 Programming Fundamentals 3 BACS 287

  2. 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

  3. 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

  4. If-Then Statements • If-Then statements have 2 formats: 1) If condition Then statement 2) If condition Then statements End If BACS 287

  5. 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

  6. 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

  7. 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

  8. 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

  9. If-Then-Else Example If intValue> 100 then intBigValueCnt += 1 Else intSmallValueCnt += 1 End If BACS 287

  10. If-Then-Else Statements • The If-Then-Else structure also allows several statement blocks to be defined. IF condition1 Then [statement block-1] ElseIfcondition2 Then [statement block-2] ... Else [statement block-n] End If BACS 287

  11. If-Then-Else Example If decSales > 100000 then decComm = Calc_Com(.17) ElseIf decSales > 50000 then decComm = Calc_Com(.13) ElseIf decSales > 10000 then decComm = Calc_Com(.08) Else decComm = Calc_Com(.05) End If BACS 287

  12. 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

  13. 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

  14. In-Class Selection Example 1 • Use a selection structure to save a value of “senior” in the strClass variable when the value of the intYear variable is equal to 4, “junior” if it is equal to 3, “sophomore” if it equals 2, and “freshman” if it equals 1. If intYear is any other value, strClass should be set to “error”. BACS 287

  15. If intYear = 1 then strClass = “freshman” ElseIf intYear = 2 then strClass= “sophomore” ElseIf intYear = 3 then strClass = “junior” ElseIf intYear = 4 then strClass = “senior” Else strClass = “error” End If Select Case intYear Case 1 strClass = “freshman” Case 2 strClass = “sophomore” Case 3 strClass = “junior” Case 4 strClass = “senior” Case Else strClass = “error” End Select In-Class Selection Answer 1 BACS 287

  16. In-Class Selection Example 2: Translate to a Selection Statement

  17. 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”

More Related