language elements 1. data types 2 floating point (real) single precision double precision decimal...

48
Language Elements 1

Upload: kelly-morris

Post on 30-Dec-2015

220 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Language Elements 1. Data Types 2 Floating Point (real) Single Precision Double Precision Decimal Fixed Point (integer) Byte Short Integer Long Numerical

LanguageElements

1

Page 2: Language Elements 1. Data Types 2 Floating Point (real) Single Precision Double Precision Decimal Fixed Point (integer) Byte Short Integer Long Numerical

Data Types

2

Page 3: Language Elements 1. Data Types 2 Floating Point (real) Single Precision Double Precision Decimal Fixed Point (integer) Byte Short Integer Long Numerical

Floating Point (real)Single Precision

Double Precision

Decimal

Fixed Point (integer)Byte

Short

Integer

Long

Numerical

3

Page 4: Language Elements 1. Data Types 2 Floating Point (real) Single Precision Double Precision Decimal Fixed Point (integer) Byte Short Integer Long Numerical

Type Bytes Digits Range

Numerical

See page 105 in the text for additional numerical data types.

Double 8 14-15 ±1.8x10-324 to ± 4.9x10308

Single 4 6-7 ±1.4x10-45 to ±3.4x1038

Long 8 19-20 -92,233,720,368,547,755,808 to 92,233,720,368,547,755,807

Byte 1 2-3 0 to 255

Short 2 4-5 -32,768 to 32,767

Integer 4 9-10 -2,147,483,648 to 2,147,483,647

Decimal 16 28-29 ± 7.9x1024

4

Page 5: Language Elements 1. Data Types 2 Floating Point (real) Single Precision Double Precision Decimal Fixed Point (integer) Byte Short Integer Long Numerical

TextualType Bytes Character Range

Char 2 1 (Unicode)

Variable depends on Length platform 0 to approx. 2 billion String

There are also fixed length Strings which we will examine later.

ASCII code is in the first 256 characters of Unicode

5

Page 6: Language Elements 1. Data Types 2 Floating Point (real) Single Precision Double Precision Decimal Fixed Point (integer) Byte Short Integer Long Numerical

Logical

Type Bytes Range

Boolean 2 True or False

6

Page 7: Language Elements 1. Data Types 2 Floating Point (real) Single Precision Double Precision Decimal Fixed Point (integer) Byte Short Integer Long Numerical

Type Bytes Range

Other

Date/ 8 midnight Jan. 1, 0001 Time to Dec. 31, 9999 Object 4 or 8 any OBJECT reference

User- varies Depends on user Defined

7

Page 8: Language Elements 1. Data Types 2 Floating Point (real) Single Precision Double Precision Decimal Fixed Point (integer) Byte Short Integer Long Numerical

Identifiers

8

Page 9: Language Elements 1. Data Types 2 Floating Point (real) Single Precision Double Precision Decimal Fixed Point (integer) Byte Short Integer Long Numerical

Non-Object NamingMust begin with a letter.

Must contain only letters, numbers, and the underscore character (_).

Punctuation characters and spaces are not allowed.

Must be no longer than 255 characters

9

Page 10: Language Elements 1. Data Types 2 Floating Point (real) Single Precision Double Precision Decimal Fixed Point (integer) Byte Short Integer Long Numerical

VariablesFirst letter is lowercase and first letter of each word is uppercase - firstName

ConstantsFirst letter of each word is uppercase - NoOfStudents

Functions/SubroutinesBegin with a verb and first letter of each word is uppercase - FindStudentRecord

10

Non-Object Naming

Page 11: Language Elements 1. Data Types 2 Floating Point (real) Single Precision Double Precision Decimal Fixed Point (integer) Byte Short Integer Long Numerical

Variables

11

Page 12: Language Elements 1. Data Types 2 Floating Point (real) Single Precision Double Precision Decimal Fixed Point (integer) Byte Short Integer Long Numerical

Single

Double

Decimal

Byte

Integer

Long

String

Object

Boolean

Date

Dim/Public/Private … As

Private age As Integer

12

Dim valid As Booleanvalid = False

Public streetAddress As StringDim valid As Boolean = False

Page 13: Language Elements 1. Data Types 2 Floating Point (real) Single Precision Double Precision Decimal Fixed Point (integer) Byte Short Integer Long Numerical

13

ArraysYou have had experience working

with arrays of simple and structured data in previous courses, so my only

comment is that Basic uses ( ) to delimit the subscripts and not [ ] that

you may have been using.

13

Dim age(100) As Integer

Creates an array of integers

Age(0) – Age(100)

