Download presentation
Presentation is loading. Please wait.
1
Slide 1 VB Program Flow Control
2
Slide 2 Making Decisions v Decision Statement: control the execution of parts of the program based on conditions. v The two basic types are: If statements and Select Case statements
3
Slide 3 If statement v If statement: single line or multiple lines v Single line: (single task) –Syntax: If condition Then command If x > 5 then x = 0 v 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
Slide 4 If statement v Using NOT operator If Not bTaxable Then cSalesTax = 0 cUseTax = 0 End If v Else part If condition Then statements to process when condition is True Else statements to process when condition is False End If
5
Slide 5 If statement v ElseIf Part If fTest < 0 Then lblResult.Caption = “Negative” ElseIf fTest = 0 Then lblResult.Caption = “Zero” Else lblResult.Caption = “Positive” End If
6
Slide 6 Select Case v 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. v Syntax: Select Case testexpression [Case expressionlist-n statement group 1 [Case Else expressionlist-n statement group 2 End Select
7
Slide 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
Slide 8 Select Case
9
Slide 9 Loops v Loops perform repetitive tasks in a program. v 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
Slide 10 For Loops v Repeats a group of statements a specified number of times. v Define a counter variable v Start and end value v 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
11
Slide 11 For Loops v Example 1: For i = 1 To 10 sTemp = i & “ squared is “ & (i * i) Printer.Print sTemp Next i v Example 2: For i = 1 To 12 fMonthSales(i) = 0 fMonthExpenses(i) = 0 fMonthProfit(i) = 0 Next i
12
Slide 12 Do Loops v Repeats a block of statements while a condition is True or until a condition becomes True. v Syntax –Do [{While | Until} condition] [statements] [Exit Do] [statements] Loop –Do [statements] [Exit Do] [statements] Loop [{While | Until} condition]
13
Slide 13 Do Loops v Example 1: Dim I As Integer I = 0 Do I = InputBox(“Enter 0 to exit the loop!”) Loop Until I = 0
14
Slide 14 Logical Expressions v 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 v The comparison is based on the ASCII coding scheme where uppercase is less than lowercase; numbers are less than uppercase characters. v A condition is an expression. Like all expressions conditions are evaluated and return a value. v The return value of a logical expression is either True or False
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.