Presentation is loading. Please wait.

Presentation is loading. Please wait.

Variable, Constants and Calculations Dr Mohammad Nabil Almunawar.

Similar presentations


Presentation on theme: "Variable, Constants and Calculations Dr Mohammad Nabil Almunawar."— Presentation transcript:

1 Variable, Constants and Calculations Dr Mohammad Nabil Almunawar

2 Learning Objectives Distinguish variables & constants Identify and specify data type for variables and constants Declare variable Make simple programs using variables

3 Variables & Constants A variable is a symbolic name (memory location) that hold a value. The value can be changed anytime. A constant is a value or a symbolic name (memory location) that has a value, which cannot be changed once it is declared.

4 Data Types A data type (type) of a variable or constant is a set of values valid for that variable or constant. A default data type for any variable or constant is variant (If you do not specify a type for a variable or constant then the type is variant). It is a good habit to specify a type for every variable or constant.

5 Data types Example deciding data types: ContentsData typesReason IC number or Student ID# StringNot used in calculation PriceCurrencyUsed in a calculation, contains decimal points Hours workedCurrencyUsed in a calculation QuantityIntegerUsed in calculation, contains a whole number Success statusBooleanContain logical value true or false

6 Naming Rules Each variable or constant name: 1.Must be one character and maximum 255 characters 2.Contains letters, digit, and underscore. They cannot contain any space or period. 3.Must be different from VB reserved names such as Sub, print, function, value.

7 Naming Convention A name (identifier) must be meaningful. Do not abbreviate, unless its meaning is obvious. Do not use very short identifier such as X and Y Precede each identifier with lowercase prefix that specifies the data type. (bln for Boolean, int for Integer, cur for Currency, lng for Long, str for string, dtm for date/time, vnt for variant, sng for single) Capitalize each word of the name (following prefix. Always use mixed case, never all uppercase.

8 Sample identifiers DataPossible name IC numberstrICNumber Pay ratecurPayRate PricecurPrice QuantityintQuantity KampongstrKampong Product namestrProductName FlagblnFlag PopulationlngPopulation

9 Exercise: What do you think about the following identifiers OmittedstrSub int#soldMinimumRate Int.Number.SoldstrCompanyName SubconMaximum sng$AmountblnSwitch CaptionlngPopulation

10 Declaring Constant Constant statement: Const Identifier [As Datatype] = Value Examples: Const strCompanyName As String = “Bakar Ali Sdn. Bhd.” Const intHourWork As Integer = 40 Const curSalesTax As Currency = 0.1

11 Declaring Variables Dim statement Dim identifier [As datatype] Example: Dim strCustName As String Dim dtmDateOfBirth As Date Dim intAmount As Integer Dim curPrice As Currency Dim sngWidth As Single Dim strGrade As String

12 Example of Programs A program to compute an area of a triangle Simple currency conversion Calculate final mark and grade the mark

13 Variables Scope Variable life time is the period of time that the variable exist. –Local variable: a variable is called a local variable if it is defined inside a subroutine (Sub or function). The scope of this variable is within the corresponding Sub or function. In other words, it cannot be seen from outside the Sub or function. –Module variable: a variable which is defined in a module (form). The scope of this variable is within a form (can be seen anywhere within the form)

14 Option Explicit Dim mModuleLevelVariable As Datatype Const mModuleLevelConstant Ad Datatype __________________________________ Private Sub cmdCalculate_Click Dim LocalVariable As Datatype Const LocalVariable As Datatype … End Sub ___________________________________ Private Function Length As Datatype Dim LocalVariable … mModuleLevelVariable = …. End Function ___________________________________

15 Calculations Arithmetic Operations Operations within parentheses “(…)” will be performed first. ^Exponentiation *, /Multiplication/division +,-Addition/substraction Order of operation

16 VB Calculation Notation Mathematical NotationEquivalent VB Statement 2A2*A 3(X + Y)3 * (X + Y) (X + Y)(X – Y)(X + Y) * (X – Y) X + Y 2 X + Y^2

17 Formatting Data FormatCurrency FormatNumber VariableValueFunctionOutput curBalance1275.675FormatCurrency(curBalance)$1,275.68 curBalance1275.675FormatCurrency(curBalance,3)$1,275.675 curBalance1275.675FormatCurrency(curBalance,0)$1,276 VariableValueFunctionOutput curBalance1275.675FormatNumber(curBalance,0)1,276 curBalance1275.675FormatNumber(curBalance,3)1,275.675 curBalance1275.675FormatNumber(curBalance,2)$1,276.68

18 FormatPercent FormatDateTime VariableValueFunctionOutput curRate0.75FormatPercent(curRate)75% curRate0.734FormatPercent(curRate,1)73.4% curRate0.734FormatPercent(curRate,2)73.40% VariableValueFunctionOutput dtmToday9/11/00FormatDateTime(dtmToday,vbShortDate)9/11/00 dtmToday9/11/00FormatDateTime(dtmToday,vbLongDate)Monday, September 11,2000 dtmTime FormatDateTime(dtmTime,vbLongTime)3:16:24 PM dtmTime FormatDateTime(dtmTime,vbShortTime)3:16

19 Exercise


Download ppt "Variable, Constants and Calculations Dr Mohammad Nabil Almunawar."

Similar presentations


Ads by Google