Presentation is loading. Please wait.

Presentation is loading. Please wait.

110 E-1 Variables, Constants and Calculations(2) Chapter 3: Operations on variables, scope of a variable, formatting data Doing Arithmetic.

Similar presentations


Presentation on theme: "110 E-1 Variables, Constants and Calculations(2) Chapter 3: Operations on variables, scope of a variable, formatting data Doing Arithmetic."— Presentation transcript:

1 110 E-1 Variables, Constants and Calculations(2) Chapter 3: Operations on variables, scope of a variable, formatting data Doing Arithmetic

2 110 E-2 Operators for double and decimal Reminder: Decimal and Double = type for a floating point variable e.g. 2.15, -1.28, NOT 3 or -4 2.15 is a Double, 2.15D is a Decimal Operators on decimal and double unary: - acts on one piece of data -23.4 -decTemperature binary: +, -, /, *, ^ acts on two pieces of data 1.609 * decMiles decRadius^2 add subtract divide multiply exponentiation

3 110 E-3 Operators for integers Reminder: Integer = type for an integer variable e.g. 3, -216, NOT 3.0 Operators on integers unary: - binary: +, -, /, *, ^ add subtract divide multiply exponentiation as usual BUT ALSO: \ and Mod \ is the integer division 2\3 is ?, 31\2 is ? Mod is the modulus operator: 114 Mod 50 is ? 2 Mod 3 is ? 015 14 2 Mod works also with decimals and doubles

4 110 E-4 Operator Precedence What is a + b*c ? Is it (a+b)*c or a+(b*c) ? precedence rules: 1. evaluate expressions in parentheses: start with the innermost set of parentheses 2. do exponentiation ^ 3. do unary - 4. do *, / 5. do \ 6. do Mod 7. do binary +, - a + b*c is a + (b*c) When coding, do not hesitate to use parentheses avoid errors and clearer!

5 110 E-5 VB binary arithmetic operators associate left to right within the same precedence level Associativity What is a/b*c ? Is it a/(b*c) or ( a/b)*c ? a/b*c is ( a/b)*c Example Add parentheses to the following expression to show how it is evaluated: a + b - c + d There are also VB operators that associate right to left e.g. the assignment operator = (but, unary - is right associative: write -2 not 2-) Answer: ((a+b) - c)+d

6 110 E-6 What happens in expressions with integers and doubles (1) 2*3*6.2 When adding an Integer and a Double, VB automatically converts the Integer to a Double Integer + DoubleDouble + Double (also with -, *, / and ^) Example: 6*6.26.0*6.2 conversion occurs here NOT before 37.2

7 110 E-7 What happens in expressions with integers and doubles (2) It may happen that VB converts a Double to an Integer Integer \ DoubleInteger \ Integer BE AWARE OF WHAT VB CAN DO WITH YOUR DATA! 6.6 is converted to 7 6.5 is converted to 6 6.4 is converted to 6 Conversion may also occur in an assignment statement Dim intNumber As Integer Dim dblNumber As Double dblNumber = 3.64 intNumber = dblNumber 'but error with 'Option Strict 'On intNumber is 4 USE OPTION STRICT to avoid automatic conversions from wider to narrower data type or between string and numeric data

8 110 E-8 Explicit conversions To perform a conversion explicitly, use a function (also called casting) See the list in the Microsoft Help Menu Some of them are CInt to convert to an Integer CDec to convert to a Decimal CStr to convert to a String e.g. CInt(3.64) is 4 ALSO conversion of a string to a number Can get numbers from text, e.g. a text box

9 110 E-9 Handling Exceptions (1) When users input numeric data that you use in your program, many things can go wrong (no-entry, bad entry…) Try Statements that might cause error Catch Message to the user End Try See example User enters bad data We try to use it Exception (error) occurs Use a Try-Catch Block

10 110 E-10 Ex. The user enters “four” instead of 4 Try intQuantity=Cint(txtQuantity.Text) Catch lblMessage.Text=“Bad Entry” End Try Handling Exceptions (2) Error(execution stops and goes to the catch block)

11 110 E-11 A Message Box MessageBox.Show( "Invalid Entry",”Warning”, MessageBoxButtons.OK) Message displayed in the box Type of buttonTitle of the box In a VB Program this gives

12 110 E-12 Scope and Lifetime of Variables(1) Module Variable: accessible from all procedures of a form Scope : visibility of a variable: Local Variable: accessible only within the Procedure in which it is declared Block Variable: accessible only within a Block of code inside a procedure. Module: all the code for a form Procedure: the code for an event,... e.g. the code for btnCommand_Click() Block: the code within a Loop, an If…Then block… Program Organization

13 110 E-13 Scope and Lifetime of Variables(2) Lifetime : period of time the variable exists Module level variables live for the entire time the form is loaded, generally the duration of the project (in simpler projects) Local Variables live only during the execution of the procedure See an example To maintain value of a variable for several executions of a procedure use Module level variables or declare them as local Static variables

14 110 E-14 Simulating an ATM Goal: The user types in a money amount and clicks on either withdrawal or deposit. The new account balance is displayed. WithdrawalExit Enter Amount Deposit Account Balance: $582.53

15 110 E-15 ATM: variables and procedures Variable to hold the account balance Must be accessed by the click events of the command buttons Withdrawal and Deposit module level scope Dim mdecBalance As Decimal Variable to hold the amount withdrawn Accessed only when the user clicks on the withdrawal button local to btnWithdrawal_Click() Dim decWithdrawal As Decimal Variable to hold the amount deposited Accessed only when the user clicks on the deposit button local to btnDeposit_Click() Dim decDeposit As Decimal

16 110 E-16 Click on Deposit? Yes Read the amount Update the account balance Display the account balance No ATM: algorithm for btnDeposit_Click() Similar for btnWithdrawal_Click()

17 110 E-17 ATM: Initialization In VB, the initial value of a numerical variable is 0 (Don't assume this for any programming language) Initially: mdecBalance is 0 What if we want to start with a non zero balance ? Initialize mdecBalance in the constructor Sub New() or in Form_Load (double click the form in the designer to generate the Sub)

18 110 E-18 Formatting Data FormatCurrency(1000) 'gives $1,000.00 FormatNumber(1.2351,2) 'gives 1.24 Can also be user defined (see VB.NET language reference): Use the Format function (See text starting on page 114) Some formats are already defined: lblDisplay.Text= Format(5459.4, "##,###.00") ' 5,459.40 is printed lblDisplay.Text= Format(1334.9, "####.00") ‘1334.90 is printed without a comma


Download ppt "110 E-1 Variables, Constants and Calculations(2) Chapter 3: Operations on variables, scope of a variable, formatting data Doing Arithmetic."

Similar presentations


Ads by Google