variable, expressions, statements and operators by: engr. faisal ur rehman ce-105 fall 2007

Post on 28-Dec-2015

220 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Variable, Expressions, Statements and OperatorsBy: Engr. Faisal ur Rehman

CE-105 Fall 2007

Goal

• Variables and its types

• Scope, lifetime and access level of variables

• Constant

• Expression

• Statement

• Procedure

• Sub Procedure

• Functions

Variable• It is the name of memory location where we put

our data.

• Declaring variable • Use Dim keyword

• Assigning values• (sometimes declared with default value assigned)

• Using variable• For data manipulation / mathematical calculation

VariablesDim x as integer = 2Dim L as double = 10Dim p as double = 5Dim R1 as double, R2 as double

Sub calcReac()

R2 = P*x / LR1 = P – R2

End sub

Data Types

Visual Basic type

Common language runtime type structure

Nominal storage allocation

Value range

Boolean Boolean Depends on implementing platform

True or False

Byte Byte 1 byte 0 through 255 (unsigned)

Char (single character)

Char 2 bytes 0 through 65535 (unsigned)

Date DateTime 8 bytes 0:00:00 (midnight) on January 1, 0001 through 11:59:59 PM on December 31, 9999

Data TypesDecimal Decimal 16 bytes 0 through +/-

79,228,162,514,264,337,593,543,950,335 (+/-7.9...E+28) † with no decimal point; 0 through +/-7.9228162514264337593543950335 with 28 places to the right of the decimal;

smallest nonzero number is +/-0.0000000000000000000000000001 (+/-1E-28) †

Double (double-precision floating-point)

Double 8 bytes -1.79769313486231570E+308 through -4.94065645841246544E-324 † for negative values;

4.94065645841246544E-324 through 1.79769313486231570E+308 † for positive values

Integer Int32 4 bytes -2,147,483,648 through 2,147,483,647 (signed)

Data Types

Long (long integer)

Int64 8 bytes -9,223,372,036,854,775,808 through 9,223,372,036,854,775,807 (9.2...E+18 †) (signed)

Object Object (class)

4 bytes on 32-bit platform

Any type can be stored in a variable of type Object

8 bytes on 64-bit platform

SByte SByte 1 byte -128 through 127 (signed)

Short (short integer)

Int16 2 bytes -32,768 through 32,767 (signed)

Single (single-precision floating-point)

Single 4 bytes -3.4028235E+38 through -1.401298E-45 † for negative values;

1.401298E-45 through 3.4028235E+38 † for positive values

Data TypesString (variable-length)

String (class) Depends on implementing platform

0 to approximately 2 billion Unicode characters

UInteger UInt32 4 bytes 0 through 4,294,967,295 (unsigned)

ULong UInt64 8 bytes 0 through 18,446,744,073,709,551,615 (1.8...E+19 †) (unsigned)

User-Defined (structure)

(inherits from ValueType)

Depends on implementing platform

Each member of the structure has a range determined by its data type and independent of the ranges of the other members

UShort UInt16 2 bytes 0 through 65,535 (unsigned)

† In scientific notation, "E" refers to a power of 10. So 3.56E+2 signifies 3.56 x 102 or 356, and 3.56E-2 signifies 3.56 / 102 or 0.0356.

Data Types• Integers are used for whole numbers

• Single is used for floating point number

• Double is used for floating point number with greater accuracy w.r.t. Single

• Long is used for financial calculation

• String and char is used for Text variable

• Object is used for reference of classes / controls

• Date / time is clear from its name

Notes for Variables

• VB.Net variables are not case sensitive• Conversion of variable from one form to other can be

implicit or explicit• Example of implicit conversion:

Dim intA as integer = 0

Dim doubA as double = 2.17

intA = doubA

• Example of explicit conversion:Dim intA as integer = 0

Dim doubA as double = 2.17

intA = integer.parse(doubA)

Data Conversion

• Use:• Integer.parse• Double.parse• Cdbl• Cint• Val• Variable.tostring

Example of object / control variable

• Dim mybtn as button

• Assignning a button to a form controlbtn1 = New Button

btn1.Top = 38

btn1.Left = 12

btn1.Size = New Size(45, 33)

btn1.Text = “Test btn”

AddHandler btn1.Click, AddressOf btn1 _Click

Me.Controls.Add(btn1)

Example of object / control variable

• Event handler code:

Private Sub mybtn_Click(ByVal sender As Object, ByVal e As EventArgs)

‘Write some code

End Sub

Scope of variable (Assign)

• Normally, a variable is in scope, or visible for reference, throughout the region in which you declare it.

• In some cases, the variable's access level can influence its scope.

Scope of variable• To make a variable visible only within a block

1. Place the Dim Statement (Visual Basic) for the variable between the initiating and terminating declaration statements of that block, for example between the For and Next statements of a For loop.

2. You can refer to the variable only from within the block.To make a variable visible only within a procedure1. Place the Dim statement for the variable inside the procedure but

outside any block (such as a With...End With block).2. You can refer to the variable only from within the procedure, including

inside any block contained in the procedure.Scope at Module or Namespace Level1. For convenience, the single term module level applies equally to

modules, classes, and structures. The access level of a module level variable determines its scope. The namespace that contains the module, class, or structure also influences the scope.

Scope of variable• To make a variable visible throughout a module, class, or

structure1. Place the Dim statement for the variable inside the module, class, or

structure, but outside any procedure.2. Include the Private (Visual Basic) keyword in the Dim statement.3. You can refer to the variable from anywhere within the module, class, or

structure, but not from outside it.

• To make a variable visible throughout a namespace1. Place the Dim statement for the variable inside the module, class, or

structure, but outside any procedure.2. Include the Friend (Visual Basic) or Public (Visual Basic) keyword in the

Dim statement.3. You can refer to the variable from anywhere within the namespace

containing the module, class, or structure.

Life time of variables (Assign)

• The lifetime of a declared element is the period of time during which it is available for use.

• Variables are the only elements that have lifetime.

• For this purpose, the compiler treats procedure parameters and function returns as special cases of variables.

• The lifetime of a variable represents the period of time during which it can hold a value. Its value can change over its lifetime, but it always holds some value.

Scope of variable (Assign)

• Different life times

• Beginning

• End

• Extension

Access Level (Assign)

• The access level of a declared element is the extent of the ability to access it, that is, what code has permission to read it or write to it.

• This is determined not only by how you declare the element itself, but also by the access level of the element's container.

• Dim, Private, Protected, Friend, Protected Friend or Public.

Constant

• Constant is the name of memory location with fixed value.

• Public Const E As double = 200E9 • Private Const E As Double = 200E9 • Protected Friend Const E As String = 200E9

Expression

• Constants and variables combined with algebraic / logical operator is called expression

• Can be algebraic or logical

If x > 3 then

Do something

End if

If 3*x = 4 then

Do something

End if

Statement

• A single line of code which is compiled successfully is called statement

• Statements are packed in to procedures

Procedures

• Procedure is a group statements or block of code

• Sub Procedure• Function

Sub Procedure

• Sub-Procedure is a block of code which do operation and do not report any result

or

• Any code within sub / end sub is called sub procedure

• Sub are within block of sub / end sub

• Statements within sub / end sub should be limited to a dozen line of code

• Sub may or may not accept arguments

Function

• Function is a procedure which do required operation and reports the result as well

• Code within Function / End Function is called a functions

• Function always need arguments

• These are also called mini-programs

Q & A

• Define:

• Variable

• Constant

• Expression

• Statement

• Scope of variable

• Procedure

• Sub Procedure

• Function

Thanks

top related