Dim age(99) …Age(0) – Age(99)

99Dim grades(24,9) As Single

10 columns

25 r

ow

s

0 90

24

In VB, most indexedObjects begin with 0In VB, most indexedObjects begin with 0

Dim names() As String

Page 14: Language Elements 1. Data Types 2 Floating Point (real) Single Precision Double Precision Decimal Fixed Point (integer) Byte Short Integer Long Numerical

Although Visual Basic does not require variable declarations, it is a moot point, since I require that you declare all variables by placing the Option Explicit statement in the first line of your code.

Option Explicit On

See page 107 in thetext for Dim variations.

14

Page 15: Language Elements 1. Data Types 2 Floating Point (real) Single Precision Double Precision Decimal Fixed Point (integer) Byte Short Integer Long Numerical

In addition to local and global variables, VB (like c and c++) has an additional one called .

15

Page 16: Language Elements 1. Data Types 2 Floating Point (real) Single Precision Double Precision Decimal Fixed Point (integer) Byte Short Integer Long Numerical

Private Sub cmdDIM_Click(…)… Dim x As Integer x = x + 1 lblDim.Text = x End Sub

Private Sub cmdStatic_Click(…)… Static x As Integer x = x + 1 lblStatic.Text = x End Sub

x shouldbe initialized

x shouldbe initialized

16

Run Program

View Code

Page 17: Language Elements 1. Data Types 2 Floating Point (real) Single Precision Double Precision Decimal Fixed Point (integer) Byte Short Integer Long Numerical

Constants

17

Page 18: Language Elements 1. Data Types 2 Floating Point (real) Single Precision Double Precision Decimal Fixed Point (integer) Byte Short Integer Long Numerical

Const MaxStudents As Integer = 100

Public Const State As String = “New York”

Constants are declared in the following ways.

18

Page 19: Language Elements 1. Data Types 2 Floating Point (real) Single Precision Double Precision Decimal Fixed Point (integer) Byte Short Integer Long Numerical

Operators

19

Page 20: Language Elements 1. Data Types 2 Floating Point (real) Single Precision Double Precision Decimal Fixed Point (integer) Byte Short Integer Long Numerical

^ power operator: 2^3 = 8

\ integer division: 26\4 = 6 (whereas 26/4 = 6.5)

Mod modulus (remainder): 26 Mod 4 = 2

Arithmetic OperatorsThe standard operators that are found in most computer languages are also found in VB. The only three you may not have seen are:

20

For a list of additional arithmeticoperators, see page 109 in the text.

Page 21: Language Elements 1. Data Types 2 Floating Point (real) Single Precision Double Precision Decimal Fixed Point (integer) Byte Short Integer Long Numerical

Although VB allows shortcut assignment operators like

total += 100we will NOT use them!

Instead we will use “longhand”total = total + 100

Arithmetic Operators

21

Page 22: Language Elements 1. Data Types 2 Floating Point (real) Single Precision Double Precision Decimal Fixed Point (integer) Byte Short Integer Long Numerical

VB allows you to concatenate (put together) strings with the string concatenation operator &.

String Operators

22

"cat" & "nip" = "catnip”

"12" & "34" = ____

(Note: some languages, including VB, use the + to indicateConcatenation. How would the following be interpreted?)

"cat" + "nip" = "12" + "34" = "12" + 34 = "12" & 34 =

(Note: some languages, including VB, use the + to indicateConcatenation. How would the following be interpreted?)

"cat" + "nip" = "12" + "34" = "12" + 34 = "12" & 34 =

Page 23: Language Elements 1. Data Types 2 Floating Point (real) Single Precision Double Precision Decimal Fixed Point (integer) Byte Short Integer Long Numerical

For a list of additional relationaloperators, see page 147 in the text.

Relational Operators

The standard operators that are found in most computer languages are also found in VB:

=<>><

>=<=

23

Page 24: Language Elements 1. Data Types 2 Floating Point (real) Single Precision Double Precision Decimal Fixed Point (integer) Byte Short Integer Long Numerical

The last two are new to VB.Netand perform “short-circuiting.”

Logical Operators

The standard operators that are found in most computer languages are also found in VB:

AndOrNot

AndAlsoOrElse

24

Page 25: Language Elements 1. Data Types 2 Floating Point (real) Single Precision Double Precision Decimal Fixed Point (integer) Byte Short Integer Long Numerical

Unlike c++, Visual Basic does automatic casting (converting mixed data types). However, I require that you disable this by placing the Option Strict statement in the second line of your code.

Option Explicit OnOption Strict On

As shown in the text, you can usefunctions or method to make theconversions.

25

