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

Slides:



Advertisements
Similar presentations
Operators & Identifiers The Data Elements. Arithmetic Operators exponentiation multiplication division ( real ) division ( integer quotient ) division.
Advertisements

Lecture Set 4 Data Types and Variables Part B – Variables, Constants, Expressions Conversion Rules Options Strict, Option Explicit Scope of Definition.
Introduction to Computers and Programming Lecture 4: Mathematical Operators New York University.
All the Operators. Precedence An operator with higher precedence is done earlier (prededes) one with lower precedence –A higher precedence is indicated.
All the Operators. Precedence An operator with higher precedence is done earlier (prededes) one with lower precedence –A higher precedence is indicated.
CS 3850 Lecture 5 Operators. 5.1 Binary Arithmetic Operators Binary arithmetic operators operate on two operands. Register and net (wire) operands are.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
JavaScript, Fourth Edition
JavaScript, Third Edition
Compunet Corporation1 Programming with Visual Basic.NET Arithmetic, Logical & Bitwise Operators Week # 3 Tariq Ibn Aziz.
VB .NET Programming Fundamentals
Spreadsheets Objective 6.02
2 Explain advanced spreadsheet concepts and functions Advanced Calculations 1 Sabbir Saleh_Lecture_17_Computer Application_BBA.
4. Python - Basic Operators
IE 212: Computational Methods for Industrial Engineering
Variable, Expressions, Statements and Operators By: Engr. Faisal ur Rehman CE-105 Fall 2007.
2440: 211 Interactive Web Programming Expressions & Operators.
Language Elements 1. Data Types 2 Floating Point (real) Single Precision Double Precision Decimal Fixed Point (integer) Byte Short Integer Long Numerical.
Chapter 3: Data Types and Operators JavaScript - Introductory.
Lecture 8 Visual Basic (2).
Lecture Set 5 Control Structures Part A - Decisions Structures.
Arrays and 2D Arrays.  A Variable Array stores a set of variables that each have the same name and are all of the same type.  Member/Element – variable.
COMPUTER PROGRAMMING I SUMMER Apply operators and Boolean expressions.
Basic Operators. What is an operator? using expression is equal to 9. Here, 4 and 5 are called operands and + is the operator Python language supports.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Dani Vainstein1 VBScript Session 1. Dani Vainstein2 Subjets for Session 1 Vbscript fundamentals. Variant subtypes. Variables. Option Explicit statement.
Visual Basic IITG to be expanded. What is Visual Basic? Object Oriented Programming Language (OOP) Graphical User Interface (GUI) Event Driven – Write.
VBScript Language. What is VBScript Based on the Visual Basic family of languages Supports object oriented features Interpreted Loosely Typed Implicitly.
Operators & Identifiers The Data Elements. Arithmetic Operators exponentiation multiplication division ( real ) division ( integer quotient ) division.
EIW - ASP Introduction1 Active Server Pages VBScript.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 3 Variables, Constants, Methods, and Calculations.
Operators Precedence - Operators with the highest precedence will be executed first. Page 54 of the book and Appendix B list C's operator precedence. Parenthesis.
COMPUTER PROGRAMMING I SUMMER Apply operators and Boolean expressions.
VARIABLES, CONSTANTS, OPERATORS ANS EXPRESSION
CNS 1120 Exam 1 Review. Programming Language Elements Syntax = the rules to follow Syntax = the rules to follow Semantics = the meaning of symbols Semantics.
Assignment statement: Assigns a value to a variable Variable must appear on the left side, value on the right side of the assignment operator Right side.
1 Scripting Languages VBScript - Recognized mainly by Internet Explorer only - Netscape does have a plug-in JavaScript - Recognized by Internet Explorer.
Dani Vainstein1 VBScript Session 3. Dani Vainstein2 What We learn Last seasson? How to declare arrays. Working with one dimensional a multi- dimensional.
Introduction to Programming Lecture Note - 2 Visual Basic Programming Fundamentals.
Visual Basic Programming I 56:150 Information System Design.
Operators & Identifiers The Data Elements. Arithmetic Operators exponentiation multiplication division ( real ) division ( integer quotient ) division.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Fluency with Information Technology Third Edition by Lawrence Snyder Chapter.
COMPUTER PROGRAMMING I 5.04 Apply Decision Making Structures.
COMPUTER PROGRAMMING I SUMMER Apply operators and Boolean expressions.
Variables Hold information that may be manipulated, used to manipulate other information or remembered for later use A storage location in memory (RAM)
CECS 5020 Computers in Education Visual Basic Variables and Constants.
Visual Basic Review LBS 126. VB programming Project Form 1Form 2Form 3 Text boxButton Picture box Objects Text box Button Objects.
OPERATORS IN C CHAPTER 3. Expressions can be built up from literals, variables and operators. The operators define how the variables and literals in the.
Computer Science Up Down Controls, Decisions and Random Numbers.
VISUAL BASIC 6.0 Designed by Mrinal Kanti Nath.
5.04 Apply Decision Making Structures
VBScript Session 1 Dani Vainstein.
VBScript Session 3.
VBScript Session 1 IT Professional Academy.
Visual Basic Variables
Visual Basic 6 (VB6) Data Types, And Operators
Data Types, Arithmetic Operations
Operators and Expressions
VBScript Session 2 Dani Vainstein.
Chapter 6 Variables What is VBScript?
Boolean Expressions and If statements
CSI 101 Elements of Computing Spring 2009
Spreadsheets 2 Explain advanced spreadsheet concepts and functions
VBScript Session 2 Dani Vainstein.
Spreadsheets Objective 6.02
Winter 2019 CISC101 4/28/2019 CISC101 Reminders
VBScript Session 1.
Spreadsheets Objective 6.02
Lecture 9: JavaScript Syntax
Presentation transcript:

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

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) = 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 two- dimensional 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 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 PublicPrivateFriend ProtectedProtected Friend

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. VB Data TypeMemory Allocation Value Range Double8 bytes E+308 through E-324 for negative values; E-324 through E+308 for positive values Single4 bytes E+38 through E-45 for negative values; † Integer4 bytes-2,147,483,648 through 2,147,483,647 (signed) StringDepends on platform 0 to approximately 2 billion Unicode characters BooleanDepends on platform True or False

Data types- VB Examples Double E.g or Integer E.g. 342 or -78 Long E.g Very 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 MS WORD COUNT!

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 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 OperatorTrue ifFalse if < (Less than)expression1 < expression2expression1 >= expression2 <= (Less than or equal to)expression1 <= expression2expression1 > expression2 > (Greater than)expression1 > expression2expression1 <= expression2 >= (Greater than or equal to)expression1 >= expression2expression1 < expression2 = (Equal to)expression1 = expression2expression1 <> expression2 <> (Not equal to)expression1 <> expression2expression1 = 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 compare Boolean expressions and return a Boolean result (True or False). Not Bitwise

Logical Operators If expression isThe value of result is TrueFalse True Negation (Not) Performs logical negation on a Boolean expression, or bitwise negation on a numeric expression. result = Not expression Example 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 And expression2 is The value of result is True False TrueFalse

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 isAnd expression2 isThe value of result is True FalseTrue FalseTrue 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 isAnd expression2 isThe value of result is True False TrueFalseTrue FalseTrue 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 ?