Visual Basic Numbers Chapter 3.3 Prepared By: Deborah 1/15/2019.

Slides:



Advertisements
Similar presentations
Lecture Set 4 Data Types and Variables Part B – Variables, Constants, Expressions Conversion Rules Options Strict, Option Explicit Scope of Definition.
Advertisements

1.
Chapter 3 Variables, Constants, and Calculations
Chapter 3: Introducing the Microsoft.NET Framework and Visual Basic.NET Visual Basic.NET Programming: From Problem Analysis to Program Design.
McGraw-Hill/Irwin Programming in Visual Basic 6.0 © 2002 The McGraw-Hill Companies, Inc. All rights reserved. Update Edition Chapter 3 Variables, Constants,
Copyright © 2001 by Wiley. All rights reserved. Chapter 3: Variables, Assignment Statements, and Arithmetic Variables Assignment Statements Arithmetic.
110-D1 Variables, Constants and Calculations(1) Chapter 3: we are familiar with VB IDE (building a form…) to make our applications more powerful, we need.
1 Chapter Two Using Data. 2 Objectives Learn about variable types and how to declare variables Learn how to display variable values Learn about the integral.
Variables and Constants
CS0004: Introduction to Programming Input and Output.
Chapter 3 Part 11 Variables, Constants, and Calculations Chapter 3 Section 3.3 Chapter 3 Section 3.4.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Access VBA Programming for Beginners - Class 2 - by Patrick Lasu
Chapter 2: Using Data.
3 - Variables Lingma Acheson Department of Computer and Information Science, IUPUI CSCI N331 VB.NET Programming.
Chapter 4 Variables and constants. 4.1 Variables -Use of variables is good programming style -easier to modify -easier for a programmer to understand.
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 3 Variables, Constants, Methods, and Calculations.
Chapter 3 P. 1 Data - Variables and Constants Definition A variable is a value which can change during program execution. A constant is a value which can.
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.
Variable, Constants and Calculations Dr Mohammad Nabil Almunawar.
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.
# 1# 1 What is a variable that you create? What is a constant that you create? What is an intrinsic (built-in) constant? What variables are built in?
Programming with Microsoft Visual Basic th Edition
Variables Hold information that may be manipulated, used to manipulate other information or remembered for later use A storage location in memory (RAM)
Variables in VB.NET. Variables  A storage location in memory (RAM)  Holds data/information while the program is running  These storage locations can.
110 E-1 Variables, Constants and Calculations(2) Chapter 3: Operations on variables, scope of a variable, formatting data Doing Arithmetic.
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.
Slide 1 Chapter 3 Variables  A variable is a name for a value stored in memory.  Variables are used in programs so that values can be represented with.
© 2006 Lawrenceville Press Slide 1 Chapter 4 Variables  A variable is a name for a value stored in memory.  Variables are created using a declaration.
Slide 1 Chapter 3 Variables  A variable is a name for a value stored in memory.  Variables are created using a declaration statement. For example: Dim.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
2.1 The Part of a C++ Program. The Parts of a C++ Program // sample C++ program #include using namespace std; int main() { cout
1.
A variable is a name for a value stored in memory.
Chapter 2 Variables.
Topics Designing a Program Input, Processing, and Output
Arithmetic operations and operators, converting data types and formatting programs for output. Year 11 Information Technology.
An Application Uses Variables to Hold Information So It May Be Manipulated, Used to Manipulate Other Information, or Remembered for Later Use.
BASIC ELEMENTS OF A COMPUTER PROGRAM
Variables, Expressions, and IO
2. Understanding VB Variables
Lecture Set 4 Data Types and Variables
Variables and Arithmetic Operations
IDENTIFIERS CSC 111.
Microsoft Visual Basic 2005 BASICS
Chapter 6 Variables What is VBScript?
Variables In programming, we often need to have places to store data. These receptacles are called variables. They are called that because they can change.
Chapter 3 The DATA DIVISION.
Visual Basic Programming Chapter Four Notes Working with Variables, Constants, Data Types, and Expressions GROUPBOX CONTROL The _____________________________________.
CIS16 Application Development Programming with Visual Basic
Chapter 2 Variables.
Prepred By: Deborah Becker
Prepred By: Deborah Becker
Prepared By: Deborah Becker
Computing in COBOL: The Arithmetic Verbs and Intrinsic Functions
CHAPTER FOUR VARIABLES AND CONSTANTS
Chapter 2: Introduction to C++.
Topics Designing a Program Input, Processing, and Output
Topics Designing a Program Input, Processing, and Output
Primitive Types and Expressions
Visual Basic Variables
Chapter 2 Variables.
Variables in C Topics Naming Variables Declaring Variables
Visual Basic Numbers Chapter 3.3 Prepared By: Deborah 7/9/2019.
Presentation transcript:

Visual Basic Numbers Chapter 3.3 Prepared By: Deborah 1/15/2019

Topics to cover Arithmetic Operators Numeric Variables Naming Conventions Declaring Variables Prepared By: Deborah 1/15/2019

Arithmetic Operators * (Multiplication) / (Division) + (Addition) - (Subtraction) ^ (Exponential) Prepared By: Deborah 1/15/2019

Expression Evaluation In an expression in VB if No parentheses are used the equation is evaluated from left to right. The order of operations is Exponential Multiplication & Division Addition & Subtraction Prepared By: Deborah 1/15/2019

Calculations Additions + A + B Substraction - A – B Multiplication * Division / A / B Exponentiation  ^ A ^ B Left to Right Prepared By: Deborah 1/15/2019

Evaluate these equations Assume x = 2 y = 4 z = 3 X + Y ^ 2 8 / Y / X X *(X + 1) X * X + 1 Y ^ X + Z * 2 Y ^ (X + Z) * 2 (Y ^ X) + Z * 2 ((Y ^ X) + Z) * 2 Prepared By: Deborah 1/15/2019

ANSWERS 18 1 6 5 22 4 To Power of 5; multiply by 2 (2048) 38 Prepared By: Deborah 1/15/2019

VB Data Types Boolean Byte Currency Date/time Double Integer Long Single String Variant (default) 1) String or String Literal enclosed in quotes (literal) can be any characters, symbols, or numbers example: strLastName = "Satzinger" strFirstName = "John" strFullName = strFirstName & strLastName 2) Integer a whole number (no decimal place) a numeric value, stored differently no quotes example: intPageCounter = 10 intAge = 29 3) Currency a number with decimal places used in calculations, not limited to "money" (no dollar sign) examples: curSalesAmount = 21.95 curSalesTax = 1.54 curDiscountRate = .045 Prepared By: Deborah 1/15/2019

Naming Conventions bln Boolean cur Currency dbl Double dtm Date int Integer lng Long sng Single str String vnt Variant (default) Just like objects have prefixes variable also have prefixes that we will use in this class. These will help you to identify different variable within the code and save you time during the debugging phase. Not using the correct prefix for objects and variables will costs you points in your home work and on test. Prepared By: Deborah 1/15/2019

VB Numbers Numeric types in Visual Basic Integer Long Currency Single Double Variant 1 3 $3.00 5.55555 9.5555555555 Prepared By: Deborah 1/15/2019

VB Data Types Memory Reqmts. Prepared By: Deborah 1/15/2019

Naming Rules and Conventions Identifier Type Prefix Identifier (name) 1 to 255 characters Can use Letters, digits, underscores Cannot use spaces, or reserved words (Print, Name, Value, Sub, etc.) strSocialSecurityNumber curPayRate intQuanity lngPopulation curTAX_RATE (constant) Naming convention requires: 1) Identifier is meaningful -- A name that indicates the purpose 2) Begins with lowercase prefix specifying data type   Starting with VB 6.0 the lowercase prefixes are now three letters as per the Microsoft MSDN Library e.g., str  for string int  for integer cur  for currency For variables, capitalize each word of name after prefix e.g.,          curHoursWorked For constants, use full uppercase and underscore e.g.,          curMINIMUM_WAGE Prepared By: Deborah 1/15/2019

When To Use a Numeric Data Type? Only if data will be used in a calculation, otherwise use String Examples: Course Number? Social Security Number? Room Number? Age? Salary Course Hours? Pay rate Prepared By: Deborah 1/15/2019

Declaring Variables Implicit vs. Explicit Option Explicit statement required in this class!!!!! Dim statement (referring to Dimension or size) This requires explicit declaration of variables. You must declare all variables used in the program! The Auto Fill In feature help you write your dimension statements. If you find the Auto Fill In feature annoying it can be turn off by: Tools/Options/Editor Deselect the Auto List Members Dim strCustomerName as String Dim intTotalSold as Integer Dim curDailySales as Currency Prepared By: Deborah 1/15/2019

