1 / 43

CS 4 Intro to Programming using Visual Basic

Outline. VB BasicsFinding information (lectures)Program Development Cycle (2.1, lectures)Debugging (lectures)VB Program flow controlsIf blocks, Select Case Blocks (5.2, 5.3, lectures)Repetitions (ch 6, lectures)VB Working with files (3.5, 6.2, lectures)Sub procedures, Function procedures (c

zanna
Download Presentation

CS 4 Intro to Programming using Visual Basic

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. CS 4 Intro to Programming using Visual Basic Exam 1 review Patchrawat Uthaisombut University of Pittsburgh 1

    2. Outline VB Basics Finding information (lectures) Program Development Cycle (2.1, lectures) Debugging (lectures) VB Program flow controls If blocks, Select Case Blocks (5.2, 5.3, lectures) Repetitions (ch 6, lectures) VB Working with files (3.5, 6.2, lectures) Sub procedures, Function procedures (ch 4, lectures) Arrays (ch 7, lectures) P. Uthaisombut 2

    3. Where to find information Textbook Lecture slides VB help system. (local copy of MSDN library) Microsoft Developer Network Library (MSDN Library) Google search Chapter 6 - VB 2005 by Schneider 3

    4. VB Help Contents Development Tools and Languages Visual Studio Integrated Development Environment for Visual Studio Visual Basic Visual Basic Programming Guide Program Structure and Code Conventions Visual Basic Language Features Reference Samples

    5. Software Development Cycle Analyze What the program should do Design Create steps to solve problem or to perform tasks Choose the interface How the application will interact with the user Build interface and write code Create interface and write code in VB Test and debug Find and remove bugs 5 P. Uthaisombut

    6. Software Development Cycle Analyze Display “Hello World!” when the user click a button Interface 1 button and 1 textbox Design Button click event handler: displays text in textbox Build interface and write code … (in IDE) … Test and Debug … (in IDE) … 6 P. Uthaisombut

    7. Debugging Break points To set/reset a break point. click on the strip to the left of the line. A red dot will appear on the strip to the left of the line. Tracing / Stepping The line to be executed will be highlighted as yellow. Press F11 to execute the line. The yellow indicator will move to the next line to be executed.

    8. Outline VB Basics Finding information (lectures) Program Development Cycle (2.1, lectures) Debugging (lectures) VB Program flow controls If blocks, Select Case Blocks (5.2, 5.3, lectures) Repetitions (ch 6, lectures) VB Working with files (3.5, 6.2, lectures) Sub procedures, Function procedures (ch 4, lectures) Arrays (ch 7, lectures) P. Uthaisombut 8

    9. Structure of VB Code Public Class frmStopwatch Private Sub btnStart_Click(…) Handles btnStart.Click txtSeconds.Text = "0" 'Reset Watch tmrWatch.Enabled = True End Sub Private Sub btnStop_Click(…) Handles btnStop.Click tmrWatch.Enabled = False End Sub Private Sub tmrWatch_Tick(…) Handles tmrWatch.Tick txtSeconds.Text = CStr((CDbl(txtSeconds.Text) + 0.1)) End Sub End Class 9

    10. Class vs End Class VB Code that belongs to a form frmStopwatch is placed between the following 2 lines. The code should be indented Public Class frmStopwatch ‘ code goes here ‘ code goes here End Class 10

    11. Sub vs End Sub VB Code that belongs to a subroutine sub1 is placed between the following 2 lines. The code should be indented Private Sub sub1() ‘ code goes here ‘ code goes here End Sub 11

    12. Outline VB Basics Finding information (lectures) Program Development Cycle (2.1, lectures) Debugging (lectures) VB Program flow controls If blocks, Select Case Blocks (5.2, 5.3, lectures) Repetitions (ch 6, lectures) VB Working with files (3.5, 6.2, lectures) Sub procedures, Function procedures (ch 4, lectures) Arrays (ch 7, lectures) P. Uthaisombut 12

    13. Chapter 5 - VB 2005 by Schneider 13 If Block The program will take a course of action based on whether a condition is true. If condition Then action1 Else action2 End If

    14. Chapter 5 - VB 2005 by Schneider 14 Another example If block If condition Then action1 End If Statement2 Statement3

    15. ElseIf clause If condition1 Then action1 Else If condition2 Then action2 Else If condition3 Then action3 Else action4 End If EndIf EndIf If condition1 Then action1 ElseIf condition2 Then action2 ElseIf condition3 Then action3 Else action4 End If Schneider & Uthaisombut 15 An extension of the If block allows for more than two possible alternatives with the inclusion of ElseIf clauses. Note: there is no space between the word "Else" and "If" Only one "End If" is required.An extension of the If block allows for more than two possible alternatives with the inclusion of ElseIf clauses. Note: there is no space between the word "Else" and "If" Only one "End If" is required.

    16. Chapter 5 - VB 2005 by Schneider 16 Example 3: Partial Code Dim x As Integer = 2, y As Integer = 3 Select Case num Case y - x, x txtPhrase.Text = "Buckle my shoe." Case Is <= 4 txtPhrase.Text = "Shut the door." Case x + y To x * y txtPhrase.Text = "Pick up sticks." Case 7, 8 txtPhrase.Text = "Lay them straight." Case Else txtPhrase.Text = "Start all over again." End Select

    17. Chapter 6 - VB 2005 by Schneider 17 Do Loop Syntax Do While condition statement(s) Loop A Do statement precedes the sequence of statements, and a Loop statement follows the sequence of statements. The condition, preceded by either the word “While” or the word “Until”, follows the word “Do” or the word “Loop”. A Do statement precedes the sequence of statements, and a Loop statement follows the sequence of statements. The condition, preceded by either the word “While” or the word “Until”, follows the word “Do” or the word “Loop”.

    18. Chapter 6 - VB 2005 by Schneider 18 Post Test Loop Do statement(s) Loop Until condition If condition is true, then the program continues with the line after the Loop statement. If condition is false, then the entire process is repeated beginning with the Do statement. In other words, the statements inside the loop are executed once and then are repeatedly executed until the condition is true.If condition is true, then the program continues with the line after the Loop statement. If condition is false, then the entire process is repeated beginning with the Do statement. In other words, the statements inside the loop are executed once and then are repeatedly executed until the condition is true.

    19. Chapter 6 - VB 2005 by Schneider 19 For…Next Loop Syntax

    20. Loop comparison Dim i As Integer i = 10 Do While i <= 20 lstOutput.Items.Add(i) i += 2 Loop Dim i As Integer i = 10 Do lstOutput.Items.Add(i) i += 2 Loop Until i>20 For i As Integer = 10 To 20 Step 2 lstOutput.Items.Add(i) Next 10 12 14 16 18 20

    21. Chapter 6 - VB 2005 by Schneider 21 Nested For…Next Loops You can put a loop inside a loop, but the inner loop must be fully contained inside the outer loopYou can put a loop inside a loop, but the inner loop must be fully contained inside the outer loop

    22. Outline VB Basics Finding information (lectures) Program Development Cycle (2.1, lectures) Debugging (lectures) VB Program flow controls If blocks, Select Case Blocks (5.2, 5.3, lectures) Repetitions (ch 6, lectures) VB Working with files (3.5, 6.2, lectures) Sub procedures, Function procedures (ch 4, lectures) Arrays (ch 7, lectures) P. Uthaisombut 22

    23. Reading a file Open sr = IO.File.OpenText(“NAMES.TXT") Read strVar = sr.ReadLine Close sr.Close() P. Uthaisombut & Schneider 23

    24. Reading a file Dim strVar As String Dim sr As IO.StreamReader sr = IO.File.OpenText(“NAMES.TXT") strVar = sr.ReadLine sr.Close() P. Uthaisombut & Schneider 24

    25. Peek method If the end of the file has been reached, the value of sr.Peek is -1. We can use Peek in combination with a loop to read a file of arbitrary length Uthaisombut & Schneider 25

    26. Uthaisombut & Schneider 26 Read and Display Dim str As String Dim sr As IO.StreamReader = _ IO.File.OpenText(“FILE.TXT") lstNumbers.Items.Clear() Do While sr.Peek <> -1 str = sr.ReadLine lstNumbers.Items.Add(str) Loop sr.Close()

    27. Chapter 6 - VB 2005 by Schneider 27 Counters and Accumulators A counter is a numeric variable that keeps track of the number of items that have been processed. An accumulator is a numeric variable that totals numbers.

    28. Loop comparison Dim i As Integer i = 10 Do While i <= 20 lstOutput.Items.Add(i) i += 2 Loop Dim i As Integer i = 10 Do lstOutput.Items.Add(i) i += 2 Loop Until i>20 For i As Integer = 10 To 20 Step 2 lstOutput.Items.Add(i) Next 10 12 14 16 18 20

    29. Uthaisombut & Schneider 29 Count and Total Dim counter As Integer Dim total As Double Dim val As Double Dim sr As IO.StreamReader = _ IO.File.OpenText(“NUMBERS.TXT") counter = 0 total = 0 Do While sr.Peek <> -1 counter += 1 val = CDbl(sr.ReadLine) total += val Loop sr.Close() lstNumbers.Items.Clear() lstNumbers.Items.Add(“There are “ & counter & _ “ numbers”) lstNumbers.Items.Add(“The total is “ & total)

    30. Chapter 6 - VB 2005 by Schneider 30 Flags A flag is a variable that keeps track of whether a certain situation has occurred. The data type most suited to flags is Boolean.

    31. Uthaisombut & Schneider 31 Count and Check Ordering Dim word1 As String = "" Dim orderFlag As Boolean = True Do While (sr.Peek <> -1) word2 = sr.ReadLine wordCounter += 1 If word1 > word2 Then orderFlag = False End If word1 = word2 Loop lstNumbers.Items.Add(“There are “ & counter & _ “ numbers”) If orderFlag Then lstNumbers.Items.Add(“Words are sorted”) Else lstNumbers.Items.Add(“Words are not sorted”) End If This program counts the number of words in the file WORDS.TXT and then reports whether the words are in alphabetical order. In each execution of the loop, a word is compared to the next word in the list. The flag variable, called orderFlag, is initially assigned the value True and is set to False if a pair of adjacent words is out of order. This program counts the number of words in the file WORDS.TXT and then reports whether the words are in alphabetical order. In each execution of the loop, a word is compared to the next word in the list. The flag variable, called orderFlag, is initially assigned the value True and is set to False if a pair of adjacent words is out of order.

    32. Outline VB Basics Finding information (lectures) Program Development Cycle (2.1, lectures) Debugging (lectures) VB Program flow controls If blocks, Select Case Blocks (5.2, 5.3, lectures) Repetitions (ch 6, lectures) VB Working with files (3.5, 6.2, lectures) Sub procedures, Function procedures (ch 4, lectures) Arrays (ch 7, lectures) P. Uthaisombut 32

    33. Chapter 4 - VB 2005 by Schneider 33 Sub Procedures Perform one or more related tasks General syntax Sub ProcedureName() statements End Sub

    34. Chapter 4 - VB 2005 by Schneider 34 Arguments and Parameters Sum(2, 3) Sub Sum(ByVal num1 As Double, ByVal num2 As Double)

    35. Chapter 4 - VB 2005 by Schneider 35 Passing by Value When a variable argument is passed to a ByVal parameter, just the value of the argument is passed. After the Sub procedure terminates, the variable has its original value.

    36. Chapter 4 - VB 2005 by Schneider 36 Example

    37. Chapter 4 - VB 2005 by Schneider 37 Passing by Reference When a variable argument is passed to a ByRef parameter, the parameter is given the same memory location as the argument. After the Sub procedure terminates, the variable has the value of the parameter.

    38. Chapter 4 - VB 2005 by Schneider 38 Same Example: n num

    39. Chapter 4 - VB 2005 by Schneider 39 Function Procedures Function procedures (aka user-defined functions) always return one value Syntax: Function FunctionName(ByVal var1 As Type1, _ ByVal var2 As Type2, _ …) As dataType statement1 statement2 … Return expression End Function

    40. Chapter 4 - VB 2005 by Schneider 40 Functions vs. Procedures Both can perform similar tasks Both can call other subs and functions Use a function when you want to return one and only one value

    41. Outline VB Basics Finding information (lectures) Program Development Cycle (2.1, lectures) Debugging (lectures) VB Program flow controls If blocks, Select Case Blocks (5.2, 5.3, lectures) Repetitions (ch 6, lectures) VB Working with files (3.5, 6.2, lectures) Sub procedures, Function procedures (ch 4, lectures) Arrays (ch 7, lectures) P. Uthaisombut 41

    42. Arrays Summary Dim student(29) As String Dim score(29) As Double Student(5) = “George” Student.GetUpperBound(0) ReDim student(50) ReDim Preserve student(50) arrayOne = arrayTwo Erase arrayName Uthaisombut & Schneider 42

    43. Uthaisombut & Schneider 43 Unsorted Array with Repetition Private Function Insert(ByVal key As Integer) As Boolean val(num) = key num += 1 Return True End Function Private Function Search(ByVal key As Integer) As Boolean For i As Integer = 0 To num – 1 If val(i) = key Then Return True End If Next Return False End Function Private Function Delete(ByVal key As Integer) As Boolean For i As Integer = 0 To num – 1 If val(i) = key Then val(i) = val(num - 1) num -= 1 Return True End If Next Return False End Function

    44. Outline VB Basics Finding information (lectures) Program Development Cycle (2.1, lectures) Debugging (lectures) VB Program flow controls If blocks, Select Case Blocks (5.2, 5.3, lectures) Repetitions (ch 6, lectures) VB Working with files (3.5, 6.2, lectures) Sub procedures, Function procedures (ch 4, lectures) Arrays (ch 7, lectures) P. Uthaisombut 44

More Related