1 / 14

VB Core II

VB Core II. Conditional statements Exception handling Loops Arrays Debugging. if statements. If c > 5 Then x = 1: y = 3. If c > 5 Then x = 1 y = 3 End If. If c > 5 Then x = 1 y = 3 ElseIf c = 4 Then z = 7 Else x = 9 End If. If c > 5 Then x = 1

Télécharger la présentation

VB Core II

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 Core II • Conditional statements • Exception handling • Loops • Arrays • Debugging

  2. if statements If c > 5 Then x = 1: y = 3 If c > 5 Then x = 1 y = 3 End If If c > 5 Then x = 1 y = 3 ElseIf c = 4 Then z = 7 Else x = 9 End If If c > 5 Then x = 1 y = 3 Else z = 7 End If

  3. Exercise – quadratic equation solver Create a form with – 3 textboxes to input values for a b and c (and labels as prompts) 2 labels where the roots will be shown A button captioned 'Solve' Code the button (square root function is sqr )

  4. select • Dim Number • Number = 8 ' Initialize variable. • Select Case Number ' Evaluate Number. • Case 1 To 5 ' Number between 1 and 5, inclusive. • x=4 • Case 6, 7, 8 ' Number between 6 and 8. • x=5 • Case 9 To 10 ' Number is 9 or 10. • x=6 • Case Else ' Other values. • x=7 • End Select

  5. Error and exception handling • exception = problem event at run-time • usually related to I/O • eg file not found, server connection lost, invalid user input • not a programming bug • VB calls exception errors • (Unlike Java) VB does not force exception handling

  6. Error handlers – example - invalid numbers Private Sub Command1_Click() Dim num1 As Integer Dim num2 As Integer Dim result As Integer On Error GoTo myErrorHandler num1 = Text1.Text num2 = Text2.Text result = num1 + num2 Label1.Caption = result Exit Sub myErrorHandler: If Err.Number = 13 Then MsgBox ("Please enter a valid number") Else MsgBox (Err.Description) End If Resume Next End Sub Exercise Try this out in the calculator program Then deal with divide by zero (11)

  7. For next loops Dim x as Integer, total As Integer total = 0 For x = 1 To 5 total = total + x Next Dim x as Integer, total As Integer total = 0 For x = 1 To 5 Step 2 total = total + x Next

  8. Debugging – debug.print • Debug.print x,y,z • Immediate window – CTRL G Exercise – write a for next loop to add up the odd numbers from 1 to 9 inclusive. Use debug.print to check it works

  9. Other loops Dim c As Integer c = 1 Do While c < 5 c = c + 1 Loop Dim c As Integer c = 1 Do Until c >4 c = c + 1 Loop Dim c As Integer c = 1 Do c = c + 1 Loop While c < 5 Dim c As Integer, x as integer c = 1 x=2 Do c = c + 1 Loop Until c>4 And x<>3

  10. Arrays (fixed size) Dim x(100) As Integer Dim i As Integer For i = 0 To 100 x(i) = 99 Next Dim x(1 To 3, 1 To 3) As Integer Dim i as integer, j As Integer For i = 1 To 3 For j = 1 To 3 x(i, j) = 99 Next Next

  11. Dynamic arrays Dim x() As Integer ReDim x(5) Dim i, j As Integer For i = 1 To 5 x(i) = 99 Next ReDim Preserve x(10) For i = 6 To 10 x(i) = 100 Next

  12. Debugging

  13. User defined type – record or struct • Private Type employee • payrollNumber As Integer • name As String • End Type • Private Sub Command1_Click() • Dim emp1 As employee • emp1.name = "John" • emp1.payrollNumber = 4082 • End Sub • Tidier • Halfway to class • Needed for Win32 API

  14. Exercise • Program a button so it finds the prime numbers up to 100 • Produce output using Debug.print • Use the Sieve of Eratosthenes: • Mark off every second number after 2 • Every third number after 3 • Every fourth number after 4 • .. • What is left are the primes

More Related