150 likes | 181 Views
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)
E N D
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) • 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
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
If statement • ElseIf Part If fTest < 0 Then lblResult.Caption = “Negative” ElseIf fTest = 0 Then lblResult.Caption = “Zero” Else lblResult.Caption = “Positive” End If
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
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
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.
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
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
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]
Do Loops • Example 1: Dim I As Integer I = 0 Do I = InputBox(“Enter 0 to exit the loop!”) Loop Until I = 0
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