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.

Slides:



Advertisements
Similar presentations
Arithmetic Calculations
Advertisements

Types and Arithmetic Operators
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 3 Expressions.
Variables: Named Storage Locations Variables must be defined or declared before they can be used so that appropriate memory storage can be allocated for.
C Language Elements (II) H&K Chapter 2 Instructor – Gokcen Cilingir Cpt S 121 (June 22, 2011) Washington State University.
1 9/17/07CS150 Introduction to Computer Science 1 Type Casting.
Introduction to Computers and Programming Lecture 4: Mathematical Operators New York University.
1 CS150 Introduction to Computer Science 1 Arithmetic Operators.
COMPSCI 125 Spring 2005 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 3: Numeric Data *Variables *Numeric data.
CS150 Introduction to Computer Science 1
Chapter 3 Numerical Data. Topics Variables Numeric data types Assignment Expressions.
Expressions An expression is a sequence of operands and operators that reduces to a single value expression operator operand An operator is a language-specific.
More about Numerical Computation CS-2301, B-Term More about Numerical Computation CS-2301, System Programming for Non-Majors (Slides include materials.
Order of Operations Using Integers. “Operators” & “Terms”… TermsOperators.
Representation and Conversion of Numeric Types 4 We have seen multiple data types that C provides for numbers: int and double 4 What differences are there.
Performing Computations C provides operators that can be applied to calculate expressions: example: tax is 8.5% of the total sale expression: tax =
Copyright © 2003 Pearson Education, Inc. Slide 2-1 Problem Solving with Java™ Second Edition Elliot Koffman and Ursula Wolz Copyright © 2003 Pearson Education,
***** SWTJC STEM ***** Chapter 2-3 cg 29 Java Operators Recall Java’s programming components: Packages - Collection of classes (Programs) Classes - Collections.
Chapter 2 part #4 Operator
Object-Oriented Programming Using C++ Third Edition Chapter 2 Evaluating C++ Expressions.
Chapter 3 Processing and Interactive Input. 2 Assignment  The general syntax for an assignment statement is variable = operand; The operand to the right.
CHAPTER 2 PART #4 OPERATOR 2 nd semester King Saud University College of Applied studies and Community Service Csc 1101 By: Asma Alosaimi Edited.
D-1 University of Washington Computer Programming I Lecture 4: Arithmetic Expressions © 2000 UW CSE.
1-1 Variables and Expressions Algebra One CP2 Chapter 1.
CPS120: Introduction to Computer Science Operations Lecture 9.
CP104 Introduction to Programming Overview of C Lecture 4__ 1 Assignment Statements An assignment statement is to store a value in a variable variable.
CHAPTER 2 COMPONENTS OF A PROGRAMMING LANGUAGE I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
Chapter 7 Expressions and Assignment Statements. Outline Introduction Arithmetic Expressions Overloaded Operators Type Conversions Assignment Statements.
Programming with Visual C++: Concepts and Projects Chapter 3A: Integral Data (Concepts)
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.
Copyright Curt Hill Casts What are they? Why do we need them?
D-1 University of Washington Computer Programming I Lecture 4: Arithmetic Expressions © 2000 UW CSE.
ECE 103 Engineering Programming Chapter 4 Operators Herbert G. Mayer, PSU CS Status 6/19/2015 Initial content copied verbatim from ECE 103 material developed.
Operators A binary operator combines two values to get one result: x OP y where OP is any binary operators such as +, -, *, /, ==, !=, >, &&, or even =.
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.
1 09/10/04CS150 Introduction to Computer Science 1 What Actions Do We Have Part 2.
ISBN Chapter 7 Expressions and Assignments Statements.
What will each of the following lines print? System.out.println("number" ); number645 System.out.println("number" + (6 + 4)+ 5); number105 System.out.println(6.
CompSci 230 S Programming Techniques
Chapter 2 Elementary Programming
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
What are they? Why do we need them?
Fundamental Data Types
Arithmetic Operator Operation Example + addition x + y
Order of Operations Using Integers
Conversions of the type of the value of an expression
Structure of a C Program
Lecture 3 Expressions Richard Gesick.
Arithmetic Expressions in C
3-3 Side Effects A side effect is an action that results from the evaluation of an expression. For example, in an assignment, C first evaluates the expression.
Numerical Data Types.
Arithmetic Expressions & Data Conversions
3-3 Side Effects A side effect is an action that results from the evaluation of an expression. For example, in an assignment, C first evaluates the expression.
Order of Operations Using Integers
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.
Chapter 3 Operators and Expressions
Data Types and Expressions
Fundamental Data Types
Arithmetic Operations
Operator King Saud University
Evaluating Expressions
is multiplied by a variable of type to yield a result of type
Data Types and Expressions
Arithmetic Expressions & Data Conversions
Data Types and Expressions
Presentation transcript:

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 number to fit? Old Odometer problem.

Arithmetic Expressions 02/04/11

Programs do Calculations Example in cs117/ch2/circle.cpp

Arithmetic Operators Algebra +, - unary x, ÷ +, - binary x n C++ +, - unary *, /, % +, - binary no exponent

Precedence 1. Parenthesis 2. Unary +, - 3. *, /, % 4. Binary +, -

Values of the Following? 50%20 = 20.0 – 6.0/ = 20.0 – 6.0/( ) = 5 + -a * 14= ?,assume a = -2

Associativity Left to right x + y + z Right to left --z x = y = 0;

No Implied Multiplication x = 2(y + z); //Error x = 2*(y + z); //Ok

Assignment Operator = variable = expression expression on right evaluated result stored in variable int x, y; x = 9*(7+4); y = x + 20;

Example

int operations int i = 5; int j = 3; int k; k = i / j; //Result of divide truncated k = i % j; Result int when operands are int

Careful with Integer Fractions

Fraction -- What’s Wrong?

Implicit Type Conversion Assume variables int k = 5, m = 4, n ; double x = 1.5, y = 2.1, z; Type automatically converted n = x; z = m;

Implicit Type Conversion Assume variables int k = 5, m = 4, n ; double x = 1.5, y = 2.1, z; z = k /m; //Result of op. Same as Operands y = m + 1.5; // mixed types

Explicit Type Conversion

int kids, families; double average; kids = 21; families = 8; average = (double)kids/families;

Next Additional Operations – Functions Read Chapter 3 for Wednesday

Exercises for Study p. 55, #1,3, 4