Visual Basic Programming Making Decisions: Loops & Decision Structures ©Copyright by Ronald P. Kessler, Ph.D.
Program Control: If…Then…Else Statement Conditional statements allow us to make logical evaluations: If Sales > 1200 then Commission= 0.08 BLOCK STYLE: If Sales < 500 Then Commission=.08 ElseIf Sales > 500 And Sales < 1000 Then Commission=.10 Else Commission =.25 End If Nothing after Then ElseIf is never alone Optional: It “catches” “other” conditions Must have End If for each If
If…Then…Else Statement Used to Make Logical Decision If MessageBox.Show("Are you sure”,”Ron’s Store”, MessageBoxButtons.YesNo) = _ Windows.Forms.DialogResult.Yes Then Me.Close() Else MessageBox.Show("Then make up your mind") End If
“Nested” If…Then…Else If txtPIN.Text = “123” Then MsgBox "Congratulations, you remembered your myPIN” btnOK.Enabled = True If optReceipt.Checked = True Then btnPrint.Enabled = True Else btnPrint.Enabled = False End If Else MsgBox “You dope. You can't even remember your myPIN. ” txtPIN.Text = "" txtPIN.Focus End If
Program Control: Select Case Statement Select Case statements are used when we want to perform different actions for each value of a variable Dim state as String = “” Dim salesTax as Single = 0 Select Case state Case “CA” salesTax=.085 Case “AZ”, “NJ” salesTax=.075 Case “NV” salesTax= 0.3 Case Else salesTax= 0 End Select Variable to evaluate Case can be value or string or condition Optional : It “catches” “other” conditions Must have End Select
Select Case Statement: Testing a Range of Values Dim discount as Single = 0 Dim unitsOrdered as Integer = 0 Select Case unitsOrdered Case 1 to 10 discount = 0 Case 11 to 19 discount =.05 Case Is >= 20 discount =.1 End Select
Using Loops...
For…Next Loop Statement Used to Add Numbers to a List Box Dim x as Integer= 0 ‘loop control variable For x = 1 To 100 lbNumbers.Items.Add(x) Next x For…Next loops are called Counted loops
Nested For…Next Loop Statement Used As a Counter... Dim x as Integer = 0 Dim y as Integer = 0 For x = 1 To 3 For y = 1 To 5 lbNumbers.Items.Add(y) Next y Next x
For…Next Loop Statement Used As a Backwards Counter... Dim x as Integer = 0 For x = 10 To 1 Step -1 ListBox1.Items.Add (x) Next x
For…Next Loop Statement Used To Count by 2’s Dim x as Integer = 0 For x = 2 To 20 Step 2 ListBox1.Items.Add (x) Next x
For…Next Loop Statement Used To Count Backwards by 2’s Dim x as Integer = 0 For x = 20 To 2 Step -2 ListBox1.Items.Add (x) Next x
Do…While Statements (Conditional Loops) Do While... Loop: Dim myPIN as String =“” Do While myPIN = “ ” myPIN =InputBox(“Enter myPIN”) Loop Label1.Text = myPIN Do...While Loop: Dim myPIN as String Do myPIN = InputBox(“ Enter myPIN ”) Loop While myPIN = “ ” Label1.Text = myPIN Pre-test condition Post-test condition
Do Until Statements (Conditional Loops) Do Until... Loop: Dim myPIN as String =“” Do Until myPIN = “Ron ” myPIN =InputBox(“Enter myPIN”) Loop Label1.Text = myPIN Do …Until Loop: Dim myPIN as String = “” Do myPIN = InputBox(“Enter myPIN”) Loop Until myPIN = “Ron ” Or myPIN = “99” Label1.Text = myPIN
Using Counts & Totals...
Counts vs Totals... Counts increase or decrease by a fixed amount... Dim count as Integer = 0 Do myPIN =InputBox(“Enter PIN”) count = count +1 Loop Until myPIN = “9” or count =5 Label1.Text = count Totals change by a variable amount… Dim myPrice as Currency = 0 Dim myTotal as Currency = 0 Total = 0 Do Price = InputBox(“Enter Price”) myTotal = myTotal + myPrice Loop Until myPrice = 0 Label1.Text = Total
Using the InputBox Function Sub Form1_Load () Dim price as Decimal = 0 Do price = InputBox (“Enter price of item...”) Loop Until Price = 0 End Sub InputBox is a “Built-In” VB function ALL functions return a value price = InputBox (“Please enter a price”, “Ron’s Store”) InputBox returns Price as a Currency variable. If they hit Cancel, then InputBox returns “” which will cause an error because “” is a string, not currency.
That’s All Folks!