Data Types, Variables & Arithmetic

Slides:



Advertisements
Similar presentations
Types and Arithmetic Operators
Advertisements

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.
Variables: Named Storage Locations Variables must be defined or declared before they can be used so that appropriate memory storage can be allocated for.
Javascript Essentials How do I write it??  Start Homesite  Between the start and end BODY tags type: 
Types and Variables. Computer Programming 2 C++ in one page!
Computer Science 1620 Other Data Types. Quick Review: checklist for performing user input: 1) Be sure variable is declared 2) Prompt the user for input.
© 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5/e Starting Out with C++: Early Objects 5 th Edition Chapter 2 Introduction.
COMPSCI 125 Spring 2005 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 3: Numeric Data *Variables *Numeric data.
Chapter 3 Numerical Data. Topics Variables Numeric data types Assignment Expressions.
Basic Elements of C++ Chapter 2.
Input & Output: Console
1 Number Types  Every value in Java is either: 1.a reference to an object or 2.one of the eight primitive types  eight primitive types: a.four integer.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Summary of what we learned yesterday Basics of C++ Format of a program Syntax of literals, keywords, symbols, variables Simple data types and arithmetic.
Arithmetic Operations. Review function statement input/output comment #include data type variable identifier constant declaration.
C++ Programming: Basic Elements of C++.
Copyright © 2012 Pearson Education, Inc. Chapter 2: Introduction to C++
Chapter 2 Overview of C++. 2 Overview  2.1 Language Elements  2.2 Reserved Words & Identifiers  2.3 Data Types & Declarations  2.4 Input/Output 
Chapter 3 – Variables and Arithmetic Operations. Variable Rules u Must declare all variable names –List name and type u Keep length to 31 characters –Older.
Data Types Declarations Expressions Data storage C++ Basics.
Copyright © – Curt Hill Types What they do.
Computing and Statistical Data Analysis Lecture 2 Glen Cowan RHUL Physics Computing and Statistical Data Analysis Variables, types: int, float, double,
Operators & Identifiers The Data Elements. Arithmetic Operators exponentiation multiplication division ( real ) division ( integer quotient ) division.
Operators and Expressions. 2 String Concatenation  The plus operator (+) is also used for arithmetic addition  The function that the + operator performs.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
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.
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
Variables, Operators, and Expressions
Today Variable declaration Mathematical Operators Input and Output Lab
Chapter 2 Variables.
Chapter Topics The Basics of a C++ Program Data Types
Expressions.
Data Types and Expressions
Chapter 2 Introduction to C++ Programming
Chapter 2: Introduction to C++
Expressions.
Computing and Statistical Data Analysis Lecture 2
BASIC ELEMENTS OF A COMPUTER PROGRAM
INSPIRING CREATIVE AND INNOVATIVE MINDS
Tokens in C Keywords Identifiers Constants
Variables A variable is a placeholder for a value. It is a named memory location where that value is stored. Use the name of a variable to access or update.
ITEC113 Algorithms and Programming Techniques
Basic Elements of C++.
Expressions An expression is a portion of a C++ statement that performs an evaluation of some kind Generally requires that a computation or data manipulation.
Assignment and Arithmetic expressions
Identifiers - symbolic names
Java Programming: From Problem Analysis to Program Design, 4e
Fundamental Data Types
Chapter 2: Introduction to C++
Basic Elements of C++ Chapter 2.
Lecture 3 Expressions Richard Gesick.
Basic Notions Review what is a variable? value? address? memory location? what is an identifier? variable name? keyword? what is legal identifier? what.
2.1 Parts of a C++ Program.
Chapter 2 Variables.
Lectures on Numerical Methods
CSE 100 Data Types Declarations Displays.
Expressions and Assignment
CS150 Introduction to Computer Science 1
Expressions An expression is a portion of a C++ statement that performs an evaluation of some kind Generally requires that a computation or data manipulation.
Chapter 2: Introduction to C++.
Data Types and Expressions
Fundamental Data Types
Engineering Problem Solving with C++ An Object Based Approach
Engineering Problem Solving with C++ An Object Based Approach
Summary of what we learned yesterday
Primitive Types and Expressions
Chapter 2 Variables.
Data Types and Expressions
Data Types and Expressions
Presentation transcript:

