510 likes | 1k Views
Outline. VB help, basic architectureFinding information (lectures)Program Development Cycle (2.1, lectures)VB Controls and Events (3.1, 3.2, lectures)VB Programming basicsStructure of VB programs (lectures)Data types, variables (3.3, 3.4, lectures)Operators (3.3, 3.4, 5.1, lectures)VB Progra
E N D
1. CS 4 Intro to Programming using Visual Basic Exam 2 review
Patchrawat UthaisombutUniversity of Pittsburgh
1
2. Outline VB help, basic architecture
Finding information (lectures)
Program Development Cycle (2.1, lectures)
VB Controls and Events (3.1, 3.2, lectures)
VB Programming basics
Structure of VB programs (lectures)
Data types, variables (3.3, 3.4, lectures)
Operators (3.3, 3.4, 5.1, lectures)
VB Program flow control
If blocks, Select Case Blocks (5.2, 5.3, lectures)
Repetitions (ch 6, lectures)
Debugging (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. VB Controls (so far) Label
Textbox
Listbox
Button
Timer Chapter 6 - VB 2005 by Schneider 7
8. VB Controls Properties Text
TextAlign
Fonts
ForeColor
BackColor
ReadOnly
Visible
Size
Chapter 6 - VB 2005 by Schneider 8
9. VB events (so far) Click
TextChanged
Leave
Tick (for timer)
Chapter 6 - VB 2005 by Schneider 9
10. Outline VB help, basic architecture
Finding information (lectures)
Program Development Cycle (2.1, lectures)
VB Controls and Events (3.1, 3.2, lectures)
VB Programming basics
Structure of VB programs (lectures)
Data types, variables (3.3, 3.4, lectures)
Operators (3.3, 3.4, 5.1, lectures)
VB Program flow control
If blocks, Select Case Blocks (5.2, 5.3, lectures)
Repetitions (ch 6, lectures)
Debugging (lectures) P. Uthaisombut 10
11. General facts about VB Code Case INsensitive
TOTAL, Total, total, ToTaL, toTAL
1 line, 1 command
Comment ( ` )
Line-continuation ( _ )
11
12. 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 12
13. 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
13
14. 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
14
15. Event Handler If subroutine btnStart_Click is an event handler for an event btnStart.Click, it should have the clause Handles btnStart.Clickafter the subroutine name.
Private Sub btnStart_Click(…) Handles btnStart.Click
‘ Code goes here
‘ Code goes here
End Sub 15
16. Outline VB help, basic architecture
Finding information (lectures)
Program Development Cycle (2.1, lectures)
VB Controls and Events (3.1, 3.2, lectures)
VB Programming basics
Structure of VB programs (lectures)
Data types, variables (3.3, 3.4, lectures)
Operators (3.3, 3.4, 5.1, lectures)
VB Program flow control
If blocks, Select Case Blocks (5.2, 5.3, lectures)
Repetitions (ch 6, lectures)
Debugging (lectures) P. Uthaisombut 16
17. Variables and Data Types A variable is a nickname to refer to a memory location in the computer.
Ex: It is easier to refer to “carSpeed” than “memory location 5472”.
Each variable has a data type.
The data type tells us what kind of information is stored in the variable (memory location); It indicates how to interpret the information stored in the variable (as an integer, a string, etc.)
In VB we have to declare a variable to be a certain data type before we can use the variable
Ex: Dim carSpeed As Integer
18. How to make sense of “x = x + 1”? In a program, “x = x + 1” does not assert that x is equal to x+1.
This is an assignment statement.
Suppose x refers to address 1703. And suppose that the address contains 5.
To interpret this.
Take the value in the address 1703.
Add 1 to it.
Then store the result in the address 1703.
After the execution, the result is that the address 1703 will contain 6.
19. Hierarchical summary Numbers
Integers
Byte, Short, Integer, Long
Floating-point
Single, Double
Text
String
Char
Boolean
Other
… 19
20. Naming Convention for Variables Use lowercase letters except for the first letters of additional words. This is called “camel casing”.
Example
gradeOnFirstExam
firstExamGrade
windowLocation
totalScore
21. Naming Convention for Controls Use camel casing
Begins with 3-4-letter prefix that indicates the type of the control
Followed by short but informative word or phrases
Examples
btnStartClock
txtCurrentTime
lblUserName
22. Some Controls and Their Prefixes
23. Number Operators + - * / ^
Math.Sqrt(x) returns square root of x
Int(x) returns the greatest integer less than or equal to x
Math.Round(n,r) returns the number n rounded to r decimal places.
x = 2 + 2 ? x is 4
y = 2^3 ? y is 8
z = Int(2.45) ? z is 2
24. Increment, add to, … x += 1 ? x = x + 1
y *= x+2 ? y = y * (x + 2)
25. String Concatenation str1 = “foot”
str2 = “ball”
str3 = str1 & str2 ? str3 is “footbal”
str2 &= “ “ & str3 ? str3 is “ball football”
acc = 3.2
op = 1.5
acc += op ? acc is 4.7
str = "op + is acc") ? “op + is acc”
str = "op" & " + is " & "acc") ? “op + is acc”
str = op & "+ is" & acc) ? “4.7+ is3.2”
str = op & " + is " & acc) ? “4.7 + is 3.2”
25
26. String functions Dim str1 As String
str1 = “abcdefghij”
str1.Length is 10
str1.SubString(0,3) is “abc”
str1.SubString(3,3) is “def”
str1.IndexOf(“ab”) is 0
str1.IndexOf(“def”) is 3
str1.IndexOf(“fed”) is -1
str1.ToUpper is “ABCDEFGHIJ”
“HeLLo”.ToLower is “hello”
“ test test “.Trim is “test test”
26
27. String functions Concatenating strings & (p91)
Converting strings to numbers CDbl (p90)
Converting numbers to strings CStr (p90)
Other strings functions (p92)
Length, ToUpper, ToLower, Trim
SubString, IndexOf
Empty string (p95)
Empty string. Textbox’s Clear() method.
27
28. Outline VB help, basic architecture
Finding information (lectures)
Program Development Cycle (2.1, lectures)
VB Controls and Events (3.1, 3.2, lectures)
VB Programming basics
Structure of VB programs (lectures)
Data types, variables (3.3, 3.4, lectures)
Operators (3.3, 3.4, 5.1, lectures)
VB Program flow control
If blocks, Select Case Blocks (5.2, 5.3, lectures)
Repetitions (ch 6, lectures)
Debugging (lectures) P. Uthaisombut 28
29. Character representation P. Uthaisombut 29
30. Chapter 5 - VB 2005 by Schneider 30 Chr Function For n between 0 and 255,
Chr(n)
is the string consisting of the character with
ANSI value n.
EXAMPLES: Chr(65) is "A"
Chr(162) is "¢"
31. Chapter 5 - VB 2005 by Schneider 31 Asc Function For a string str,
Asc(str)
is ANSI value of the first character of str.
EXAMPLES: Asc("A") is 65
Asc("¢25") is 162
32. Chapter 5 - VB 2005 by Schneider 32 Relational Operators < less than
<= less than or equal to
> greater than
>= greater than or equal to
= equal to
<> not equal to
ANSI values are used to decide order for strings.
33. Chapter 5 - VB 2005 by Schneider 33 Logical Operators Used with Boolean expressions
Not – makes a False expression True and vice versa
And – will yield a True if and only if both expressions are True
Or – will yield a True if at least one of both expressions are True
34. Data type of subexpressions ( 2 < n ) And ( n < 5 )
Integer Integer Integer Integer
Boolean Boolean
Boolean P. Uthaisombut 34
35. guess <> secret
NOT (guess = secret)
(guess < secret) OR (guess > secret)
P. Uthaisombut 35
36. Outline VB help, basic architecture
Finding information (lectures)
Program Development Cycle (2.1, lectures)
VB Controls and Events (3.1, 3.2, lectures)
VB Programming basics
Structure of VB programs (lectures)
Data types, variables (3.3, 3.4, lectures)
Operators (3.3, 3.4, 5.1, lectures)
VB Program flow control
If blocks, Select Case Blocks (5.2, 5.3, lectures)
Repetitions (ch 6, lectures)
Debugging (lectures) P. Uthaisombut 36
37. Chapter 5 - VB 2005 by Schneider 37 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
38. Chapter 5 - VB 2005 by Schneider 38 Another example If block If condition Then
action1
End If
Statement2
Statement3
39. 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 39 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.
40. Example from Guess-A-Number Here, we assume that guess is not equal to secret. Otherwise, the game should have already terminated.
If guess < secret Then
` too small
Else
If guess > secret Then
` too big
Endif
EndIf
If guess < secret Then
` too small
EndIf
If guess > secret Then
` too big
EndIf
-------------------------
If guess < secret Then
` too small
Else
` too big
EndIf
41. Simplified Nested If Statement If A < B Then
If A < C Then
(A is smallest)
Else
(C is smallest)
End If
Else
If B < C Then
(B is smallest)
Else
(C is smallest)
End If
End If If A < B And A < C Then
(A is smallest)
ElseIf B < A And B < C Then
(B is smallest)
Else
(C is smallest)
End If
(Assume A,B,C are not equal) Schneider & Uthaisombut 41
42. Chapter 5 - VB 2005 by Schneider 42 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
43. Outline VB help, basic architecture
Finding information (lectures)
Program Development Cycle (2.1, lectures)
VB Controls and Events (3.1, 3.2, lectures)
VB Programming basics
Structure of VB programs (lectures)
Data types, variables (3.3, 3.4, lectures)
Operators (3.3, 3.4, 5.1, lectures)
VB Program flow control
If blocks, Select Case Blocks (5.2, 5.3, lectures)
Repetitions (ch 6, lectures)
Debugging (lectures) P. Uthaisombut 43
44. Chapter 6 - VB 2005 by Schneider 44 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”.
45. Chapter 6 - VB 2005 by Schneider 45 Pseudocode and Flow Chart for a Do Loop
46. Dim num As Integer = 1
Do While num <= 7
lstNumbers.Items.Add(num)
num += 1 'Add 1 to the value of num
Loop
Dim x As Integer = 5
Dim y As Integer = 1
Do While y <= 12
lstTable.Items.Add(x & " * " & y & " = " & x * y)
y += 1
Loop P. Uthaisombut 46
47. Chapter 6 - VB 2005 by Schneider 47 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.
48. Chapter 6 - VB 2005 by Schneider 48 Pseudocode and Flowchart for a Post-Test Loop
49. Chapter 6 - VB 2005 by Schneider 49 Example: Repeat Request Until Proper Response is Given Do
passWord = InputBox("What is the password?")
passWord = passWord.ToUpper
Loop Until passWord = "SHAZAM"
50. secretNum = CInt(Int(Rnd() * 5 + 1))
Do
guessNum = Cint(InputBox("Guess."))
Loop Until guessNum = secretNum
MsgBox(“You are right!”);
--------------------------------------------------
secretNum = CInt(Int(Rnd() * 5 + 1))
guessNum = CInt(InputBox(“Guess.“)
Do While guessNum <> secretNum
guessNum = Cint(InputBox(“Wrong. Guess again."))
Loop
MsgBox(“You are right!”);
P. Uthaisombut 50
51. Outline VB help, basic architecture
Finding information (lectures)
Program Development Cycle (2.1, lectures)
VB Controls and Events (3.1, 3.2, lectures)
VB Programming basics
Structure of VB programs (lectures)
Data types, variables (3.3, 3.4, lectures)
Operators (3.3, 3.4, 5.1, lectures)
VB Program flow control
If blocks, Select Case Blocks (5.2, 5.3, lectures)
Repetitions (ch 6, lectures)
Debugging (lectures) P. Uthaisombut 51
52. Tracing
Perform one statement at a time. After each statement, the debugger will stop the program to allow the programmer to inspect the state of the program.
Break points
Statements in the program where the programmer has previously specified set.
When the program reaches a break point, it stops. This allows the programmer to inspect the state of the program