What are they? Why do we need them?

Slides:



Advertisements
Similar presentations
Arithmetic Calculations
Advertisements

Integer Arithmetic. Operator Priority Real Number Arithmetic.
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.
Copyright © by Curt Hill Expressions and Assignment Statements Part 2.
Variables: Named Storage Locations Variables must be defined or declared before they can be used so that appropriate memory storage can be allocated for.
Review Question What kind error is it when I try to multiply a number in a program by 1000 and store in a variable, but the variable is too small for the.
ISBN Chapter 7 Expressions and Assignment Statements.
Chapter 3 Numerical Data. Topics Variables Numeric data types Assignment Expressions.
ISBN Lecture 07 Expressions and Assignment Statements.
1 Data types, operations, and expressions Continued l Overview l Assignment statement l Increment and Decrement operators l Short hand operators l The.
What is a variable?  A variable holds data in memory so the program may use that data, or store results.  Variables have a data type. int, boolean, char,
Copyright © 1998 by Addison -Wesley Longman, Inc. 1 Chapter 6 Arithmetic Expressions - Their evaluation was one of the motivations for the development.
1-1 University of Hail College of Computer Science and Engineering Department of computer Science and Software Engineering Course: ICS313: Fundamentals.
BUILDING JAVA PROGRAMS CHAPTER 2 PRIMITIVE DATA TYPES AND OPERATIONS.
Integer numerical data types. The integer data types The integer data types use the binary number system as encoding method There are a number of different.
Unit 3 Lesson 4 How Data Types Affect Calculations Dave Clausen La Cañada High School.
CP104 Introduction to Programming Overview of C Lecture 4__ 1 Assignment Statements An assignment statement is to store a value in a variable variable.
Arithmetic Expressions
ISBN Chapter 7 Expressions and Assignment Statements.
Chapter 7 © 1998 by Addison -Wesley Longman, Inc Arithmetic Expressions - Their evaluation was one of the motivations for the development of the.
Copyright © – Curt Hill Types What they do.
Introduction to Algorithmic Processes CMPSC 201C Fall 2000.
Copyright Curt Hill Arrays in C/C++ What? Why? How?
Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes.
Arithmetic OperatorOperationExample +additionx + y -subtractionx - y *multiplicationx * y /divisionx / y Mathematical FormulaC Expressions b 2 – 4acb *
This will all add up in the end. Assignment operator =Simple Assignment operator Arithmetic Operators +Additive operator – Subtraction operator * Multiplication.
Arithmetic Expressions Addition (+) Subtraction (-) Multiplication (*) Division (/) –Integer –Real Number Mod Operator (%) Same as regular Depends on the.
Copyright Curt Hill Casts What are they? Why do we need them?
Copyright Curt Hill The Assignment Operator and Statement The Most Common Statement you will use.
C-1 9/30/99 CSE / ENGR 142 Programming I Arithmetic Expressions © 1999 UW CSE.
CSCI 1100/1202 January 18, Arithmetic Expressions An expression is a combination of operators and operands Arithmetic expressions compute numeric.
Introduction to Algorithmic Processes CMPSC 201C Fall 2000.
Copyright © Curt Hill The Assignment Operator and Statement The Most Common Statement you will use.
1 09/10/04CS150 Introduction to Computer Science 1 What Actions Do We Have Part 2.
ISBN Chapter 7 Expressions and Assignments Statements.
7-1/27 Chapter 7 Expressions and Assignment Statements Introduction Arithmetic Expressions Overloaded Operators Type Conversions Relational and Boolean.
Copyright © Curt Hill The C++ IF Statement More important details More fun Part 3.
1 Section 3.2b Arithmetic Operators Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
Expressions and Assignment Statements
Expressions and Assignment Statements
7.2 Arithmetic Expressions
Expressions.
Chapter 7: Expressions and Assignment Statements
Lecture 3 Java Operators.
Expressions and Assignment Statements
Side Effect Operators Changing the value of variables
More important details More fun Part 3
Chapter 7: Expressions and Assignment Statements
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.
Multiple variables can be created in one declaration
Assignment and Arithmetic expressions
Expressions and Assignment Statements
Type Conversion, Constants, and the String Object
Arithmetic Operator Operation Example + addition x + y
Type Conversion, Constants, and the String Object
Increment and Decrement
Expressions and Assignment Statements
College of Computer Science and Engineering
Arithmetic Expressions & Data Conversions
Chapter-3 Operators.
What are they? Why do we need them?
Doing Arithmetic Today’s lecture is in chapter 2 Assignment statement:
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.
SSEA Computer Science: Track A
Chapter 7 Expressions and Assignment Statements.
Arithmetic Operations
Operator King Saud University
PRESENTED BY ADNAN M. UZAIR NOMAN
Arithmetic Expressions & Data Conversions
Presentation transcript:

What are they? Why do we need them? Casts What are they? Why do we need them? Copyright 2005-2007 Curt Hill

Mixed Mode Arithmetic Multiple types in one arithmetic expression 3.5 * 2 A real and integer constant Most programming languages allow some form of mixed mode C and C++ are quite liberal Java somewhat more restrictive Copyright 2005-2007 Curt Hill

Better Example Suppose: int a=2, b; double d; cin >> b >> d; d = d*b – a; a = b*d – a; What is the difference? One loses information and the other does not! Copyright 2005-2007 Curt Hill

Conversions C++ will perform certain conversions automatically Converting an integer value to a double value is automatic Doubles are stronger This conversion does not lose anything The conversion from double to int will also be done automatically Even though information is lost Copyright 2005-2007 Curt Hill

When is the conversion done? Consider the following: int a=2, b=3; double d = 3; d = a/b; What do we get? From an algebra view: 2/3 should be .6666… Actually we get zero Copyright 2005-2007 Curt Hill

Why zero? C++ only looks at the immediate pair of operands and refuses to look ahead So it first does a/b, which is 2/3 Since both are integers, we do integer division This gives a zero Copyright 2005-2007 Curt Hill

Observations C++ is very short sighted Even though the resulting expression of: d = a/b*d; is double, it does no conversion until needed It avoids some thornier problems by doing so Copyright 2005-2007 Curt Hill

So what is the fix? How do we get 2/3 instead of zero? A cast A cast is a conversion of type from one type to another The variable type is not changed! We extract the value and change the type of that value Copyright 2005-2007 Curt Hill

Styles of Casts The compiler will do a cast automatically for mixed mode arithmetic For operators convert weaker to stronger For assignments convert to receiving type C style cast C++ style cast Copyright 2005-2007 Curt Hill

The C Cast The form of a cast is the type in parenthesis This is a unary operator, it takes the operand on its right and converts the value into the appropriate type: (double)a/b It has very high precedence so it occurs before the division Copyright 2005-2007 Curt Hill

The C++ Cast C++ introduced a cast that looks like a function call or constructor double(a)/b Use the type name like a function name Precedence is not an issue This can only be used for types with a single name Use (long double)a/b for multiple names Copyright 2005-2007 Curt Hill

The 2/3 problem solved We have two ways to accomplish what we wanted: d = double(a)/b; d = (double)a/b; d = a/double(b); These work, but these do not: d = (double)(a/b); d = double(a/b); The damage is already done Copyright 2005-2007 Curt Hill

Rounding How is a double converted into an int? Truncation! (int)0.99999 is zero! How do we round? Simple: add 0.5 prior to the cast (int)(0.9999 + 0.5) is (int)(1.4999) is 1 Copyright 2005-2007 Curt Hill