Data Types, Variables & Arithmetic

C++ Integer Types Data Size in signed unsigned Type bytes Range Range short 2 ± 32,767 0 to 65,553 int 4 ~ ±2 billion 0 to ~4 billion long 4 (same as int, currently) long long 8 ~ ± 9x1018 0 to ~1.8x1019 On a 64-bit CPU. Size/Range vary by CPU model and Word size.

C++ Integer Types signed vs. unsigned unsigned short x; //range 0 to 65553 signed short x; //range ± 32767 short x; //assumed signed

C++ Float Types Type Size # significant digits float 4 bytes 7 double 8 bytes 15 There are (usually) no unsigned floats or doubles. Using unsigned float in a program will result in: - a compiler error (must be corrected) or - a compiler warning (compiler ignores unsigned)

Floats are stored in scientific notation as two integer values C++ Float Types Floats are stored in scientific notation as two integer values Mantissa: digits with an assumed radix point after the first digit. Exponent: (actually an exponent of 2 in binary) Example: (in decimal) 1234.567 = 1.234567 x 103 Mantissa = 1234567 Exponent = 3

Addition in Scientific Notation C++ Float Types Addition in Scientific Notation 1.234567 x 104 Exponents must match! + 7.654321 x 102 (4 - 2 = 2) 123.456700 x 102 Move radix point, subtract from exponent + 7.654321 x 102 131.110321 x 102 Add the mantissas, keep the common exponent

A CPU must go through a very similar process to add two Floats! C++ Float Types Addition in Scientific Notation 131.110321 x 102 1.31110321 x 104 Convert the result to Scientific Notation 1.31110321 x 104 Round off to 7 significant digits 1.311103 x 104 Done! A CPU must go through a very similar process to add two Floats!

Integer vs. Float Consideration Integer Float Increasing Size increases Range increases number of significant digits Arithmetic fast and easy slow and complicated Precision always precise imprecise due to max number of significant digits Range Limited (small) Unlimited (practically)

General Design Principles Integer vs. Float General Design Principles - Use Integers unless Floats are needed - Use larger Integer types when increased Range (larger numbers) is needed - Use larger Float types when increased Precision (number of significant digits) is needed.

Character Types char exactly one character string a sequence of 0 or more characters String variables are not native to C++; they are an Extension. To use this type, you must: #include <string>

Variable A variable is a container for data that has a NAME: an identifier (created by the programmer) (DATA) TYPE: describes data values and operations CONTENTS: can change during execution of the program Variables must be Declared before used!

Declaration Statement Syntax: type identifier; type identifier = literal; type id1, id2, id3; type id1 = lit1, id2 = lit2, id3 = lit3;

Declaration Statement Semantics: type identifier = literal; - Creates a new variable named identifier of type type - Sets the initial value of the variable to literal

Declaration Statement Semantics: Example: int a = 3; 1. Memory is allocated (“set aside”), the size of an int. 2. The location is “named” a. 3. The value 3 is placed in the location.

Declaration Statement Style: Create meaningful variable names. double length, width, area; Not double l, w, a;

Declaration Statement Examples: int length; long long width = 0; float radius = 3.25; double length = 2.5, width = 3.75, height = 0; char letterGrade = ’A’; string our_cheer = ”Go Big Blue!”;

Assignment Statement Purpose: change the value of a variable Syntax: variable = expression; - The variable must already be declared - The expression must evaluate to a value that is compatible with the type of the variable.

Assignment Statement Syntax: “compatible” usually means “same type”, as for char and string. char letterGrade = ’A’; string our_cheer = ”Go Big Blue!”; Not char letterGrade = ”A”; string our_cheer = ’Go Big Blue!’; letterGrade = our_cheer;

Assignment Statement Syntax: floats and integers are generally cross-“compatible” syntactically, but semantically there are caveats ("dangers"). double y = 3; // no problem short x = 2.5;//OK, BUT see below

Assignment Statement Semantics: variable = expression; - Evaluate the expression on the right. - When the types differ but are compatible, convert the expression’s type to the variable’s type. - Change the current value of the variable to the evaluated result.

