CIVIL AND GEOMATIC ENGINEERING FT Okyere. CIV 257- COMPUTER PROGRAMMING Lecture 3.

Click here to load reader

download CIVIL AND GEOMATIC ENGINEERING FT Okyere. CIV 257- COMPUTER PROGRAMMING Lecture 3.

of 37

Transcript of CIVIL AND GEOMATIC ENGINEERING FT Okyere. CIV 257- COMPUTER PROGRAMMING Lecture 3.

CIV 257- COMPUTER PROGRAMMING

CIVIL AND GEOMATIC ENGINEERINGFT Okyere.CIV 257- COMPUTER PROGRAMMINGLecture 31Scope of a Variables A variable's scope is determined by where you declare it. When you declare a variable within a procedure, only code within that procedure can access or change the value of that variable. It has local scope and is a procedure-level variable. If you declare a variable outside a procedure, you make it recognizable to all the procedures in your script. This is a script-level variable, and it has script-level scope.2Lifetime of Variables The lifetime of a variable depends on how long it exists. The lifetime of a script-level variable extends from the time it is declared until the time the script is finished running.At procedure level, a variable exists only as long as you are in the procedure. When the procedure exits, the variable is destroyed. Local variables are ideal as temporary storage space when a procedure is executing. You can have local variables of the same name in several different procedures because each is recognized only by the procedure in which it is declared.3Assigning Values to Variables Values are assigned to variables creating an expression as follows: the variable is on the left side of the expression and the value you want to assign to the variable is on the right.For example: B = 200Speed1=20t=+1 or t=t+1

4Scalar Variables and Array VariablesMuch of the time, you only want to assign a single value to a variable you have declared.A variable containing a single value is a scalar variable. Other times, it is convenient to assign more than one related value to a single variable.Then you can create a variable that can contain a series of values. This is called an array variable. Array variables and scalar variables are declared in the same way, except that the declaration of an array variable uses parentheses ( ) following the variable name.

5Array Variablessingle-dimension array containing 11 elements is declared: Dim A(10) Although the number shown in the parentheses is 10, all arrays in VBScript are zero-based, so this array actually contains 11 elements. E.g. Dimension an array to contain 12 elements.Dim arr1(11)You assign data to each of the elements of the array using an index into the array.

6Array VariablesA(0) = 256A(1) = 324A(2) = 100 . . .A(10) = 55 Similarly, the data can be retrieved from any element using an index into the particular array element you want. For example: SomeVariable = A(8)

7Array VariablesVB program to add two vectors of same dimension3x1 +3x1Create 3 arraysDim arr1(2)Dim arr2(2)Dim arr3(2)assign values to the arrayArr1(0)=txtarr11.textArr1(1)=txtarr12.text

8Array Variables- ExampleArr1(2)=txtarr13.textArr2(0)=txtarr21.textArr2(1)=txtarr22.textArr2(2)=txtarr23.textNow lets add array elementsArr3(0)=arr1(0)+arr2(0)Arr3(1)=arr1(1)+arr2(1)Arr3(2)=arr1(2)+arr2(2)

similarly9Array Variables- MultidimensionalYou can declare multiple dimensions(UP TO 60 DIMENSIONS) by separating an array's size numbers in the parentheses with commas. In the following example, the MyTable variable is a two-dimensional array consisting of 6 rows and 11 columns: Dim MyTable(5, 10) Remember zero indexingYou can also declare an array whose size changes during the time your script is running.This is called a dynamic array. The array is initially declared within a procedure usingDim MyArray()ReDim AnotherArray()

10Access Levels in Visual Basic Public The Public (Visual Basic) keyword in the declaration statement specifies that the elements can be accessed from code anywhere in the same project, from other projects that reference the project, and from any assembly built from the project.Public class classforallthe elements within are accessible from everywhere in the project and from projects that reference this projectEnd classPublic

PrivateFriend

ProtectedProtected Friend11Access Levels in Visual Basic Private The Private (Visual Basic) keyword in the declaration statement specifies that the elements can be accessed only from within the same module, class, or structure. Private x1 as integer this is accessible form the within the module and not without it At module level dim statement works like privateProtected The Protected (Visual Basic) keyword in the declaration statement specifies that the elements can be accessed only from within the same class, or from a class derived from this class.12Access Levels in Visual Basic Friend The Friend (Visual Basic) keyword in the declaration statement specifies that the elements can be accessed from within the same assembly, but not from outside the assembly.Protected Friend The Protected and Friend keywords together in the declaration statement specify that the elements can be accessed either from derived classes or from within the same assembly, or both.

13Public Class Form1Protected t As StringPrivate Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click End SubEnd Class14Data Types -Visual Basic The following table shows the Visual Basic data types, their supporting common language runtime types, their nominal storage allocation, and their value ranges.

VB Data TypeMemory AllocationValue RangeDouble8 bytes-1.79769313486231570E+308 through 4.94065645841246544E-324 for negative values; 4.94065645841246544E-324 through 1.79769313486231570E+308 for positive valuesSingle4 bytes-3.4028235E+38 through -1.401298E-45 for negative values; Integer4 bytes-2,147,483,648 through 2,147,483,647 (signed)StringDepends on platform0 to approximately 2 billion Unicode characters BooleanDepends on platformTrue or False15Data types- VBExamplesDouble E.g. 2.3433434343 or -44.4Integer E.g. 342 or -78Long E.g Very large integer 2,147,483,648,000E.g. dim t as string= characters make up a stringThe variable t stores the string made up of c, h, a, r, a, c, t, e, r, s, ,m,a,k,e, ,u,p, ,a, ,s,t,r,I,n,gSPACES ARE CHARACTERS- TRY MS WORD COUNT!