Page 26: Language Elements 1. Data Types 2 Floating Point (real) Single Precision Double Precision Decimal Fixed Point (integer) Byte Short Integer Long Numerical

Dim age As Integer age = CInt(txtAge.Text)

You can use casting in:

Assignment - Dim x As Decimal x = Convert.ToDecimal(txtSalary.Text)

In context - If (CInt(txtCount.Text) < 10) Then …

Microsoft Help - Conversion Functions

Microsoft Help - Conversion Methods 26

Dim statements - Dim age As Integer = CInt(txtAge.Text)

Page 27: Language Elements 1. Data Types 2 Floating Point (real) Single Precision Double Precision Decimal Fixed Point (integer) Byte Short Integer Long Numerical

Subroutines&

Functions27

Page 28: Language Elements 1. Data Types 2 Floating Point (real) Single Precision Double Precision Decimal Fixed Point (integer) Byte Short Integer Long Numerical

1. You understand the difference between passing parameters by value (copy of the data) and reference (address of the data).

2. You know the difference between “formal” parameters (must be variables) and “actual” parameters (can be expressions).

3. You understand the concept of “scope.”

4. You know the difference between local and global variables.

28

Assumptions:

Page 29: Language Elements 1. Data Types 2 Floating Point (real) Single Precision Double Precision Decimal Fixed Point (integer) Byte Short Integer Long Numerical

5. You know the difference between a subroutine (sub) procedure and a function procedure.

6. You know the difference between calling a subroutine and calling a function.

7. You know when/why you should use a subroutine/function.

29

Assumptions:

Therefore, we will focus on how subroutinesand functions are implemented in VB!

Page 30: Language Elements 1. Data Types 2 Floating Point (real) Single Precision Double Precision Decimal Fixed Point (integer) Byte Short Integer Long Numerical

30

[Private | Public] Sub ProcedureName ([parameterlist]) statements[Exit Sub] [statements]End Sub

Subroutines

Parameterlist Syntax:

[ByVal | By Ref] variableName As type

DefaultDefaultSubroutine Call:

[Call] [Me.] ProcedureName ([argumentList])

Page 31: Language Elements 1. Data Types 2 Floating Point (real) Single Precision Double Precision Decimal Fixed Point (integer) Byte Short Integer Long Numerical

Public Sub ClearTextBoxes() txtPrin.Text = “” txtRate.Text = “” txtTime.Text = “”End Sub

31

...ClearTextBoxes()...

See pages 173 & 175 in the text for additional examples of defining and calling subroutines.

Subroutines

Page 32: Language Elements 1. Data Types 2 Floating Point (real) Single Precision Double Precision Decimal Fixed Point (integer) Byte Short Integer Long Numerical

32

[Private | Public] Function FunctionName ([parameterlist]) [As type] statements[Exit Function][Return expression] [statements]End Function

Functions

Function Call:

variable = [Me.] FunctionName ([argumentList])

Option Strict OnREQUIRES this!

Option Strict OnREQUIRES this!

Actually as long as FunctionName ([argumentList]) isused properly in “context,” a function call is initiated.Actually as long as FunctionName ([argumentList]) isused properly in “context,” a function call is initiated.

Page 33: Language Elements 1. Data Types 2 Floating Point (real) Single Precision Double Precision Decimal Fixed Point (integer) Byte Short Integer Long Numerical

Private Function FindInterest( ByVal p As Decimal, ByVal r As Decimal, ByVal t As Decimal)

FindInterest = p*r/100D*t

End Function

33

...If(FindInterest(principal,rate,3D)< 1000D) Then ...... See pages 178 - 180 in the text for additional

examples of defining and calling functions.

Functions

Principal and ratewould be dimensioned as?

Principal and ratewould be dimensioned as?

As Decimal

Return p*r/100D*t

Page 34: Language Elements 1. Data Types 2 Floating Point (real) Single Precision Double Precision Decimal Fixed Point (integer) Byte Short Integer Long Numerical

Programming ConstructsSequence

Repetition

Selection

34

Page 35: Language Elements 1. Data Types 2 Floating Point (real) Single Precision Double Precision Decimal Fixed Point (integer) Byte Short Integer Long Numerical

SelectionConditional Statements

35

Page 36: Language Elements 1. Data Types 2 Floating Point (real) Single Precision Double Precision Decimal Fixed Point (integer) Byte Short Integer Long Numerical

If statement(s)If (cond) Then S1 If (Rate > 65) Then FineMsg = "You owe $75.00"

If (cond) Then S1 Else S2 If (Grade < 60) Then lblmsg.Text = "failing" Else _ lblMsg.Text = "passing"

