Presentation is loading. Please wait.

Presentation is loading. Please wait.

Visual Basic Programming Making Decisions: Loops & Decision Structures ©Copyright 1999-2012 by Ronald P. Kessler, Ph.D.

Similar presentations


Presentation on theme: "Visual Basic Programming Making Decisions: Loops & Decision Structures ©Copyright 1999-2012 by Ronald P. Kessler, Ph.D."— Presentation transcript:

1 Visual Basic Programming Making Decisions: Loops & Decision Structures ©Copyright 1999-2012 by Ronald P. Kessler, Ph.D.

2 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

3 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

4 “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

5 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

6 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

7 Using Loops...

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

9 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 123451234512345123451234512345

10 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 10 9 8 7 6 5 4 3 2 1

11 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 2 4 6 8 10 12 14 16 18 20

12 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 20 18 16 14 12 10 8 6 4 2

13 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

14 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

15 Using Counts & Totals...

16 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

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

18 That’s All Folks!


Download ppt "Visual Basic Programming Making Decisions: Loops & Decision Structures ©Copyright 1999-2012 by Ronald P. Kessler, Ph.D."

Similar presentations


Ads by Google