16Mathematical Operators

17Mathematical Operators The arithmetic and concatenation operators have the order of precedencedescribed in the following section, and all have greater precedence than the comparison,logical, and bitwise operators. The logical and bitwise operators have the order of precedenceThey have lower precedence than the arithmetic, concatenation, and comparison operators. Arithmetic and conc.>Comparison>logicalOperators with equal precedence are evaluated left to right in the order in which they appear in the expression. 18Mathematical Operators Exponentiation (^) Raises a number to the power of another number. number ^ exponentE.g. t= 2^3 t=8Unary identity and negation (+, ) Multiplies two numbers. -1*numberoperand number -6+7In mathematics, aunary operationis an operation with only one operand

19Mathematical Operators Multiplication and floating-point division (*, /) Divides two numbers and retfloating-point result. E.g. t= 2*3 T=6

expression1 / expression2E.g. t=8/2 t=4

20Mathematical Operators Integer division (\)Divides two numbers and returns an integer result. expression1 \ expression2 Dim t As Integer t = 4 \ 3 t=1 Modulus arithmetic (Mod) Divides two numbers and returns only the remainder.

number1 Mod number2 E.g. t= 2 mod 9 t=121Mathematical Operators Addition and subtraction (+, ), string concatenation (+) Adds two numbers or returns thepositive value of a numeric expression. The Can also be used to concatenate two stringexpressions. expression1 + expression2- or -+ expression1 E.g. t= 2+11 str=The Garfield+ Movie = expression2 (Greater than)expression1 > expression2expression1 = (Greater than or equal to)expression1 >= expression2expression1 < expression2= (Equal to)expression1 = expression2expression1 expression2 (Not equal to)expression1 expression2expression1 = expression223Comparison Operators The result of a comparison operation is a boolean datatypeTrue or false ExampleDim testResult As Boolean testResult = 45 < 35testResult = 45 = 45testResult = 4 3testResult = "5" > "4444" 24Arithmetic Operators VBExampleDim num1, num2, difference, product, quotient As Singlenum1 = TextBox1.Textnum2 = TextBox2.Textsum=num1+num2difference=num1-num2product = num1 * num2quotient=num1/num2Label1.Text=sumLabel2.Text=differenceLabel3.Text = product

25Logical Operators Logical Operators Logical operators compare Boolean expressions and return a Boolean result (True orFalse). Not Bitwise26Logical Operators If expression isThe value of result isTrueFalseFalseTrueNegation (Not)Performs logical negation on a Boolean expression, or bitwise negation on a numericexpression. result = Not expression

Example Dim var As Boolean = Not (2 < 3)

27Logical Operators Conjunction (And, AndAlso) Performs a logical conjunction on two Boolean expressions, or a bitwise conjunction ontwo numeric expressions. result = expression1 And expression2

If expression1 isAnd expression2 isThe value of result isTrueTrueTrueTrueFalseFalseFalseTrueFalseFalseFalseFalse28Logical Operators Inclusive disjunction (Or, OrElse) Performs a logical disjunction on two Boolean expressions, or a bitwise disjunction on two numeric expressions.result = expression1 Or expression2

If expression1 isAnd expression2 isThe value of result isTrueTrueTrueTrueFalseTrueFalseTrueTrueFalseFalseFalse29Logical Operators Exclusive disjunction (Xor) Performs a logical exclusion on two Boolean expressions, or a bitwise exclusion on two numeric expressions.result = expression1 Xor expression2For Boolean comparison, result is True if and only if exactly one of expression1 and expression2 evaluates to True

If expression1 isAnd expression2 isThe value of result isTrueTrueFalseTrueFalseTrueFalseTrueTrueFalseFalseFalse30Arithmetic, logical and comparison operator Examples: BODMAS with exponentiation and other operatorsBEODMASCL

BRACKET EXPONENTIAL OF DIVISION MULTIPLICATION ADDITION SUBTRACTION COMPARISON LOGICAL((1+3)-(2*4-1)/-1 ((5+3)-(24-1)/(-1-1*4)(1>2) XOR (3>4)

31VB Control StructuresRepetition and Conditional StatementsVb sYNTAXIf condition [ Then ] [ statements ][ ElseIf elseifcondition [ Then ] [ elseifstatements ] ][ Else [ elsestatements ] ]End If

32VB Control StructuresIf Statementcondition A condition is required. That expression must evaluate to True or False, or to a data typethat is implicitly convertible to Boolean (e.g. a>b).

Optional. One or more statements following If...Then that are executed if condition evaluates to True.

elseifcondition Required if ElseIf is present. The expression must evaluate to True or False, or to a data type that is implicitly convertible to Boolean.

33VB Control Structureselseifstatements Optional. One or more statements following ElseIf...Then that are executed if elseifcondition evaluates to True.

elsestatements Optional. One or more statements that are executed if no previous condition orelseifcondition expression evaluates to True. End If

34VB Control StructuresExample

If i = 2 Then g2 = i * 1000 ElseIf i = 4 Then MsgBox("i=4") End If 3536The appalling silence of good people?37