CIV 257- COMPUTER PROGRAMMING Lecture 3 CIVIL AND GEOMATIC ENGINEERING FT Okyere. Scope 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. Lifetime 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. Assigning 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 = 200 Speed1=20 t=+1 or t=t+1 Scalar Variables and Array Variables Much 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. Array Variables single-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. Array Variables A(0) = 256 A(1) = 324 A(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) Array Variables VB program to add two vectors of same dimension 3x1 +3x1 Create 3 arrays Dim arr1(2) Dim arr2(2) Dim arr3(2) ‘assign values to the array Arr1(0)=txtarr11.text Arr1(1)=txtarr12.text Array Variables- Example Arr1(2)=txtarr13.text Arr2(0)=txtarr21.text Arr2(1)=txtarr22.text Arr2(2)=txtarr23.text ‘Now lets add array elements Arr3(0)=arr1(0)+arr2(0) Arr3(1)=arr1(1)+arr2(1) Arr3(2)=arr1(2)+arr2(2) similarly Array Variables- Multidimensional You 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 twodimensional array consisting of 6 rows and 11 columns: Dim MyTable(5, 10) Remember zero indexing You 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 using Dim MyArray() ReDim AnotherArray() Access Levels in Visual Basic Public Private Protected Protected Friend Friend 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 classforall ‘the elements within are accessible from everywhere in the project and from ‘projects that reference this project End class Access 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 private Protected 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. Access 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. Public Class Form1 Protected t As String Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click End Sub End Class Data 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. Type Memory VB Data Allocation Value Range Double 8 bytes -1.79769313486231570E+308 through 4.94065645841246544E-324 for negative values; 4.94065645841246544E-324 through 1.79769313486231570E+308 for positive values Single 4 bytes -3.4028235E+38 through -1.401298E-45 for negative values; † Integer 4 bytes -2,147,483,648 through 2,147,483,647 (signed) String Depends on platform 0 to approximately 2 billion Unicode characters Boolean Depends on platform True or False Data types- VB Examples Double E.g. 2.3433434343 or -44.4 Integer E.g. 342 or -78 Long E.gVery large integer 2,147,483,648,000 E.g. dim t as string= “characters make up a string” The 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,g SPACES ARE CHARACTERS- TRY MSWORD COUNT! Mathematical Operators Mathematical Operators The arithmetic and concatenation operators have the order of precedence described 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 precedence They have lower precedence than the arithmetic, concatenation, and comparison operators. Arithmetic and conc.>Comparison>logical Operators with equal precedence are evaluated left to right in the order in which they appear in the expression. Mathematical Operators Exponentiation (^) Raises a number to the power of another number. number ^ exponent E.g. t= 2^3 t=8 Unary identity and negation (+, –) Multiplies two numbers. -1*number operand number -6 +7 In mathematics, a unary operation is an operation with only one operand Mathematical Operators Multiplication and floating-point division (*, /) Divides two numbers and retfloating-point result. E.g. t= 2*3 T=6 expression1 / expression2 E.g. t=8/2 t=4 Mathematical 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=1 Mathematical Operators Addition and subtraction (+, –), string concatenation (+) Adds two numbers or returns the positive value of a numeric expression. The Can also be used to concatenate two string expressions. expression1 + expression2 - or + expression1 E.g. t= 2+11 str=”The Garfield”+” Movie” <STRING CONCATENATION str=” The Garfield Movie” Comparison Operators Operator True if False if < (Less than) expression1 < expression2 expression1 >= expression2 <= (Less than or equal to) expression1 <= expression2 expression1 > expression2 > (Greater than) expression1 > expression2 expression1 <= expression2 >= (Greater than or equal to) expression1 >= expression2 expression1 < expression2 = (Equal to) expression1 = expression2 expression1 <> expression2 <> (Not equal to) expression1 <> expression2 expression1 = expression2 Comparison Operators The result of a comparison operation is a boolean datatype True or false Example Dim testResult As Boolean testResult = 45 < 35 testResult = 45 = 45 testResult = 4 <> 3 testResult = "5" > "4444" Arithmetic Operators VBExample 1. Dim num1, num2, difference, product, quotient As Single 2. num1 = TextBox1.Text 3. num2 = TextBox2.Text 4. sum=num1+num2 5. difference=num1-num2 6. product = num1 * num2 7. quotient=num1/num2 8. Label1.Text=sum 9. Label2.Text=difference 10. Label3.Text = product Logical Operators Logical Operators Logical operators compare Boolean expressions and return a Boolean result (True or False). Not Bitwise Logical Operators Negation (Not) Performs logical negation on a Boolean expression, or bitwise negation on a numeric expression. result = Not expression If expression is True False Example The value of result is False True Dim var As Boolean = Not (2 < 3) Logical Operators Conjunction (And, AndAlso) Performs a logical conjunction on two Boolean expressions, or a bitwise conjunction on two numeric expressions. result = expression1 And expression2 If expression1 is True True False False And expression2 is True False True False The value of result is True False False False Logical 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 is And expression2 is The value of result is True True True True False True False True True False False False Logical Operators Exclusive disjunction (Xor) Performs a logical exclusion on two Boolean expressions, or a bitwise exclusion on two numeric expressions. result = expression1 Xor expression2 For Boolean comparison, result is True if and only if exactly one of expression1 and expression2 evaluates to True If expression1 is And expression2 is The value of result is True True False True False True False True True False False False Arithmetic, logical and comparison operator Examples: BODMAS with exponentiation and other operators BEODMASCL BRACKET EXPONENTIAL OF DIVISION MULTIPLICATION ADDITION SUBTRACTION COMPARISON LOGICAL 1. ((1+3)-(2*4-1)/-1 2. ((5+3)-(2ˆ4-1)/(-1-1*4) 3. (1>2) XOR (3>4) VB Control Structures Repetition and Conditional Statements Vb sYNTAX If condition [ Then ] [ statements ] [ ElseIf elseifcondition [ Then ] [ elseifstatements ] ] [ Else [ elsestatements ] ] End If VB Control Structures If Statement condition A condition is required. That expression must evaluate to True or False, or to a data type that 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. VB Control Structures elseifstatements 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 or elseifcondition expression evaluates to True. End If VB Control Structures Example If i = 2 Then g2 = i * 1000 ElseIf i = 4 Then MsgBox("i=4") End If The appalling silence of good people ?