visualbasic lecture6.pdf
Transcript of visualbasic lecture6.pdf
-
8/10/2019 visualbasic lecture6.pdf
1/9
Copyright 2011 Pearson Addison-Wesley
Addison Wesley
is an imprint of
2011 Pearson Addison-Wesley. All rights reserved.
Addison Wesley
is an imprint of
Section 3.3
Performing Calculations
Visual Basic has powerful arithmetic
operators that perform calculations with numeric variables and literals.
-
8/10/2019 visualbasic lecture6.pdf
2/9
Copyright 2011 Pearson Addison-Wesley
Common Arithmetic Operators
Visual Basic provides operators for the
common arithmetic operations:
+ Addition- Subtraction
* ultiplication
/ !ivision
^ "#ponentiation
Chapter 3$ Slide %
-
8/10/2019 visualbasic lecture6.pdf
3/9
-
8/10/2019 visualbasic lecture6.pdf
4/9
Copyright 2011 Pearson Addison-Wesley
Special &nteger !ivision Operator
'he bac(slash )&* is used as an integer division
operator
!ivides one integer b+ another
'he result is alwa+s an integer, created b+
discarding an+ remainder from the division
&f calculating the number of hours in a givennumber of minutes
int'ours = int(inutes & )
-ith intinutes eual to /01, this calculationChapter 3$ Slide
-
8/10/2019 visualbasic lecture6.pdf
5/9
Copyright 2011 Pearson Addison-Wesley
odulus )O!* Operator
'his operator can be used in place of the bac(slash
operator to give the remainder of a division operation
inte"ainder = , (.D % result is 0
dble"ainder = ,12 (.D % result is 012
4se of the 5 or O!
operator to perform integer
division b+ 6ero causes a!ivideB+7ero"#ception
runtime error
Chapter 3$ Slide 8
-
8/10/2019 visualbasic lecture6.pdf
6/9
Copyright 2011 Pearson Addison-Wesley
"#plicit '+pe Conversions
A function performs some predetermined
operation and provides a single output
VB provides a set of functions that permit
narrowing conversions with Option Strict On
'hese functions will accept a constant,variable name, or arithmetic e#pression
'he function returns the converted valueChapter 3$ Slide 9
-
8/10/2019 visualbasic lecture6.pdf
7/9
Copyright 2011 Pearson Addison-Wesley
"#plicit '+pe Conversion "#amples
ounding can be done with the C&nt function
int#ount = #!nt3,0145 int#ount alue is
,0int#ount = #!nt3,0125 int#ount alue is
,%
CStr converts an integer value to a string
Di" strText As $tring = #$tr30)5
C!ec converts a string to a double7 7
Chapter 3$ Slide ;
-
8/10/2019 visualbasic lecture6.pdf
8/9
Copyright 2011 Pearson Addison-Wesley
Commonl+ 4sed Conversion
-
8/10/2019 visualbasic lecture6.pdf
9/9
Copyright 2011 Pearson Addison-Wesley
A ist of Conversion