Assignment Statement Semantics: Warning on Float-to-Integer! Float values are truncated (not rounded). int x = 2.9; // sets x to 2 double y = 3.6; int z = y; // sets z to 3 cout << x << " " << y; Prints: 2 3

Arithmetic Operators +, -, *, / work as expected except: - integer / integer results in an integer - Mixed operands (one int, one float) always results in a float. double x = 2/3; // int/int->int cout << x; // prints 0 x = 2/3.0; // int / float -> float cout << x; // prints 0.6666

Arithmetic Modulus Operator for integers: % int dividend, remainder; dividend = 7 / 5; // dividend = 1 remainder = 7 % 5; // remainder = 2

Arithmetic Order of Precedence: First: * , / , % left to right Second: + , - left to right int x; x = 5 + 2 * 4 – 6 / 3; // x = 11 // x = 5 + (2*4) – (6/3) // x = 5 + 8 – 2 // x = 11

Arithmetic Parentheses may be used double x; x = (3 + 2) * 5; // x = 25 X = 3 + 2 * 5; // x = 13

Arithmetic Raising to an Exponent, Square Root, Round double x = sqrt(16); // x = 4 double y = pow(3,2); // y = 9 double z = round(2.8); // z = 3.0 double w = round(2.2); // w = 2 double d = round(3.12345 * 100) / 100; // d = 3.12

Abbreviated Assignment Increment and Decrement int y=0, x=0; x++; // x = x + 1; ++x; // x = x + 1; y = x++; // y = x; x = x + 1; y = ++x; // x = x + 1; y = x; x--; // x = x – 1; --x; // x = x – 1;

Abbreviated Assignment += -= *= /= int X=1; X += 2; // x = x + 2; X -= 2; // x = x – 2; X *= 2; // x = x * 2; X /= 2; // x = x / 2;

Constants Definition: An identifier with a value that can never change. Syntax and Semantics: Same as a variable declaration with an initial value, but with the reserved word const in front: const type identifier = literal;

Constants Example: const double tax_rate = 0.06; … // syntax error: value cannot be changed tax_rate = 0.07;

Constants Why use constants? 1. Clarity 2. Consistency 3. Maintainability

Constants Clarity – how well a programmer can understand a program due to use of good style. extra = subtotal * 0.06; What is 0.06? A tax rate? Interest rate? Fee rate? const float tax_rate = 0.06; … tax = subtotal * tax_rate; Now we know!

Constants Consistency – when a value that should be exactly the same throughout a program actually is. area = 3.14 * radius * radius; circumference = 2 * 3.1415 * radius; sphere_surface = 4 * 3.1415926 * radius * radius; The value of π is not consistent…this is better: const double pi = 3.1415926; area = pi * radius * radius; circumference = 2 * pi * radius; sphere_surface = 4 * pi * radius * radius;

Constants Maintenance Any change to a program after it is “finished”. Maintainability The ease/speed of doing Maintenance.

Constants Maintainability Example In 2013, by coincidence, the state and local rates were the same state_tax = subtotal * 0.06; local_tax = subtotal * 0.06; In 2016, the local tax rate changed to 0.07 (but the state rate remained at 0.06) Now we have to go through all programs, find all 0.06’s and decide if each needs to change to 0.07 or not.

Constants Maintainability Example This would have been better: const double state_tax_rate = 0.06; const double local_tax_rate = 0.06; … state_tax = subtotal * state_tax_rate; local_tax = subtotal * local_tax_rate; Now, all we have to do is change the constant’s value in one place, and re-compile all programs that use it.

Vocabulary Term Definition Variable Container for data: has a name, type and current contents. Declaration Statement that defines a variable (name, type, initial variable) and allocates the variable during execution. Allocate During execution, the reservation of memory location(s) represented by a variable. Assignment Statement that changes the value (current contents) of a variable. Mantissa The significant digits of a number in Scientific Notation, with an assumed radix point after the first digit. Modulus Operator that calculates the remainder of an integer division. % in C++ Constant An identifier with a declared type and value that cannot change.

Vocabulary Term Definition Clarity How well a programmer can understand a program due to use of good style. Consistency When a value that should be the same throughout a program actually is. Maintenance Any changes to a program after it is “finished”. Maintainability The ease/speed of changing a program after it is “finished”.