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

Slides:



Advertisements
Similar presentations
Chapter 3: Using Variables and Constants Programming with Microsoft Visual Basic 2005, Third Edition.
Advertisements

Lecture Set 4 Data Types and Variables Part B – Variables, Constants, Expressions Conversion Rules Options Strict, Option Explicit Scope of Definition.
Data Types in Java Data is the information that a program has to work with. Data is of different types. The type of a piece of data tells Java what can.
1 ICS103 Programming in C Lecture 4: Data Types, Operators & Expressions.
Chapter 3 Variables, Constants, and Calculations
Introduction to Computers and Programming Lecture 4: Mathematical Operators New York University.
Control structures Part 2 iteration control To enable repetition of a statement block.
The number of calories burned per hour by cycling, jogging and swimming are 200, 475 and 275 respectively. A person loses 1pound of weight for each 3500.
VB.Net Introduction - 2. Counter Example: Keep track the number of times a user clicks a button Need to declare a variable: Dim Counter As Integer Need.
IS 1181 IS 118 Introduction to Development Tools VB Chapter 04.
Chapter 3 Numerical Data. Topics Variables Numeric data types Assignment Expressions.
Chapter Three Using Variables and Constants Programming with Microsoft Visual Basic th Edition.
Copyright © 2001 by Wiley. All rights reserved. Chapter 3: Variables, Assignment Statements, and Arithmetic Variables Assignment Statements Arithmetic.
Apply Sub Procedures/Methods and User Defined Functions
Variables, Constants, Methods, and Calculations Chapter 3 - Review.
Programming with Microsoft Visual Basic th Edition CHAPTER THREE USING VARIABLES AND CONSTANTS.
110-K1 Iterations (1) Up to now, need to call the procedure again (e.g. click again on a command button) To repeat a piece of code: Can also use loops:
1 Visual Basic for Applications (VBA) for Excel Prof. Yitzchak Rosenthal.
1 Chapter 3 – Variables, Input, and Output 3.1 Numbers 3.2 Strings 3.3 Input and Output.
© 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Programming with Java Chapter 4 Performing Calculations and Formatting.
Programming Examples to Accompany Structure Topic Please use speaker notes for additional information!
D-1 University of Washington Computer Programming I Lecture 4: Arithmetic Expressions © 2000 UW CSE.
Chapter 2: Using Data.
Microsoft Visual Basic 2005 CHAPTER 4 Variables and Arithmetic Operations.
Chapter 4 Variables and constants. 4.1 Variables -Use of variables is good programming style -easier to modify -easier for a programmer to understand.
Department of Computer Engineering Computer Programming for International Engineers I NTERNATIONAL S CHOOL OF E NGINEERING C HULALONGKORN U NIVERSITY.
Input Textboxes Input Boxes Different than textboxes Good for small amount of input (form full of textboxes is not nice) X = Inputbox(“prompt message”,
1 Κατανεμημένες Διαδικτυακές Εφαρμογές Πολυμέσων Γιάννης Πετράκης.
Visual Basic IITG to be expanded. What is Visual Basic? Object Oriented Programming Language (OOP) Graphical User Interface (GUI) Event Driven – Write.
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 3 Variables, Constants, Methods, and Calculations.
Applications Development
Introduction to Visual Basic Programming. Introduction Simple Program: Printing a Line of Text Another Simple Program: Adding Integers Memory Concepts.
Chapter 3 Variables, Constants and Calculations Programming In Visual Basic.NET.
Variables & Function Calls. Overview u Variables  Programmer Defined & Intrinsic  Data Types  Calculation issues u Using Functions  The val() function.
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Three Memory Locations and Calculations.
Chapter 3 w Variables, constants, and calculations DIM statements - declaration temporary memory locations identifier, data type, scope data types - values.
Chapter 4 Variables and constants. 4.1 Variables -Use of variables is good programming style -easier to modify -easier for a programmer to understand.
Variables and Constants Variable Memory locations that hold data that can be changed during project execution Example: a customer’s name Constant Memory.
Chapter 3 Control Structures. The If…Then Statement The If…Then statement is a Decision statement = that executes a set of statements when a condition.
Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Three Using Variables and Constants.
Exceptions, handling exceptions & message boxes Year 11 Information Technology.
1 Chapter 3 – Examples The examples from chapter 3, combining the data types, variables, expressions, assignments, functions and methods with Windows controls.
Hungarian Notation A must in this course Every object used MUST be renamed including the form(s) using the following rules Form  frmFormName E.g. frmTemperature.
Programming with Microsoft Visual Basic th Edition
D-1 University of Washington Computer Programming I Lecture 4: Arithmetic Expressions © 2000 UW CSE.
CHAPTER THREE Representing Data: Constants and Variables.
Controlling Program Flow with Decision Structures.
Microsoft Visual Basic 2012 CHAPTER FOUR Variables and Arithmetic Operations.
CIS 338: VB Variables Dr. Ralph D. Westfall April, 2011.
Making Interactive Programs with Visual Basic .NET
Chapter 4.  Variables – named memory location that stores a value.  Variables allows the use of meaningful names which makes the code easier to read.
Instructors: Sidra Ehsan Budoor Bawazeer CpCS 206.
Microsoft Visual Basic 2010 CHAPTER FOUR Variables and Arithmetic Operations.
An Application Uses Variables to Hold Information So It May Be Manipulated, Used to Manipulate Other Information, or Remembered for Later Use.
Visual Basic 6 (VB6) Data Types, And Operators
2.5 Another Java Application: Adding Integers
Variables and Arithmetic Operations
Multiple variables can be created in one declaration
Lecture Set 4 Data Types and Variables
Variables and Arithmetic Operations
Visual Basic Programming Chapter Four Notes Working with Variables, Constants, Data Types, and Expressions GROUPBOX CONTROL The _____________________________________.
Arithmetic Expressions & Data Conversions
Visual Basic Numbers Chapter 3.3 Prepared By: Deborah 1/15/2019.
Doing Arithmetic Today’s lecture is in chapter 2 Assignment statement:
Data Types and Expressions
Visual Basic Numbers Chapter 3.3 Prepared By: Deborah 7/9/2019.
Data Types and Expressions
Arithmetic Expressions & Data Conversions
Data Types and Expressions
Python Creating a calculator.
Presentation transcript:

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

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 is a Double, 2.15D is a Decimal Operators on decimal and double unary: - acts on one piece of data decTemperature binary: +, -, /, *, ^ acts on two pieces of data * decMiles decRadius^2 add subtract divide multiply exponentiation

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 ? Mod works also with decimals and doubles

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!

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

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

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 is converted to 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

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

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

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)

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

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

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

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

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

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()

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)

110 E-18 Formatting Data FormatCurrency(1000) 'gives $1, 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, is printed lblDisplay.Text= Format(1334.9, "####.00") ‘ is printed without a comma