If (cond) Then statement(s)[Else statement(s)]End If [ ] denote optional statements

36

Unlike c/c++ and Java, no { } areneeded to delimit a blockof statements! The Then,

Else, or EndIf are the delimiters.

Unlike c/c++ and Java, no { } areneeded to delimit a blockof statements! The Then,

Else, or EndIf are the delimiters.

Page 37: Language Elements 1. Data Types 2 Floating Point (real) Single Precision Double Precision Decimal Fixed Point (integer) Byte Short Integer Long Numerical

Using multiple If statements, write the code that would implement the following grading scale:

100 - 90 A 89 - 80 B

79 - 70 C 69 - 60 D

59 - 0 E

(Assume that the variables NGrade and LGradehave been appropriately declared. How is this done,

and what types would have been used?)37

Page 38: Language Elements 1. Data Types 2 Floating Point (real) Single Precision Double Precision Decimal Fixed Point (integer) Byte Short Integer Long Numerical

ElseIf statement If (cond1) Then statement(s) ElseIf (cond2) Then statement(s) ElseIf (cond3) ... [Else statement(s)] End If

Note: ElseIf is one word but End Ifare two words like End Sub and End Class. If you mistype either, VB will automatically correct it for you!!!

Note: ElseIf is one word but End Ifare two words like End Sub and End Class. If you mistype either, VB will automatically correct it for you!!!

38

Page 39: Language Elements 1. Data Types 2 Floating Point (real) Single Precision Double Precision Decimal Fixed Point (integer) Byte Short Integer Long Numerical

Select Case statement

Select Case var (or property) Case expression1

statement(s) Case expression2 statement(s) [Case Else statement(s)]

End Select

39

Page 40: Language Elements 1. Data Types 2 Floating Point (real) Single Precision Double Precision Decimal Fixed Point (integer) Byte Short Integer Long Numerical

In the next example, we are using the same button, which can have one of two “texts”. This is similar to what is done in the iTunes player, except the button has graphics instead of text. The Play button changes from to the Pause button . Depending on the graphic, a different action is performed when the button is clicked.

40

Page 41: Language Elements 1. Data Types 2 Floating Point (real) Single Precision Double Precision Decimal Fixed Point (integer) Byte Short Integer Long Numerical

Now using the Select Case statement, we will determine what action to take, based on the text of a specific command button.

41

Page 42: Language Elements 1. Data Types 2 Floating Point (real) Single Precision Double Precision Decimal Fixed Point (integer) Byte Short Integer Long Numerical

Select Case cmdButton.Text Case "Start" statement(s) cmdButton.Text = "Stop" Case "Stop"

statement(s) cmdButton.Text = "Start"End Select

The code would be:

Be careful, eventhough VB is not case sensitive, strings are!!!

Be careful, eventhough VB is not case sensitive, strings are!!!

42

Page 43: Language Elements 1. Data Types 2 Floating Point (real) Single Precision Double Precision Decimal Fixed Point (integer) Byte Short Integer Long Numerical

RepetitionLooping Statements

43

Page 44: Language Elements 1. Data Types 2 Floating Point (real) Single Precision Double Precision Decimal Fixed Point (integer) Byte Short Integer Long Numerical

Looping Statements

Enumerated

Pre-test

Post-test

44

Page 45: Language Elements 1. Data Types 2 Floating Point (real) Single Precision Double Precision Decimal Fixed Point (integer) Byte Short Integer Long Numerical

For var = beg To end [Step step]

statement(s)

Next var

Enumerated

For Counter = 1 To 10 . . .

Next Counter45

If we were indexingan array, we would

do: 0 To 9

If we were indexingan array, we would

do: 0 To 9

Page 46: Language Elements 1. Data Types 2 Floating Point (real) Single Precision Double Precision Decimal Fixed Point (integer) Byte Short Integer Long Numerical

Do While (cond) statement(s) Loop

Pre-test

Do While (Len(txtLastName.Text) = 0)

. . .

Loop46

What is this testing?What is this testing?

You could also test:txtLastName.Text = “”You could also test:txtLastName.Text = “”

Page 47: Language Elements 1. Data Types 2 Floating Point (real) Single Precision Double Precision Decimal Fixed Point (integer) Byte Short Integer Long Numerical

Post-test

Do Until (Temperature < 0) . . .

Loop

47

Do Until (cond) statement(s) Loop

Page 48: Language Elements 1. Data Types 2 Floating Point (real) Single Precision Double Precision Decimal Fixed Point (integer) Byte Short Integer Long Numerical

Read the

Description

Of Assigment-2

48