1 / 14

VB Program Flow Control

VB Program Flow Control. Making Decisions. Decision Statement: control the execution of parts of the program based on conditions. The two basic types are: If statements and Select Case statements. If statement. If statement: single line or multiple lines Single line: (single task)

jenkinst
Download Presentation

VB Program Flow Control

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. VB Program Flow Control

  2. Making Decisions • Decision Statement: control the execution of parts of the program based on conditions. • The two basic types are: If statements and Select Case statements

  3. If statement • If statement: single line or multiple lines • Single line: (single task) • Syntax: If condition Then command If x > 5 then x = 0 • multiple lines: (multiple tasks) • Syntax: If condition Then Commands end if If x > 5 Then x = 0 End If If cDepositAmt > 0 Then cTotalPaid = cTotalPaid + cDepositAmt cDepositAmt = 0 Call UpdateReservation End If

  4. If statement • Using NOT operator If Not bTaxable Then cSalesTax = 0 cUseTax = 0 End If • Else part If condition Then statements to process when condition is True Else statements to process when condition is False End If

  5. If statement • ElseIf Part If fTest < 0 Then lblResult.Caption = “Negative” ElseIf fTest = 0 Then lblResult.Caption = “Zero” Else lblResult.Caption = “Positive” End If

  6. Select Case • Allows to conditionally execute any of a series of statement groups based on the value of a test expression, which can be a single variable or a complex expression. • Syntax: Select Casetestexpression    [Case expressionlist-n        statement group 1    [Case Else expressionlist-n        statement group 2 End Select

  7. Select Case nQtyOrdered = Val(txtQuantity) Select Case nQtyOrdered Case Is < 0 ‘note use of comparison MsgBox “Order quantity cannot be negative!”, vbExclamation Exit Sub Case 1, 2, 3 ‘note use of list fDiscount = 0 Case 4 To 9 ‘note use of range fDiscount = 0.03 Case 10 To 49 fDiscount = 0.08 Case Is > 50 fDiscount = 0.1 End Select

  8. Select Case

  9. Loops • Loops perform repetitive tasks in a program. • Three main types: • Counter loops (For): perform a task a set number of times • Conditional loops (Do): perform a task while a specified condition exists or until a specified condition exists. • Enumeration loops: used to perform an action on each item in a group of objects.

  10. For Loops • Repeats a group of statements a specified number of times. • Define a counter variable • Start and end value • Step value (Optional) • For counter = start To end [Step step]    [statements]    [Exit For]    [statements]Next Nested For Loop: For I = 1 To 10 For J = 1 To 10 For K = 1 To 10 . . . Next Next Next

  11. For Loops • Example 1: For i = 1 To 10 sTemp = i & “ squared is “ & (i * i) Printer.Print sTemp Next i • Example 2: For i = 1 To 12 fMonthSales(i) = 0 fMonthExpenses(i) = 0 fMonthProfit(i) = 0 Next i

  12. Do Loops • Repeats a block of statements while a condition is True or until a condition becomes True. • Syntax • Do [{While | Until} condition]    [statements]    [Exit Do]    [statements]Loop • Do    [statements]    [Exit Do]    [statements]Loop [{While | Until} condition]

  13. Do Loops • Example 1: Dim I As Integer I = 0 Do I = InputBox(“Enter 0 to exit the loop!”) Loop Until I = 0

  14. Logical Expressions • Relational operators are used to construct Logical Expressions. The standard operators are: = Equal to > Greater than < Less than <> Not equal to >= Greater than or Equal to <= Less than or Equal to • The comparison is based on the ASCII coding scheme where uppercase is less than lowercase; numbers are less than uppercase characters. • A condition is an expression. Like all expressions conditions are evaluated and return a value. • The return value of a logical expression is either True or False

More Related