Where to declare identifiers? Identifier-another name for variable. (A variable is a data element that is part of a data file or used in calculations in the code section of your project). Variables can be declared in the programs general declaration area or in procedures. Variable declared in the GD area can be used in all procedures within the Form Variables declared in Sub Procedures can only be used within that procedure. (Scope) Scope or Focus Prepared By: Deborah 1/15/2019

Scope of Variables Visibility of Variable Can it be seen or used in this location? Global - used anywhere in project Public - used to declare global variables Module-Level Variables – used in all procedures of a form mstrFirstName (module-level string) Local – used only in the current procedure Static – used only in a procedure but retains its value while form is executing Need to control when and where a variable can be used or its value changed Global Variable (In a later chapter) Public Variable can be "seen" or used anywhere in the project (multiple forms)--Public rather than Dim Module-Level Variable or sometimes-called Form-Level Variable, are variable that can be "seen" or used anywhere in the code attached to a form. They are declared in the General Declarations of the Form. Local Variable—can be "seen" or used only in the procedure where it is declared and is Declared in the Procedure where it is used You can indicate scope in the variable name for module variable by add a m to the variable name Prepared By: Deborah 1/15/2019

curTaxes = curTaxRate * curPay Guidelines Declare identifiers for Numeric values that will be needed in your program If you need to use a literal value declare a constant in the GD Const dblFica as Double = .0745 Const strCoName as String = “My Company” In the GD area declare constant values like this Const sngTaxRate as Single = .76 Constant variable values may not be reassigned at run time. They hold the original value while the form is active. We use variables to make our programs more functional. Using literals and numeric assignment of literal values within your programs causes the program to be hard to maintain. I call these magic numbers Numeric Literals in programs require continual maintenance Always declare a variable for your data and use that variable name in your calculation, and assignment statements. This will add to the lines of code needed but it will also make you program more functional OK to assign numeric value to Label Caption lblTotal.Caption = curSalesAmount + curSalesTax HOWEVER: if the value will be used later for additional calculations it should be placed in a variable not a property.  Basic Code Example curTaxes = curTaxRate * curPay  * Do not use numeric literals in calculations Prepared By: Deborah 1/15/2019

Calculations Calculations can be preformed with variables, constants and with the properties of certain objects. The Text property of a TextBox is actually a string. If you must use the data entered into the TextBox in a calculation you must use the Type Conversion Function to convert the text to a numeric value. Prepared By: Deborah 1/15/2019

Type Conversion Functions Use conversion functions to convert String data to numeric data curSalesAmount=CDbl(txtSalesAmount.Text) Some programs declare all variables as string and use the conversion functions to do calculations on numeric data. The format is sngPrice = CSng(txtPrice.Text) Use for Validating data - (An additional process for validation will be used when the IF statement is introduced—“Is Numeric”) if user types in number to text box, fine if user types anything else, Val returns 0 this prevents error conditions (like the non-numeric data error)   Prepared By: Deborah 1/15/2019

Counting & Accumulation Sums Declare Module-Level variables in the General Declaration section msngScoreSum mintTotalCount In the TextBox procedure the basic code will look like this: We use counter to count the number of time that a sequence of code has been executed. We use accumulator variables to accumulate the value of data read into the program Usually counters and accumulators are declared as modular or static variables msngScoreSum += CSng(txtScore.Text) mintTotalCount += 1 Prepared By: Deborah 1/15/2019

Using The Format Function To format means to control the way output will look, either on the screen or on a report. VB.Net uses formatting functions to format numbers to display common formats FormatCurrency FormatNumbers FormatPercent FormatDateTime Prepared By: Deborah 1/15/2019

Formatting Data See Handout Prepared By: Deborah 1/15/2019

Format this numeric data Your rate of pay (10.50) to 2 decimal places in sngPayRate The sales tax rate (.75) as a percentage in sngSalesTax With one decimal With two decimals The dtmTodaysDate to show a long date Prepared By: Deborah 1/15/2019

Answers FormatCurrency(sngPayRate) FormatPercent(sngSalesTax) $10.50 FormatDateTime(dtmToday, vbShortDate) $10.50 75% 7.5% .75% 02/07/2005 Be careful when declaring variables as DateTime. The content of these variablse will never be Null Prepared By: Deborah 1/15/2019