IS 350 Numeric Data Types.

Slides:



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

Chapter 3: Using Variables and Constants Programming with Microsoft Visual Basic 2005, Third Edition.
Lecture Set 4 Data Types and Variables Part B – Variables, Constants, Expressions Conversion Rules Options Strict, Option Explicit Scope of Definition.
Chapter 3 Numerical Data. Topics Variables Numeric data types Assignment Expressions.
Data types and variables
Chapter 2 Data Types, Declarations, and Displays
Chapter 3: Introducing the Microsoft.NET Framework and Visual Basic.NET Visual Basic.NET Programming: From Problem Analysis to Program Design.
Chapter Three Using Variables and Constants Programming with Microsoft Visual Basic th Edition.
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.
Chapter 4 Introduction to Numeric Data Types and Variables.
Objectives You should be able to describe: Data Types
Variables and Constants
Chapter 3: Using Variables and Constants
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Basic Notions Review what is a variable? value? address? memory location? what is an identifier? variable name? keyword? what is a legal identifier? what.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Input, Output, and Processing
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.
Operators & Identifiers The Data Elements. Arithmetic Operators exponentiation multiplication division ( real ) division ( integer quotient ) division.
Operators & Identifiers The Data Elements. Arithmetic Operators exponentiation multiplication division ( real ) division ( integer quotient ) division.
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 3 Variables, Constants, Methods, and Calculations.
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Three Memory Locations and Calculations.
Chapter 4 Variables and constants. 4.1 Variables -Use of variables is good programming style -easier to modify -easier for a programmer to understand.
Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Three Using Variables and Constants.
Operators & Identifiers The Data Elements. Arithmetic Operators exponentiation multiplication division ( real ) division ( integer quotient ) division.
Programming with Microsoft Visual Basic th Edition
Module B - Computation1/61 Module-B-Computation Variables Basic Memory Operations Expressions.
Introduction ABAP Fields and Variables. Slide 2 Fields (Introduction) In ABAP, fields (or data objects) are named locations in memory Variables store.
Tokens in C  Keywords  These are reserved words of the C language. For example int, float, if, else, for, while etc.  Identifiers  An Identifier is.
Variables and Expressions Programming Right from the Start with Visual Basic.NET 1/e 7.
Microsoft Visual Basic 2012 CHAPTER FOUR Variables and Arithmetic Operations.
Chapter 3: Introducing the Microsoft.NET Framework and Visual Basic.NET Visual Basic.NET Programming: From Problem Analysis to Program Design.
Chapter 4 Chapter 4: Variables, Constants, and Arithmetic Operators.
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.
Programming with Microsoft Visual Basic 2012 Chapter 3: Using Variables and Constants.
© 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.
2.1 The Part of a C++ Program. The Parts of a C++ Program // sample C++ program #include using namespace std; int main() { cout
Microsoft Visual Basic 2010 CHAPTER FOUR Variables and Arithmetic Operations.
28 Formatted Output.
Egyptian Language School Computer Department
A variable is a name for a value stored in memory.
Topics Designing a Program Input, Processing, and Output
Arithmetic operations and operators, converting data types and formatting programs for output. Year 11 Information Technology.
Data Types, Variables & Arithmetic
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 and Arithmetic Operations
Chapter 3: Using Variables and Constants
Computing with C# and the .NET Framework
Lecture Set 4 Data Types and Variables
Data Types and Variables Part D – Common Methods (ToString and Parse)
Variables and Arithmetic Operations
IDENTIFIERS CSC 111.
Chapter 6 Variables What is VBScript?
Basic Notions Review what is a variable? value? address? memory location? what is an identifier? variable name? keyword? what is legal identifier? what.
Introduction to Tools, Numeric Data Types and Variables
CIS16 Application Development Programming with Visual Basic
Chapter 2 Variables.
C++ Data Types Data Type
Data Types and Variables Part A – Introduction Numeric Data Types
Introduction to Java Applications
Topics Designing a Program Input, Processing, and Output
Data Types and Expressions
Topics Designing a Program Input, Processing, and Output
An Introduction to Programming with C++ Fifth Edition
Presentation transcript:

IS 350 Numeric Data Types

Objectives Work with numeric data Describe the difference between value types and reference types Declare and use variables to store data Create constants Write expressions containing arithmetic operators and operands

Objectives (continued) Convert data between numeric and String data types Use the FV method Use the TextBox control Define the order in which control instances get input focus (Tab Order)

Introduction to Data Types Every object and property has a data type A data type defines what kind of data an object or property can store Data types that store numbers are called numeric data types Integral data types store whole numbers Floating-point data types store values having a fractional part Other data types store strings Many other additional data types exist

Integral Data Types Integral data types store data that does not have a decimal point Some integral data types can store 0 or positive values Other integral data types can store both positive and negative values Negative values are indicated by a sign bit

Integral data types

Integral Data Types Integral values are stored as binary numbers The SByte data type stores its data in 8 bits 7 bits store the data 1 bit stores the value's sign The largest possible value is 2^7-1 or 127 The value has the binary pattern 01111111

