VBHTP2e_03-Beta
VBHTP2e_03-Beta
3
Introduction to
Visual Basic
Programming
OBJECTIVES
In this chapter you will learn:
To write simple Visual Basic programs using code rather
than visual programming.
To write statements that input data from the keyboard and
output data to the screen.
To declare and use data of various types.
To store and retrieve data from memory.
To use arithmetic operators to perform calculations.
To use the precedence of arithmetic operators to determine
the order in which operators are applied.
To write decision-making statements.
To use equality and relational operators to compare
operands.
To use message dialogs to display messages.
3.1 Introduction
3.2 Displaying a Line of Text
3.3 Creating Your First Console Application in Visual
Basic Express
3.4 Displaying a Single Line of Text with Multiple
Statements
3.5 Adding Integers
3.6 Memory Concepts
3.7 Arithmetic
3.8 Decision Making: Equality and Relational Operators
3.9 Using a Message Dialog to Display a Message
3.10 (Optional) Software Engineering Case Study:
Examining the ATM Requirements Document
3.11 Wrap-Up
3.12 Web Resources
3.1 Introduction
• Console Application
– Input and output text in console window
• Windows 95/98/ME: MS-DOS prompt
• Other version of Windows: Command Prompt
• Sample program
– Displays a line of text
– Illustrates several important VB language features
• Blank line
– Makes program more readable
– Blank lines, spaces, and tabs are white-space characters
– Ignored by compiler
4 Module FirstWelcome
The following are retained as keywords, although they are no longer supported in Visual
Basic 2005
EndIf GoSub Let Variant Wend
6 Sub Main()
Fig. 3.3 | Creating a Console Application with the New Project dialog.
Fig. 3.12 | Executing the program shown in Fig. 3.1 from a Command Prompt.
Fig. 3.13 | Executing the program shown in Fig. 3.1 from a Command Prompt.
• Variables
– Location in memory that stores a value
• Declare with name and type before use
– In most cases, uses keyword Dim when declaring variables
– Variable name: any valid identifier
• Initialize variable in its declaration
– Equal sign
– Assignment statement
• Calculates sum of number1 and number2 (right hand side)
• Uses assignment operator = to assign result to variable sum
• Read as: sum gets the value of number1 + number2
• number1 and number2 are operands
Primitive Types
• Variables
– Every variable has a name, a type, a size and a value
• Name corresponds to location in memory
– When new value is placed into a variable, replaces (and
destroys) previous value
– Reading variables from memory does not change them
Fig. 3.19 | Memory location showing name and value of variable number1.
Fig. 3.20 | Memory locations after values for variables number1 and number2
have been input.
3.7 Arithmetic
Addition + f+7 f + 7
Subtraction – p–c p - c
Multiplication * bm b * m
x
/ x y or or x÷y x / y
Division (floating point) y
Exponentiation ^ qp q ^ p
Unary minus - –e –e
Unary plus + +g +g
• Operator precedence
– Some arithmetic operators act before others (i.e.,
multiplication before addition)
• Use parenthesis when needed or for clarity
– Example: Find the average of three variables a, b and c
• Do not use: a + b + c / 3
• Use: ( a + b + c ) / 3
Equality operators
= x = y x is equal to y
Operators Type
^ exponentiation
+ - sign operations (unary)
* / multiplication and floating-point division
\ Integer division
Mod modulus
+ - addition and subtraction (binary)
= <> < <= > >= equality and relational
Fig. 3.30 | Obtaining documentation for a class by using the Index dialog.
Fig. 3.33 | Adding an assembly reference to a project in the Visual Basic 2005 Express IDE.
Fig. 3.38 | Use case diagram for the ATM system from the user’s perspective.
Fig. 3.39 | Use case diagram for a modified version of our ATM system that also allows
users to transfer money between accounts.