SlideShare ist ein Scribd-Unternehmen logo
1 von 43
© 2002 Prentice Hall. All rights reserved.
1
Outline
3.1 Introduction
3.2 Simple Program: Printing a Line of Text
3.3 Another Simple Program: Adding Integers
3.4 Memory Concepts
3.5 Arithmetic
3.6 Decision Making: Equality and Relational Operators
3.7 Using a Dialog to Display a Message
Chapter 3 – Introduction to Visual Basic
Programming
© 2002 Prentice Hall. All rights reserved.
2
3.1 Introduction
• In this chapter we introduce
– Visual Basic programming
• We present examples that illustrate several important features
of the language
– Console applications
• Applications that contain only text output
• Output is displayed in a command window
© 2002 Prentice Hall. All rights reserved.
3
3.2 Simple Program: Printing a Line of Text
• Simple program that displays a line of text
• When the program is run
– output appears in a command window
• It illustrates important Visual Basic features
– Comments
– Modules
– Sub procedures
© 2002 Prentice Hall.
All rights reserved.
Outline
4
Welcome1.vb
Program Output
1 ' Fig. 3.1: Welcome1.vb
2 ' Simple Visual Basic program.
3
4 Module modFirstWelcome
5
6 Sub Main()
7 Console.WriteLine("Welcome to Visual Basic!")
8 End Sub ' Main
9
10 End Module ' modFirstWelcome
Welcome to Visual Basic!
Single-quote character (')
indicates that the remainder
of the line is a comment
Visual Basic console
applications consist of pieces
called modules
The Main procedure is the entry
point of the program. It is present
in all console applications
The Console.WriteLine
statement displays text output
to the console
•A few Good Programming Practices
– Comments
• Every program should begin with one or more comments
– Modules
• Begin each module with mod to make modules easier to identify
– Procedures
• Indent the entire body of each procedure definition one “level” of
indentation
© 2002 Prentice Hall. All rights reserved.
5
3.2 Simple Program: Printing a Line of Text
• Now a short step-by-step explanation of how to
create and run this program using the features of
Visual Studio .NET IDE…
© 2002 Prentice Hall. All rights reserved.
6
3.2 Simple Program: Printing a Line of Text
1. Create the console application
– Select File > New > Project…
– In the left pane, select Visual Basic Projects
– In the right pane, select Console Application
– Name the project Welcome1
– Specify the desired location
2. Change the name of the program file
– Click Module1.vb in the Solution Explorer window
– In the Properties window, change the File Name
property to Welcome1.vb
© 2002 Prentice Hall. All rights reserved.
7
3.2 Simple Program: Printing a Line of Text
Fig. 3.2 Creating a Console Application with the New Project dialog.
Left pane Right pane
Project
name
File
location
© 2002 Prentice Hall. All rights reserved.
8
3.2 Simple Program: Printing a Line of Text
Fig. 3.3 IDE with an open console application.
Editor window (containing
program code)
© 2002 Prentice Hall. All rights reserved.
9
3.2 Simple Program: Printing a Line of Text
Fig. 3.4 Renaming the program file in the Properties window.
Solution Explorer
File Name
property
Click Module1.vb to
display its properties
Properties window
© 2002 Prentice Hall. All rights reserved.
10
3.2 Simple Program: Printing a Line of Text
3. Change the name of the module
– Module names must be modified in the editor window
– Replace the identifier Module1 with
modFirstWelcome
4. Writing code
– Type the code contained in line 7 of Fig. 3.1 between Sub
Main() and End Sub
• Note that after typing the class name and the dot operator the
IntelliSense is displayed. It lists a class’s members.
• Note that when typing the text between the parenthesis
(parameter), the Parameter Info and Parameter List windows
are displayed
© 2002 Prentice Hall. All rights reserved.
11
3.2 Simple Program: Printing a Line of Text
5. Run the program
– To compile, select Build > Build Solution
• This creates a new file, named Welcome1.exe
– To run, select Debug > Start Without Debugging
© 2002 Prentice Hall. All rights reserved.
12
3.2 Simple Program: Printing a Line of Text
Fig. 3.5 IntelliSense feature of the Visual Studio .NET IDE.
Partially-typed member Member list
Description of
highlighted member
© 2002 Prentice Hall. All rights reserved.
13
3.2 Simple Program: Printing a Line of Text
Fig. 3.6 Parameter Info and Parameter List windows.
Up arrow Down arrow
Parameter List window
Parameter Info window
© 2002 Prentice Hall. All rights reserved.
14
3.2 Simple Program: Printing a Line of Text
Fig. 3.7 Executing the program shown in Fig. 3.1.
Command window prompts the user to
press a key after the program terminates
© 2002 Prentice Hall. All rights reserved.
15
3.2 Simple Program: Printing a Line of Text
Fig. 3.8 IDE indicating a syntax error.
Omitted parenthesis character
(syntax error)
Blue underline
indicates a
syntax error
Task List window
Error description(s)
© 2002 Prentice Hall.
All rights reserved.
Outline
16
Welcome2.vb
Program Output
1 ' Fig. 3.9: Welcome2.vb
2 ' Writing line of text with multiple statements.
3
4 Module modSecondWelcome
5
6 Sub Main()
7 Console.Write("Welcome to ")
8 Console.WriteLine("Visual Basic!")
9 End Sub ' Main
11
12 End Module ' modSecondWelcome
Welcome to Visual Basic!
Method Write does not position the output
cursor at the beginning of the next line
Method WriteLine positions the output
cursor at the beginning of the next line
© 2002 Prentice Hall. All rights reserved.
17
3.3 Another Simple Program: Adding
Integers
• User input two integers
– Whole numbers
• Program computes the sum
• Display result
© 2002 Prentice Hall.
All rights reserved.
Outline
18
Addition.vb
1 ' Fig. 3.10: Addition.vb
2 ' Addition program.
3
4 Module modAddition
5
6 Sub Main()
7
8 ' variables for storing user input
9 Dim firstNumber, secondNumber As String
10
11 ' variables used in addition calculation
12 Dim number1, number2, sumOfNumbers As Integer
13
14 ' read first number from user
15 Console.Write("Please enter the first integer: ")
16 firstNumber = Console.ReadLine()
17
18 ' read second number from user
19 Console.Write("Please enter the second integer: ")
20 secondNumber = Console.ReadLine()
21
22 ' convert input values to Integers
23 number1 = firstNumber
24 number2 = secondNumber
25
26 sumOfNumbers = number1 + number2 ' add numbers
27
28 ' display results
29 Console.WriteLine("The sum is {0}", sumOfNumbers)
30
31 End Sub ' Main
32
33 End Module ' modAddition
Declarations begin with keyword Dim
These variables store strings of characters
These variables store integers values
First value entered by user is assigned
to variable firstNumber
Method ReadLine causes program
to pause and wait for user input
Implicit conversion from String to
Integer
Sums integers and assigns result to
variable sumOfNumbers
Format indicates that the argument
after the string will be evaluated and
incorporated into the string
© 2002 Prentice Hall.
All rights reserved.
Outline
19
Addition.vb
Please enter the first integer: 45
Please enter the second integer: 72
The sum is 117
© 2002 Prentice Hall. All rights reserved.
20
3.3 Another Simple Program: Adding
Integers
Fig. 3.11 Dialog displaying a run-time error.
If the user types a non-integer value, such
as “hello,” a run-time error occurs
© 2002 Prentice Hall. All rights reserved.
21
3.4 Memory Concepts
• Variables
– correspond to actual locations in the computer’s memory
– Every variable has a
• Name
• Type
• Size
• value
– A value placed in a memory location replaces the value
previously stored
• The previous value is destroyed
– When value is read from a memory location, it is not
destroyed
© 2002 Prentice Hall. All rights reserved.
22
3.4 Memory Concepts
Fig. 3.12 Memory location showing name and value of variable number1.
Fig. 3.13 Memory locations after values for variables number1 and number2 have been input.
45number1
45
45
number1
number2
© 2002 Prentice Hall. All rights reserved.
23
3.5 Arithmetic
• Arithmetic operators
– Visual Basic use various special symbols not used in algebra
• Asterisk (*), keyword Mod
– Binary operators
• Operates using two operands
– sum + value
– Unary operators
• Operators that take only one operand
– +9, -19
© 2002 Prentice Hall. All rights reserved.
24
3.5 Arithmetic
Fig. 3.14 Memory locations after an addition operation.
45
45
number1
number2
sumOfNumbers 45
© 2002 Prentice Hall. All rights reserved.
25
3.5 Arithmetic
• Integer division
– Uses the backslash, 
– 7  4 evaluates to 1
• Floating-point division
– Uses the forward slash, /
– 7 / 4 evaluates to 1.75
• Modulus operator, Mod
– Yields the remainder after Integer division
– 7 Mod 4 yields 3
© 2002 Prentice Hall. All rights reserved.
26
3.5 Arithmetic
Visual Basic operation Arithmetic operator Algebraic expression Visual Basic
expression
Addition + f + 7 f + 7
Subtraction – p – c p - c
Multiplication * bm b * m
Division (float) / x / y or <Anchor10> or
x y
x / y
Division (Integer)  none v  u
Modulus % r mod s r Mod s
Exponentiation ^ q p q
^ p
Unary Negative - –e –e
Unary Positive + +g +g
Fig. 3.14 Arithmetic operators.
Fig. 3.14 Arithmetic Operators.
© 2002 Prentice Hall. All rights reserved.
27
3.5 Arithmetic
• Rules of operator precedence
1. Operators in expressions contained within parentheses
2. Exponentiation
3. Unary positive and negative
4. Multiplication and floating-point division
5. Integer division
6. Modulus operations
7. Addition and subtraction operations
© 2002 Prentice Hall. All rights reserved.
28
3.5 Arithmetic
Operator(s) Operation Order of evaluation (precedence)
( ) Parentheses Evaluated first. If the parentheses are nested, the
expression in the innermost pair is evaluated first. If
there are several pairs of parentheses “on the same
level” (i.e., not nested), they are evaluated from left
to right.
^ Exponentiation Evaluated second. If there are several such operators,
they are evaluated from left to right.
+, – Sign operations Evaluated third. If there are several such operators,
they are evaluated from left to right.
*, / Multiplication and
Division
Evaluated fourth. If there are several such operators,
they are evaluated from left to right.
 Integer
division
Evaluated fifth. If there are several such operators,
they are evaluated from left to right.
Mod Modulus Evaluated sixth. If there are several such operators,
they are evaluated from left to right.
+, – Addition and
Subtraction
Evaluated last. If there are several such operators,
they are evaluated from left to right.
Fig. 3.15 Precedence of arithmetic operators.
Fig. 3.15 Precedence of arithmetic operators.
© 2002 Prentice Hall. All rights reserved.
29
3.5 Arithmetic
Fig. 3.16 Order in which a second-degree polynomial is evaluated.
Step 1.
Step 2.
Step 5.
Step 3.
Step 4.
Step 6.
y = 2 * 5 * 5 + 3 * 5 + 7
2 * 5 is 10 (Leftmost multiplication)
y = 10 * 5 + 3 * 5 + 7
10 * 5 is 50 (Leftmost multiplication)
y = 50 + 3 * 5 + 7
3 * 5 is 15 (Multiplication before addition)
y = 50 + 15 + 7
50 + 15 is 65 (Leftmost addition)
y = 65 + 7
65 + 7 is 72 (Last addition)
y = 72 (Last operation—place 72 into y)
© 2002 Prentice Hall. All rights reserved.
30
3.6 Decision Making: Equality and
Relational Operators
• If/Then structure
– Allows a program to make decision based on the truth or
falsity of some expression
– Condition
• The expression in an If/Then structure
– If the condition is true, the statement in the body of the
structure executes
– Conditions can be formed by using
• Equality operators
• Relational operators
© 2002 Prentice Hall. All rights reserved.
31
3.6 Decision Making: Equality and
Relational Operators
Standard algebraic
equality operator or
relational operator
Visual Basic
equality
or relational
operator
Example
of Visual Basic
condition
Meaning of
Visual Basic condition
Equality
operators
= = x = y x is equal to y
<> x <> y x is not equal to y
Relational
operators
> > x > y x is greater than y
< < x < y x is less than y
>= x >= y x is greater than or equal to y
? <= x <= y x is less than or equal to y
Fig. 3.17 Equality and relational operators.
Fig. 3.17 Equality and relational operators.
© 2002 Prentice Hall.
All rights reserved.
Outline
32
Comparison.vb
1 ' Fig. 3.19: Comparison.vb
2 ' Using equality and relational operators.
3
4 Module modComparison
5
6 Sub Main()
7
8 ' declare Integer variables for user input
9 Dim number1, number2 As Integer
10
11 ' read first number from user
12 Console.Write("Please enter first integer: ")
13 number1 = Console.ReadLine()
14
15 ' read second number from user
16 Console.Write("Please enter second integer: ")
17 number2 = Console.ReadLine()
18
19 If (number1 = number2) Then
20 Console.WriteLine("{0} = {1}", number1, number2)
21 End If
22
23 If (number1 <> number2) Then
24 Console.WriteLine("{0} <> {1}", number1, number2)
25 End If
26
27 If (number1 < number2) Then
28 Console.WriteLine("{0} < {1}", number1, number2)
29 End If
30
31 If (number1 > number2) Then
32 Console.WriteLine("{0} > {1}", number1, number2)
33 End If
Variables of the same type may
be declared in one declaration
The If/Then structure compares the values
of number1 and number2 for equality
© 2002 Prentice Hall.
All rights reserved.
Outline
33
Comparison.vb
Program Output
34
35 If (number1 <= number2) Then
36 Console.WriteLine("{0} <= {1}", number1, number2)
37 End If
38
39 If (number1 >= number2) Then
40 Console.WriteLine("{0} >= {1}", number1, number2)
41 End If
42
43 End Sub ' Main
44
45 End Module ' modComparison
Please enter first integer: 1000
Please enter second integer: 2000
1000 <> 2000
1000 < 2000
1000 <= 2000
Please enter first integer: 515
Please enter second integer: 49
515 <> 49
515 > 49
515 >= 49
Please enter first integer: 333
Please enter second integer: 333
333 = 333
333 <= 333
333 >= 333
© 2002 Prentice Hall. All rights reserved.
34
3.6 Decision Making: Equality and
Relational Operators
Operators Associativity Type
() left to right parentheses
^ left to right exponentiation
* / left to right multiplicative
 left to right integer division
Mod left to right modulus
+ - left to right additive
= <> < <= > >= left to right equality and relational
Fig. 3.19 Precedence and associativity of operators introduced in this chapter.
Fig. 3.19 Precedence and associativity of operators introduced in this chapter.
© 2002 Prentice Hall. All rights reserved.
35
3.7 Using a Dialog to Display a Message
• Dialogs
– Windows that typically display messages to the user
– Visual Basic provides class MessageBox for creating
dialogs
© 2002 Prentice Hall.
All rights reserved.
Outline
36
SquareRoot.vb
Program Output
1 ' Fig. 3.20: SquareRoot.vb
2 ' Displaying square root of 2 in dialog.
3
4 Imports System.Windows.Forms ' Namespace containing MessageBox
5
6 Module modSquareRoot
7
8 Sub Main()
9
10 ' Calculate square root of 2
11 Dim root As Double = Math.Sqrt(2)
12
13 ' Display results in dialog
14 MessageBox.Show("The square root of 2 is " & root, _
15 "The Square Root of 2")
16 End Sub ' Main
17
18 End Module ' modThirdWelcome
Sqrt method of the Math class is called
to compute the square root of 2
The Double data type stores floating-
point numbers
Method Show of class MessageBox
Line-continuation character
Empty command
window
© 2002 Prentice Hall. All rights reserved.
37
3.7 Using a Dialog to Display a Message
Fig. 3.21 Dialog displayed by calling MessageBox.Show.
Title bar
Close box
Mouse pointer
Dialog sized to
accommodate
contents.
OK button allows
the user to dismiss
the dialog.
© 2002 Prentice Hall. All rights reserved.
38
3.7 Using a Dialog to Display a Message
• Assembly
– File that contain many classes provided by Visual Basic
– These files have a .dll (or dynamic link library) extension.
– Example
• Class MessageBox is located in assembly
System.Windows.Forms.dll
• MSDN Documentation
– Information about the assembly that we need can be found in
the MSDN documentation
– Select Help > Index… to display the Index dialog
© 2002 Prentice Hall. All rights reserved.
39
3.7 Using a Dialog to Display a Message
Fig. 3.22 Obtaining documentation for a class by using the Index dialog.
Search string
Filter
Link to MessageBox
documentation
© 2002 Prentice Hall. All rights reserved.
40
3.7 Using a Dialog to Display a Message
Fig. 3.23 Documentation for the MessageBox class.
Requirements
section heading
MessageBox class
documentation
Assembly containing
class MessageBox
© 2002 Prentice Hall. All rights reserved.
41
3.7 Using a Dialog to Display a Message
• Reference
– It is necessary to add a reference to the assembly if you wish
to use its classes
– Example
• To use class MessageBox it is necessary to add a reference
to System.Windows.Forms
• Imports
– Forgetting to add an Imports statement for a referenced
assembly is a syntax error
© 2002 Prentice Hall. All rights reserved.
42
3.7 Using a Dialog to Display a Message
Fig. 3.24 Adding a reference to an assembly in the Visual Studio .NET IDE.
References
folder
(expanded)
Solution Explorer
before reference is
added
Solution Explorer
after reference is
added
System.Windows.Forms
reference
© 2002 Prentice Hall. All rights reserved.
43
3.7 Using a Dialog to Display a Message
Fig. 3.25 Internet Explorer window with GUI components.
Label
Button (displaying
an icon) Menu (e.g., Help) Text box Menu bar

Weitere ähnliche Inhalte

Was ist angesagt?

Program Logic Formulation - Ohio State University
Program Logic Formulation - Ohio State UniversityProgram Logic Formulation - Ohio State University
Program Logic Formulation - Ohio State UniversityReggie Niccolo Santos
 
Book management system
Book management systemBook management system
Book management systemSHARDA SHARAN
 
Ch2 C Fundamentals
Ch2 C FundamentalsCh2 C Fundamentals
Ch2 C FundamentalsSzeChingChen
 
Introduction to Visual Basic 6.0 Fundamentals
Introduction to Visual Basic 6.0 FundamentalsIntroduction to Visual Basic 6.0 Fundamentals
Introduction to Visual Basic 6.0 FundamentalsSanay Kumar
 
Problem Solving Techniques and Introduction to C
Problem Solving Techniques and Introduction to CProblem Solving Techniques and Introduction to C
Problem Solving Techniques and Introduction to CPrabu U
 
Refinery Blending Problems by Engr. Adefami Olusegun
Refinery Blending Problems by Engr. Adefami OlusegunRefinery Blending Problems by Engr. Adefami Olusegun
Refinery Blending Problems by Engr. Adefami OlusegunEngr. Adefami Segun, MNSE
 
Assembler - System Programming
Assembler - System ProgrammingAssembler - System Programming
Assembler - System ProgrammingRadhika Talaviya
 
Visual basic ppt for tutorials computer
Visual basic ppt for tutorials computerVisual basic ppt for tutorials computer
Visual basic ppt for tutorials computersimran153
 
Visual basic
Visual basicVisual basic
Visual basicDharmik
 
Software Engineering
Software EngineeringSoftware Engineering
Software Engineeringpoonam.rwalia
 
Control Flow Analysis
Control Flow AnalysisControl Flow Analysis
Control Flow AnalysisEdgar Barbosa
 
Computer programming all chapters
Computer programming all chaptersComputer programming all chapters
Computer programming all chaptersIbrahim Elewah
 
Solutions manual for c++ programming from problem analysis to program design ...
Solutions manual for c++ programming from problem analysis to program design ...Solutions manual for c++ programming from problem analysis to program design ...
Solutions manual for c++ programming from problem analysis to program design ...Warren0989
 
Getting started with Keil uVision 2020
Getting started with Keil uVision 2020Getting started with Keil uVision 2020
Getting started with Keil uVision 2020SaravananVijayakumar4
 
Refactoring And Unit Testing
Refactoring And Unit TestingRefactoring And Unit Testing
Refactoring And Unit Testingliufabin 66688
 

Was ist angesagt? (20)

Program Logic Formulation - Ohio State University
Program Logic Formulation - Ohio State UniversityProgram Logic Formulation - Ohio State University
Program Logic Formulation - Ohio State University
 
Vb introduction.
Vb introduction.Vb introduction.
Vb introduction.
 
Book management system
Book management systemBook management system
Book management system
 
Ch2 C Fundamentals
Ch2 C FundamentalsCh2 C Fundamentals
Ch2 C Fundamentals
 
Chapter 02 - Problem Solving
Chapter 02 - Problem SolvingChapter 02 - Problem Solving
Chapter 02 - Problem Solving
 
Introduction to Visual Basic 6.0 Fundamentals
Introduction to Visual Basic 6.0 FundamentalsIntroduction to Visual Basic 6.0 Fundamentals
Introduction to Visual Basic 6.0 Fundamentals
 
Problem Solving Techniques and Introduction to C
Problem Solving Techniques and Introduction to CProblem Solving Techniques and Introduction to C
Problem Solving Techniques and Introduction to C
 
Refinery Blending Problems by Engr. Adefami Olusegun
Refinery Blending Problems by Engr. Adefami OlusegunRefinery Blending Problems by Engr. Adefami Olusegun
Refinery Blending Problems by Engr. Adefami Olusegun
 
Assembler - System Programming
Assembler - System ProgrammingAssembler - System Programming
Assembler - System Programming
 
Visual basic ppt for tutorials computer
Visual basic ppt for tutorials computerVisual basic ppt for tutorials computer
Visual basic ppt for tutorials computer
 
Visual basic
Visual basicVisual basic
Visual basic
 
Software Engineering
Software EngineeringSoftware Engineering
Software Engineering
 
Control Flow Analysis
Control Flow AnalysisControl Flow Analysis
Control Flow Analysis
 
Computer programming all chapters
Computer programming all chaptersComputer programming all chapters
Computer programming all chapters
 
VB.Net GUI Unit_01
VB.Net GUI Unit_01VB.Net GUI Unit_01
VB.Net GUI Unit_01
 
Solutions manual for c++ programming from problem analysis to program design ...
Solutions manual for c++ programming from problem analysis to program design ...Solutions manual for c++ programming from problem analysis to program design ...
Solutions manual for c++ programming from problem analysis to program design ...
 
Getting started with Keil uVision 2020
Getting started with Keil uVision 2020Getting started with Keil uVision 2020
Getting started with Keil uVision 2020
 
Chapter 4 5
Chapter 4 5Chapter 4 5
Chapter 4 5
 
Refactoring And Unit Testing
Refactoring And Unit TestingRefactoring And Unit Testing
Refactoring And Unit Testing
 
Vp lecture1 ararat
Vp lecture1 araratVp lecture1 ararat
Vp lecture1 ararat
 

Andere mochten auch

Tutorial mikrotik-1
Tutorial mikrotik-1Tutorial mikrotik-1
Tutorial mikrotik-1dhi her
 
Ebooktiketkapal
EbooktiketkapalEbooktiketkapal
Ebooktiketkapaldhi her
 
Modulpelatihanmikrotik
ModulpelatihanmikrotikModulpelatihanmikrotik
Modulpelatihanmikrotikdhi her
 
Mikrotikrb750modulbeta
Mikrotikrb750modulbetaMikrotikrb750modulbeta
Mikrotikrb750modulbetadhi her
 
Dasarpemrogramanvisualbasic
DasarpemrogramanvisualbasicDasarpemrogramanvisualbasic
Dasarpemrogramanvisualbasicdhi her
 
Dasarpemrogramanvisualbasic
DasarpemrogramanvisualbasicDasarpemrogramanvisualbasic
Dasarpemrogramanvisualbasicdhi her
 
Setting buat-router-mikrotik-
Setting buat-router-mikrotik-Setting buat-router-mikrotik-
Setting buat-router-mikrotik-dhi her
 
Uu no-13-tahun-2003
Uu no-13-tahun-2003Uu no-13-tahun-2003
Uu no-13-tahun-2003dhi her
 

Andere mochten auch (9)

Tutorial mikrotik-1
Tutorial mikrotik-1Tutorial mikrotik-1
Tutorial mikrotik-1
 
Ebooktiketkapal
EbooktiketkapalEbooktiketkapal
Ebooktiketkapal
 
Modulpelatihanmikrotik
ModulpelatihanmikrotikModulpelatihanmikrotik
Modulpelatihanmikrotik
 
Mikrotikrb750modulbeta
Mikrotikrb750modulbetaMikrotikrb750modulbeta
Mikrotikrb750modulbeta
 
Dasarpemrogramanvisualbasic
DasarpemrogramanvisualbasicDasarpemrogramanvisualbasic
Dasarpemrogramanvisualbasic
 
№123
№123№123
№123
 
Dasarpemrogramanvisualbasic
DasarpemrogramanvisualbasicDasarpemrogramanvisualbasic
Dasarpemrogramanvisualbasic
 
Setting buat-router-mikrotik-
Setting buat-router-mikrotik-Setting buat-router-mikrotik-
Setting buat-router-mikrotik-
 
Uu no-13-tahun-2003
Uu no-13-tahun-2003Uu no-13-tahun-2003
Uu no-13-tahun-2003
 

Ähnlich wie Visual Basic Programming Fundamentals

Chapter0002222programming language2.pptx
Chapter0002222programming language2.pptxChapter0002222programming language2.pptx
Chapter0002222programming language2.pptxstephen972973
 
Csphtp1 03
Csphtp1 03Csphtp1 03
Csphtp1 03HUST
 
Basic Programming concepts - Programming with C++
Basic Programming concepts - Programming with C++Basic Programming concepts - Programming with C++
Basic Programming concepts - Programming with C++Mohamed El Desouki
 
COM 211 PRESENTATION.pptx
COM 211 PRESENTATION.pptxCOM 211 PRESENTATION.pptx
COM 211 PRESENTATION.pptxAnasYunusa
 
Background Tasks with Worker Service
Background Tasks with Worker ServiceBackground Tasks with Worker Service
Background Tasks with Worker Servicessusere19c741
 
PT1420 File Access and Visual Basic .docx
PT1420 File Access and Visual Basic                      .docxPT1420 File Access and Visual Basic                      .docx
PT1420 File Access and Visual Basic .docxamrit47
 
Algorithm and c language
Algorithm and c languageAlgorithm and c language
Algorithm and c languagekamalbeydoun
 
C++ advanced PPT.pdf
C++ advanced PPT.pdfC++ advanced PPT.pdf
C++ advanced PPT.pdfDinashMaliya3
 
2-Vb.net Basic Concepts and structure of .net code.pptx
2-Vb.net Basic Concepts and structure of .net code.pptx2-Vb.net Basic Concepts and structure of .net code.pptx
2-Vb.net Basic Concepts and structure of .net code.pptxUsama182938
 
Whats new in visual studio 2017
Whats new in visual studio 2017Whats new in visual studio 2017
Whats new in visual studio 2017Md. Mahedee Hasan
 

Ähnlich wie Visual Basic Programming Fundamentals (20)

Chapter03_PPT.ppt
Chapter03_PPT.pptChapter03_PPT.ppt
Chapter03_PPT.ppt
 
Chapter0002222programming language2.pptx
Chapter0002222programming language2.pptxChapter0002222programming language2.pptx
Chapter0002222programming language2.pptx
 
Csphtp1 03
Csphtp1 03Csphtp1 03
Csphtp1 03
 
Vb6.0 intro
Vb6.0 introVb6.0 intro
Vb6.0 intro
 
Basic Programming concepts - Programming with C++
Basic Programming concepts - Programming with C++Basic Programming concepts - Programming with C++
Basic Programming concepts - Programming with C++
 
COM 211 PRESENTATION.pptx
COM 211 PRESENTATION.pptxCOM 211 PRESENTATION.pptx
COM 211 PRESENTATION.pptx
 
2621008 - C++ 1
2621008 -  C++ 12621008 -  C++ 1
2621008 - C++ 1
 
Ms vb
Ms vbMs vb
Ms vb
 
Cocomomodel
CocomomodelCocomomodel
Cocomomodel
 
COCOMO Model
COCOMO ModelCOCOMO Model
COCOMO Model
 
Cocomo model
Cocomo modelCocomo model
Cocomo model
 
Background Tasks with Worker Service
Background Tasks with Worker ServiceBackground Tasks with Worker Service
Background Tasks with Worker Service
 
Chapter 1.ppt
Chapter 1.pptChapter 1.ppt
Chapter 1.ppt
 
intro to c
intro to cintro to c
intro to c
 
PT1420 File Access and Visual Basic .docx
PT1420 File Access and Visual Basic                      .docxPT1420 File Access and Visual Basic                      .docx
PT1420 File Access and Visual Basic .docx
 
Algorithm and c language
Algorithm and c languageAlgorithm and c language
Algorithm and c language
 
C++ advanced PPT.pdf
C++ advanced PPT.pdfC++ advanced PPT.pdf
C++ advanced PPT.pdf
 
2-Vb.net Basic Concepts and structure of .net code.pptx
2-Vb.net Basic Concepts and structure of .net code.pptx2-Vb.net Basic Concepts and structure of .net code.pptx
2-Vb.net Basic Concepts and structure of .net code.pptx
 
Whats new in visual studio 2017
Whats new in visual studio 2017Whats new in visual studio 2017
Whats new in visual studio 2017
 
Session1 c1
Session1 c1Session1 c1
Session1 c1
 

Mehr von dhi her

Panduansettingdialupspeedydenganroutermikrotik
PanduansettingdialupspeedydenganroutermikrotikPanduansettingdialupspeedydenganroutermikrotik
Panduansettingdialupspeedydenganroutermikrotikdhi her
 
Modulpelatihanp4tkmikrotik
Modulpelatihanp4tkmikrotikModulpelatihanp4tkmikrotik
Modulpelatihanp4tkmikrotikdhi her
 
Manualmikrotik
ManualmikrotikManualmikrotik
Manualmikrotikdhi her
 
Tutorial mikrotik-2
Tutorial mikrotik-2Tutorial mikrotik-2
Tutorial mikrotik-2dhi her
 
Vbtutorial
VbtutorialVbtutorial
Vbtutorialdhi her
 
Vbreport program
Vbreport programVbreport program
Vbreport programdhi her
 
Practicalfileofvb workshop
Practicalfileofvb workshopPracticalfileofvb workshop
Practicalfileofvb workshopdhi her
 
Ebookrentalfilm
EbookrentalfilmEbookrentalfilm
Ebookrentalfilmdhi her
 

Mehr von dhi her (8)

Panduansettingdialupspeedydenganroutermikrotik
PanduansettingdialupspeedydenganroutermikrotikPanduansettingdialupspeedydenganroutermikrotik
Panduansettingdialupspeedydenganroutermikrotik
 
Modulpelatihanp4tkmikrotik
Modulpelatihanp4tkmikrotikModulpelatihanp4tkmikrotik
Modulpelatihanp4tkmikrotik
 
Manualmikrotik
ManualmikrotikManualmikrotik
Manualmikrotik
 
Tutorial mikrotik-2
Tutorial mikrotik-2Tutorial mikrotik-2
Tutorial mikrotik-2
 
Vbtutorial
VbtutorialVbtutorial
Vbtutorial
 
Vbreport program
Vbreport programVbreport program
Vbreport program
 
Practicalfileofvb workshop
Practicalfileofvb workshopPracticalfileofvb workshop
Practicalfileofvb workshop
 
Ebookrentalfilm
EbookrentalfilmEbookrentalfilm
Ebookrentalfilm
 

Kürzlich hochgeladen

Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
General AI for Medical Educators April 2024
General AI for Medical Educators April 2024General AI for Medical Educators April 2024
General AI for Medical Educators April 2024Janet Corral
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxVishalSingh1417
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajanpragatimahajan3
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Disha Kariya
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphThiyagu K
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpinRaunakKeshri1
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAssociation for Project Management
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...Sapna Thakur
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room servicediscovermytutordmt
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingTeacherCyreneCayanan
 

Kürzlich hochgeladen (20)

Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
General AI for Medical Educators April 2024
General AI for Medical Educators April 2024General AI for Medical Educators April 2024
General AI for Medical Educators April 2024
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajan
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpin
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room service
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writing
 

Visual Basic Programming Fundamentals

  • 1. © 2002 Prentice Hall. All rights reserved. 1 Outline 3.1 Introduction 3.2 Simple Program: Printing a Line of Text 3.3 Another Simple Program: Adding Integers 3.4 Memory Concepts 3.5 Arithmetic 3.6 Decision Making: Equality and Relational Operators 3.7 Using a Dialog to Display a Message Chapter 3 – Introduction to Visual Basic Programming
  • 2. © 2002 Prentice Hall. All rights reserved. 2 3.1 Introduction • In this chapter we introduce – Visual Basic programming • We present examples that illustrate several important features of the language – Console applications • Applications that contain only text output • Output is displayed in a command window
  • 3. © 2002 Prentice Hall. All rights reserved. 3 3.2 Simple Program: Printing a Line of Text • Simple program that displays a line of text • When the program is run – output appears in a command window • It illustrates important Visual Basic features – Comments – Modules – Sub procedures
  • 4. © 2002 Prentice Hall. All rights reserved. Outline 4 Welcome1.vb Program Output 1 ' Fig. 3.1: Welcome1.vb 2 ' Simple Visual Basic program. 3 4 Module modFirstWelcome 5 6 Sub Main() 7 Console.WriteLine("Welcome to Visual Basic!") 8 End Sub ' Main 9 10 End Module ' modFirstWelcome Welcome to Visual Basic! Single-quote character (') indicates that the remainder of the line is a comment Visual Basic console applications consist of pieces called modules The Main procedure is the entry point of the program. It is present in all console applications The Console.WriteLine statement displays text output to the console •A few Good Programming Practices – Comments • Every program should begin with one or more comments – Modules • Begin each module with mod to make modules easier to identify – Procedures • Indent the entire body of each procedure definition one “level” of indentation
  • 5. © 2002 Prentice Hall. All rights reserved. 5 3.2 Simple Program: Printing a Line of Text • Now a short step-by-step explanation of how to create and run this program using the features of Visual Studio .NET IDE…
  • 6. © 2002 Prentice Hall. All rights reserved. 6 3.2 Simple Program: Printing a Line of Text 1. Create the console application – Select File > New > Project… – In the left pane, select Visual Basic Projects – In the right pane, select Console Application – Name the project Welcome1 – Specify the desired location 2. Change the name of the program file – Click Module1.vb in the Solution Explorer window – In the Properties window, change the File Name property to Welcome1.vb
  • 7. © 2002 Prentice Hall. All rights reserved. 7 3.2 Simple Program: Printing a Line of Text Fig. 3.2 Creating a Console Application with the New Project dialog. Left pane Right pane Project name File location
  • 8. © 2002 Prentice Hall. All rights reserved. 8 3.2 Simple Program: Printing a Line of Text Fig. 3.3 IDE with an open console application. Editor window (containing program code)
  • 9. © 2002 Prentice Hall. All rights reserved. 9 3.2 Simple Program: Printing a Line of Text Fig. 3.4 Renaming the program file in the Properties window. Solution Explorer File Name property Click Module1.vb to display its properties Properties window
  • 10. © 2002 Prentice Hall. All rights reserved. 10 3.2 Simple Program: Printing a Line of Text 3. Change the name of the module – Module names must be modified in the editor window – Replace the identifier Module1 with modFirstWelcome 4. Writing code – Type the code contained in line 7 of Fig. 3.1 between Sub Main() and End Sub • Note that after typing the class name and the dot operator the IntelliSense is displayed. It lists a class’s members. • Note that when typing the text between the parenthesis (parameter), the Parameter Info and Parameter List windows are displayed
  • 11. © 2002 Prentice Hall. All rights reserved. 11 3.2 Simple Program: Printing a Line of Text 5. Run the program – To compile, select Build > Build Solution • This creates a new file, named Welcome1.exe – To run, select Debug > Start Without Debugging
  • 12. © 2002 Prentice Hall. All rights reserved. 12 3.2 Simple Program: Printing a Line of Text Fig. 3.5 IntelliSense feature of the Visual Studio .NET IDE. Partially-typed member Member list Description of highlighted member
  • 13. © 2002 Prentice Hall. All rights reserved. 13 3.2 Simple Program: Printing a Line of Text Fig. 3.6 Parameter Info and Parameter List windows. Up arrow Down arrow Parameter List window Parameter Info window
  • 14. © 2002 Prentice Hall. All rights reserved. 14 3.2 Simple Program: Printing a Line of Text Fig. 3.7 Executing the program shown in Fig. 3.1. Command window prompts the user to press a key after the program terminates
  • 15. © 2002 Prentice Hall. All rights reserved. 15 3.2 Simple Program: Printing a Line of Text Fig. 3.8 IDE indicating a syntax error. Omitted parenthesis character (syntax error) Blue underline indicates a syntax error Task List window Error description(s)
  • 16. © 2002 Prentice Hall. All rights reserved. Outline 16 Welcome2.vb Program Output 1 ' Fig. 3.9: Welcome2.vb 2 ' Writing line of text with multiple statements. 3 4 Module modSecondWelcome 5 6 Sub Main() 7 Console.Write("Welcome to ") 8 Console.WriteLine("Visual Basic!") 9 End Sub ' Main 11 12 End Module ' modSecondWelcome Welcome to Visual Basic! Method Write does not position the output cursor at the beginning of the next line Method WriteLine positions the output cursor at the beginning of the next line
  • 17. © 2002 Prentice Hall. All rights reserved. 17 3.3 Another Simple Program: Adding Integers • User input two integers – Whole numbers • Program computes the sum • Display result
  • 18. © 2002 Prentice Hall. All rights reserved. Outline 18 Addition.vb 1 ' Fig. 3.10: Addition.vb 2 ' Addition program. 3 4 Module modAddition 5 6 Sub Main() 7 8 ' variables for storing user input 9 Dim firstNumber, secondNumber As String 10 11 ' variables used in addition calculation 12 Dim number1, number2, sumOfNumbers As Integer 13 14 ' read first number from user 15 Console.Write("Please enter the first integer: ") 16 firstNumber = Console.ReadLine() 17 18 ' read second number from user 19 Console.Write("Please enter the second integer: ") 20 secondNumber = Console.ReadLine() 21 22 ' convert input values to Integers 23 number1 = firstNumber 24 number2 = secondNumber 25 26 sumOfNumbers = number1 + number2 ' add numbers 27 28 ' display results 29 Console.WriteLine("The sum is {0}", sumOfNumbers) 30 31 End Sub ' Main 32 33 End Module ' modAddition Declarations begin with keyword Dim These variables store strings of characters These variables store integers values First value entered by user is assigned to variable firstNumber Method ReadLine causes program to pause and wait for user input Implicit conversion from String to Integer Sums integers and assigns result to variable sumOfNumbers Format indicates that the argument after the string will be evaluated and incorporated into the string
  • 19. © 2002 Prentice Hall. All rights reserved. Outline 19 Addition.vb Please enter the first integer: 45 Please enter the second integer: 72 The sum is 117
  • 20. © 2002 Prentice Hall. All rights reserved. 20 3.3 Another Simple Program: Adding Integers Fig. 3.11 Dialog displaying a run-time error. If the user types a non-integer value, such as “hello,” a run-time error occurs
  • 21. © 2002 Prentice Hall. All rights reserved. 21 3.4 Memory Concepts • Variables – correspond to actual locations in the computer’s memory – Every variable has a • Name • Type • Size • value – A value placed in a memory location replaces the value previously stored • The previous value is destroyed – When value is read from a memory location, it is not destroyed
  • 22. © 2002 Prentice Hall. All rights reserved. 22 3.4 Memory Concepts Fig. 3.12 Memory location showing name and value of variable number1. Fig. 3.13 Memory locations after values for variables number1 and number2 have been input. 45number1 45 45 number1 number2
  • 23. © 2002 Prentice Hall. All rights reserved. 23 3.5 Arithmetic • Arithmetic operators – Visual Basic use various special symbols not used in algebra • Asterisk (*), keyword Mod – Binary operators • Operates using two operands – sum + value – Unary operators • Operators that take only one operand – +9, -19
  • 24. © 2002 Prentice Hall. All rights reserved. 24 3.5 Arithmetic Fig. 3.14 Memory locations after an addition operation. 45 45 number1 number2 sumOfNumbers 45
  • 25. © 2002 Prentice Hall. All rights reserved. 25 3.5 Arithmetic • Integer division – Uses the backslash, – 7 4 evaluates to 1 • Floating-point division – Uses the forward slash, / – 7 / 4 evaluates to 1.75 • Modulus operator, Mod – Yields the remainder after Integer division – 7 Mod 4 yields 3
  • 26. © 2002 Prentice Hall. All rights reserved. 26 3.5 Arithmetic Visual Basic operation Arithmetic operator Algebraic expression Visual Basic expression Addition + f + 7 f + 7 Subtraction – p – c p - c Multiplication * bm b * m Division (float) / x / y or <Anchor10> or x y x / y Division (Integer) none v u Modulus % r mod s r Mod s Exponentiation ^ q p q ^ p Unary Negative - –e –e Unary Positive + +g +g Fig. 3.14 Arithmetic operators. Fig. 3.14 Arithmetic Operators.
  • 27. © 2002 Prentice Hall. All rights reserved. 27 3.5 Arithmetic • Rules of operator precedence 1. Operators in expressions contained within parentheses 2. Exponentiation 3. Unary positive and negative 4. Multiplication and floating-point division 5. Integer division 6. Modulus operations 7. Addition and subtraction operations
  • 28. © 2002 Prentice Hall. All rights reserved. 28 3.5 Arithmetic Operator(s) Operation Order of evaluation (precedence) ( ) Parentheses Evaluated first. If the parentheses are nested, the expression in the innermost pair is evaluated first. If there are several pairs of parentheses “on the same level” (i.e., not nested), they are evaluated from left to right. ^ Exponentiation Evaluated second. If there are several such operators, they are evaluated from left to right. +, – Sign operations Evaluated third. If there are several such operators, they are evaluated from left to right. *, / Multiplication and Division Evaluated fourth. If there are several such operators, they are evaluated from left to right. Integer division Evaluated fifth. If there are several such operators, they are evaluated from left to right. Mod Modulus Evaluated sixth. If there are several such operators, they are evaluated from left to right. +, – Addition and Subtraction Evaluated last. If there are several such operators, they are evaluated from left to right. Fig. 3.15 Precedence of arithmetic operators. Fig. 3.15 Precedence of arithmetic operators.
  • 29. © 2002 Prentice Hall. All rights reserved. 29 3.5 Arithmetic Fig. 3.16 Order in which a second-degree polynomial is evaluated. Step 1. Step 2. Step 5. Step 3. Step 4. Step 6. y = 2 * 5 * 5 + 3 * 5 + 7 2 * 5 is 10 (Leftmost multiplication) y = 10 * 5 + 3 * 5 + 7 10 * 5 is 50 (Leftmost multiplication) y = 50 + 3 * 5 + 7 3 * 5 is 15 (Multiplication before addition) y = 50 + 15 + 7 50 + 15 is 65 (Leftmost addition) y = 65 + 7 65 + 7 is 72 (Last addition) y = 72 (Last operation—place 72 into y)
  • 30. © 2002 Prentice Hall. All rights reserved. 30 3.6 Decision Making: Equality and Relational Operators • If/Then structure – Allows a program to make decision based on the truth or falsity of some expression – Condition • The expression in an If/Then structure – If the condition is true, the statement in the body of the structure executes – Conditions can be formed by using • Equality operators • Relational operators
  • 31. © 2002 Prentice Hall. All rights reserved. 31 3.6 Decision Making: Equality and Relational Operators Standard algebraic equality operator or relational operator Visual Basic equality or relational operator Example of Visual Basic condition Meaning of Visual Basic condition Equality operators = = x = y x is equal to y <> x <> y x is not equal to y Relational operators > > x > y x is greater than y < < x < y x is less than y >= x >= y x is greater than or equal to y ? <= x <= y x is less than or equal to y Fig. 3.17 Equality and relational operators. Fig. 3.17 Equality and relational operators.
  • 32. © 2002 Prentice Hall. All rights reserved. Outline 32 Comparison.vb 1 ' Fig. 3.19: Comparison.vb 2 ' Using equality and relational operators. 3 4 Module modComparison 5 6 Sub Main() 7 8 ' declare Integer variables for user input 9 Dim number1, number2 As Integer 10 11 ' read first number from user 12 Console.Write("Please enter first integer: ") 13 number1 = Console.ReadLine() 14 15 ' read second number from user 16 Console.Write("Please enter second integer: ") 17 number2 = Console.ReadLine() 18 19 If (number1 = number2) Then 20 Console.WriteLine("{0} = {1}", number1, number2) 21 End If 22 23 If (number1 <> number2) Then 24 Console.WriteLine("{0} <> {1}", number1, number2) 25 End If 26 27 If (number1 < number2) Then 28 Console.WriteLine("{0} < {1}", number1, number2) 29 End If 30 31 If (number1 > number2) Then 32 Console.WriteLine("{0} > {1}", number1, number2) 33 End If Variables of the same type may be declared in one declaration The If/Then structure compares the values of number1 and number2 for equality
  • 33. © 2002 Prentice Hall. All rights reserved. Outline 33 Comparison.vb Program Output 34 35 If (number1 <= number2) Then 36 Console.WriteLine("{0} <= {1}", number1, number2) 37 End If 38 39 If (number1 >= number2) Then 40 Console.WriteLine("{0} >= {1}", number1, number2) 41 End If 42 43 End Sub ' Main 44 45 End Module ' modComparison Please enter first integer: 1000 Please enter second integer: 2000 1000 <> 2000 1000 < 2000 1000 <= 2000 Please enter first integer: 515 Please enter second integer: 49 515 <> 49 515 > 49 515 >= 49 Please enter first integer: 333 Please enter second integer: 333 333 = 333 333 <= 333 333 >= 333
  • 34. © 2002 Prentice Hall. All rights reserved. 34 3.6 Decision Making: Equality and Relational Operators Operators Associativity Type () left to right parentheses ^ left to right exponentiation * / left to right multiplicative left to right integer division Mod left to right modulus + - left to right additive = <> < <= > >= left to right equality and relational Fig. 3.19 Precedence and associativity of operators introduced in this chapter. Fig. 3.19 Precedence and associativity of operators introduced in this chapter.
  • 35. © 2002 Prentice Hall. All rights reserved. 35 3.7 Using a Dialog to Display a Message • Dialogs – Windows that typically display messages to the user – Visual Basic provides class MessageBox for creating dialogs
  • 36. © 2002 Prentice Hall. All rights reserved. Outline 36 SquareRoot.vb Program Output 1 ' Fig. 3.20: SquareRoot.vb 2 ' Displaying square root of 2 in dialog. 3 4 Imports System.Windows.Forms ' Namespace containing MessageBox 5 6 Module modSquareRoot 7 8 Sub Main() 9 10 ' Calculate square root of 2 11 Dim root As Double = Math.Sqrt(2) 12 13 ' Display results in dialog 14 MessageBox.Show("The square root of 2 is " & root, _ 15 "The Square Root of 2") 16 End Sub ' Main 17 18 End Module ' modThirdWelcome Sqrt method of the Math class is called to compute the square root of 2 The Double data type stores floating- point numbers Method Show of class MessageBox Line-continuation character Empty command window
  • 37. © 2002 Prentice Hall. All rights reserved. 37 3.7 Using a Dialog to Display a Message Fig. 3.21 Dialog displayed by calling MessageBox.Show. Title bar Close box Mouse pointer Dialog sized to accommodate contents. OK button allows the user to dismiss the dialog.
  • 38. © 2002 Prentice Hall. All rights reserved. 38 3.7 Using a Dialog to Display a Message • Assembly – File that contain many classes provided by Visual Basic – These files have a .dll (or dynamic link library) extension. – Example • Class MessageBox is located in assembly System.Windows.Forms.dll • MSDN Documentation – Information about the assembly that we need can be found in the MSDN documentation – Select Help > Index… to display the Index dialog
  • 39. © 2002 Prentice Hall. All rights reserved. 39 3.7 Using a Dialog to Display a Message Fig. 3.22 Obtaining documentation for a class by using the Index dialog. Search string Filter Link to MessageBox documentation
  • 40. © 2002 Prentice Hall. All rights reserved. 40 3.7 Using a Dialog to Display a Message Fig. 3.23 Documentation for the MessageBox class. Requirements section heading MessageBox class documentation Assembly containing class MessageBox
  • 41. © 2002 Prentice Hall. All rights reserved. 41 3.7 Using a Dialog to Display a Message • Reference – It is necessary to add a reference to the assembly if you wish to use its classes – Example • To use class MessageBox it is necessary to add a reference to System.Windows.Forms • Imports – Forgetting to add an Imports statement for a referenced assembly is a syntax error
  • 42. © 2002 Prentice Hall. All rights reserved. 42 3.7 Using a Dialog to Display a Message Fig. 3.24 Adding a reference to an assembly in the Visual Studio .NET IDE. References folder (expanded) Solution Explorer before reference is added Solution Explorer after reference is added System.Windows.Forms reference
  • 43. © 2002 Prentice Hall. All rights reserved. 43 3.7 Using a Dialog to Display a Message Fig. 3.25 Internet Explorer window with GUI components. Label Button (displaying an icon) Menu (e.g., Help) Text box Menu bar