Floating-point Data Types Floating-point numbers store values with a decimal point Floating-point numbers are stored in a form of scientific notation One bit is the sign bit A 23-bit mantissa stores the fractional part of a number The exponent is stored in 8 bits

Floating-point data types

Rounding Errors Floating-point numbers are an approximation of an actual value 1/3 or 2/3 must be rounded, for example Rounding introduces an error called rounding error The greater the precision of the data type, the smaller the rounding error The Decimal data type has the smallest rounding error and the greatest precision

Performance of Numeric Data Types Performance is a key characteristic of any application Different data types have different performance characteristics Arithmetic operations using integral data types is faster than operations on floating-point data types The Decimal data type is very slow Decimal prevents rounding error

Properties of Numeric Data Types Numeric data types have constant fields that store the minimum and maximum possible values for the data type MinValue stores the minimum possible value MaxValue stores the maximum possible value Primary data types are structures having constant values

Introduction to the String Data Type Data is often represented as a string of characters Textual data is represented as a string Data entered by end users is textual Strings are used to display output Strings are represented differently than numbers (numeric data types) Much more later

Storage of the String Data Type Strings are stored as Unicode characters in two bytes as an unsigned short (UShort) The first 127 values store letters, digits, and special characters Values 128 to 255 store special characters such as keyboard arrows Values 256 to 65,535 store international characters and diacritical marks

Introduction to Value Types and Reference Types A data type can be characterized as a value type or reference type Value types store data in the memory allocated to the variable Reference types store a 32-bit memory address The memory address points to the actual object

Value Types All value types derive from the System.ValueType class Integral and floating-point data types are value types Dates and times are value types Structures are value types

Storage of value types

Reference Types All reference type variables are 32 bits in size The value stored in a reference type variable is a memory address This memory address points to the memory allocated to the actual data The term pointer is often used to describe a reference type

Storage of reference types

Introduction to Variables A variable stores data while an application runs The process of creating a variable is called declaring a variable A variable has a name (identifier) Naming rules for variables and procedures are the same A variable has a data type

Variable Characteristics All variables have three characteristics Lifetime Scope Accessibility

Variable Lifetime Variable lifetime refers to the period of time that a variable exists A variable having local lifetime exists only while a procedure executes A variable with a module-level lifetime exists while a class instance exists or a module is loaded

Variable Scope and Accessibility Variable scope refers to the statements that can use a variable Variables declared inside a procedure with the Dim statement have local scope Variables declared outside a procedure with the Private statement have module-level scope

Variable Declaration Syntax [Dim] varName As type [= initexpr] Dim keyword declares a local variable The variable and its value are destroyed when the procedure ends varName contains the name (identifier) of the variable type defines the variable’s data type Optional initexpr is initializes the variable’s value

Declaring a Local Variable (example) Declare a local variable named Total having the Double data type Sub Main() Dim Total As Double End Sub 25

Initializing Variables Variables can be declared and initialized in the same statement Example: Declare a variable named CurrentYear and initialize it’s value to 2010: Dim CurrentYear As Integer = 2010

Common Initialization Errors Numeric initialization values cannot contain formatting characters The following statements are illegal: Dim Value1 As Double = 100,000.52 Dim Value2 As Double = $100,000.52

Declaring Multiple Variables in One Statement Multiple variables can be declared in the same statement Separate each declaration with a comma as follows: Dim IntegerValue As Integer, DoubleValue As Double If variables have the same data type, use the following shorthand notation: Dim IntegerValue1, IntegerValue2 As Integer

Variable Naming Conventions A scheme of naming variables is called a naming convention Naming conventions improve a program’s readability Common naming conventions Hungarian notation Pascal case Camel case

Naming Conventions (description) Hungarian notation: Use a prefix to denote the data type followed by a descriptive name Pascal case Use whole words Capitalize the first letter of each word Camel case Use lower case for the first letter of the first word Capitalize the first letter of each remaining word

Introduction to Constants Constants are similar to variables The value of a constant does not change while an application runs Trying to assign a value to a constant will cause an error Use the Const statement to declare a constant Constants are typically declared at the module-level

User-defined Constant (example) Declare a constant to store the value of PI Private Const PI_VALUE As Single _ = 3.14159 Declare a constant to store the value of PI multiplied by 2 Private Const TWO_PI_VALUE As Single _ = PI_VALUE * 2

Introduction to Expressions Variables can be used in assignment statements along with object properties Examples: Dim Result As Integer Dim Example As Integer = 4 Result = 3 ' Literal value Result = txtExample.Height ' Property Result = Example ' Another variable

Common Expression Errors Values on the right and left sides of an assignment statement must have the same data type The Font and Integer data types are not compatible so the following statements will cause an error: Dim SomeInteger As Integer SomeInteger = txtExample.Font

Parts of an Expression Operators Operands Arithmetic operators such as (+, -, *, /, ^, \) Comparison and logical operators Operands Literal values Object properties Constants Variables

Operator Precedence Operators have an evaluation order called precedence Visual Basic precedence rules are the same as algebraic precedence rules Use parentheses to override the default precedence rules Use parentheses to clarify evaluation order Extra parentheses do no harm

Operators and Precedence Programming with Microsoft Visual Basic 2008: An Object-Oriented Approach, Third Edition

Arithmetic Operators (example) Divide 10 by 5 and multiply the intermediate result by 100 The value 200 is stored in Result Dim Numerator As Double = 10 Dim Denominator As Double = 5 Dim Result As Double Result = Numerator / Denominator * 100

Integer Division The result of an Integer division operation is an integral value The result is truncated rather than rounded Examples: Result = 4 \ 3 ' 1.33 -> 1 Result = 5 \ 2 ' 2.5 -> 2 Result = 14 \ 4 ' 3.5 -> 3

The Mod Operator Mod operator calculates the Integer remainder of a division operation The operation is the same as the remainder resulting from a long division operation Examples: Result = 4 Mod 3 ' Remainder is 1 Result = 14 Mod 4 ' Remainder is 2

Operator Precedence Arithmetic operators are applied in a default order known as precedence Exponentiation Multiplication and division Integer division Mod operator Addition and subtraction Use parentheses to change the default precedence

Numeric Data Types and Type Conversion Type conversion is used to convert data from one data type to another How type conversion is performed depends on whether strict type checking is enabled or disabled If strict type checking is enabled, less restrictive types will not be implicitly converted to more restrictive types Strings will not be implicitly converted to numeric data types

Introduction to Strict Type Checking Widening type coercion allows more restrictive types to be implicitly converted to less restrictive types Integral data types will be converted to floating-point types Short to Integer is legal Integer to Single is legal Single to Double is legal Double to Integer is not legal Integer to Short is not legal

Enabling Strict Type Checking Enable strict type checking to prevent hard to find type conversion errors Option Strict statement enables or disables strict type checking Option Strict statement must appear at the beginning of a module Example: Option Strict On ' Enable Option Strict Off ' Disable

Explicit Type Conversion Methods ToInt16, ToInt32, and ToInt64 convert the argument to an integral value ToDecimal converts the argument to the Decimal data type ToDouble and ToSingle convert the argument to floating-point data ToString converts the argument to a string

Performing Explicit Type Conversion Members of the System.Convert class explicitly convert data from one type to another Methods exist for each primary data type Example: Dim DoubleValue As Double = 123.99 Dim IntegerResult As Integer IntegerResult = _ System.Convert.ToInt32(DoubleValue)

Formatting Output Strings The ToString method accepts an argument to format numeric data Named formats supply common formatting actions Currency and Percent for example Custom formats are created with placeholder characters

Format specifiers

Placeholder characters

The Imports Statement Use the Imports statement to eliminate fully qualified references to method names The Imports statement appears after the Option Explicit and Option Strict statements It’s possible to import any number of namespaces or classes The order in which Imports statements appear is not significant Types can also be imported using the Project Properties dialog box

Imports Statement (example) Import the System.Convert class Option Explicit On Option Strict On Imports System.Convert Public Class frmMain ' statements End Class

The FV Method and Intrinsic Function The FV function calculates the future value of an investment Arguments: First argument contains the interest rate Second argument contains the number of periods Third argument contains the amount of the periodic payment Optional fourth argument contains the present (initial) value of the investment Optional fifth argument defines whether payments are made at the beginning or end of the period

The FV Function (Example) Calculate the future value of $1,000.00 for 12 periods at 1% per period Dim MonthRate As Integer = 0.01 Dim MonthTerm As Integer = 12 Dim InitialValue As Single = 1000.00 Dim FutureValue As Double FutureValue = FV(0.01, 12, 0, 1000) FutureValue = FV(MonthRate, MonthTerm, 0, _ InitialValue) ' -1,126.83.

The TextBox Control (Introduction) The TextBox control gets input and displays output The control derives from the TextBoxBase class Other textual controls include the MaskedTextBox and RichTextBox controls

The TextBox Control (properties) BorderStyle - defines the style of the border ForeColor and BackColor - define text and background colors Enabled - defines whether the control instance can get input focus ReadOnly - controls whether the text in the control instance can be copied to the clipboard MultiLine - defines whether text will appear on multiple lines MaxLength - defines the maximum number of characters

The TextBox Control (properties continued) ScrollBars - defines whether scroll bars appear Scroll bars only appear when the MultiLine property is True Text - stores the visible text TextAlign - defines how text is aligned in the control instance The size and position is defined by the X, Y, Height and Width properties

The TextBox Control (Methods and Events) Focus - used to set input focus to a text box Enter - fires when the control instance gets focus Leave - fires when the control instance loses focus

Introduction to Tab Order Every form has exactly one active control instance at run time This is the control instance having input focus The order in which control instances get focus is called the tab order tab order is set using the TabIndex property tab order can also be set using a visual designer