Object-Oriented Programming Using C++ Third Edition Chapter 2 Evaluating C++ Expressions.

Slides:



Advertisements
Similar presentations
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.
Advertisements

Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 3 Expressions.
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.
1 9/17/07CS150 Introduction to Computer Science 1 Type Casting.
1 CS150 Introduction to Computer Science 1 Arithmetic Operators.
1 9/20/06CS150 Introduction to Computer Science 1 Review: Exam 1.
CS150 Introduction to Computer Science 1
Program Elements We can now examine the core elements of programming (as implemented in Java) We focuse on: data types variable declaration and use, constants.
CIS 234: Order of Operations, Shortcut & Other Operators Dr. Ralph D. Westfall February, 2004.
ECE122 L3: Expression Evaluation February 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 3 Expression Evaluation and Program Interaction.
Data types and variables
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.
1 Expressions, Operators Expressions Operators and Precedence Reading for this class: L&L, 2.4.
Performing Computations C provides operators that can be applied to calculate expressions: example: tax is 8.5% of the total sale expression: tax =
Operators and Expressions
Performing Simple Calculations with C# Svetlin Nakov Telerik Corporation
C++ Operators CS242 COMPUTER PROGRAMMING T.Banan Al-Hadlaq.
Chapter 4: Basic C Operators
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.
Objectives You should be able to describe: Data Types
Ryan Chu. Arithmetic Expressions Arithmetic expressions consist of operators, operands, parentheses, and function calls. The purpose is to specify an.
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
CHAPTER:8 OPERATORS AND EXPRESSION IN C++ Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्सजेंड़र ) PGT(CS),KV JHAGRAKHAND.
Chapter 2 part #4 Operator
LESSON 6 – Arithmetic Operators
CHAPTER 2 PART #4 OPERATOR 2 nd semester King Saud University College of Applied studies and Community Service Csc 1101 By: Asma Alosaimi Edited.
Chapter 2: Using Data.
1 Chapter 3 Numeric Types, Expressions, and Output Dale/Weems/Headington.
Assignment Statements Operator Precedence. ICS111-Java Programming Blanca Polo 2 Assignment, not Equals  An assignment statement changes the value of.
BUILDING JAVA PROGRAMS CHAPTER 2 PRIMITIVE DATA TYPES AND OPERATIONS.
CPS120: Introduction to Computer Science Operations Lecture 9.
CHAPTER 2 COMPONENTS OF A PROGRAMMING LANGUAGE I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
Operators Precedence - Operators with the highest precedence will be executed first. Page 54 of the book and Appendix B list C's operator precedence. Parenthesis.
1 C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating float double long.
Primitive Variables.
Chapter 6 Mathematical Operations. 6.1 Mathematical Expressions In mathematics this expression is valid 0 = -4y + 5 It is invalid in programming Left.
COMP Primitive and Class Types Yi Hong May 14, 2015.
Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes.
Arithmetic Expressions in C++. CSCE 1062 Outline Data declaration {section 2.3} Arithmetic operators in C++ {section 2.6} Mixed data type arithmetic in.
CSCI 3133 Programming with C Instructor: Bindra Shrestha University of Houston – Clear Lake.
Principles of Programming Chapter 4: Basic C Operators  In this chapter, you will learn about:  Arithmetic operators  Unary operators  Binary operators.
4. EXPRESSIONS. Display the value of pi, to 5 decimal places, right justified, in 9 columns Read in someone’s height in feet and inches using.
Module B - Computation1/61 Module-B-Computation Variables Basic Memory Operations Expressions.
Programming Fundamentals. The setw Manipulator setw changes the field width of output. The setw manipulator causes the number (or string) that follows.
Operators.
1 CS161 Introduction to Computer Science Topic #6.
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.
STRUCTURED PROGRAMMING C++ Operators. Content 2  C++ operators  Assignment operators  Arithmetic operators  Increment and decrement operators  Decision.
Programming Fundamentals. Summary of Previous Lectures Phases of C++ Environment Data Types cin and cout.
Operators A binary operator combines two values to get one result: x OP y where OP is any binary operators such as +, -, *, /, ==, !=, >, &&, or even =.
CSCI 1100/1202 January 18, Arithmetic Expressions An expression is a combination of operators and operands Arithmetic expressions compute numeric.
CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2012 CMPE-013/L Operators Gabriel Hugh Elkaim Spring 2012.
Intro. to Computer Programming Eng. Nehal A. Mohamed Spring Semester-2016.
OPERATORS IN C CHAPTER 3. Expressions can be built up from literals, variables and operators. The operators define how the variables and literals in the.
1Object-Oriented Program Development Using C++ Built-in Data Types Data type –Range of values –Set of operations on those values Literal: refers to acceptable.
ECE 103 Engineering Programming Chapter 4 Operators Herbert G. Mayer, PSU Status 6/10/2016 Initial content copied verbatim from ECE 103 material developed.
Chapter 7: Expressions and Assignment Statements
Lecture 3 Java Operators.
Computing Fundamentals
Chapter 7: Expressions and Assignment Statements
Assignment and Arithmetic expressions
Arithmetic Operator Operation Example + addition x + y
Structure of a C Program
Lecture 3 Expressions Richard Gesick.
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.
Data Types and Expressions
Using C++ Arithmetic Operators and Control Structures
Operator King Saud University
Data Types and Expressions
Data Types and Expressions
Presentation transcript:

Object-Oriented Programming Using C++ Third Edition Chapter 2 Evaluating C++ Expressions

Object-Oriented Programming Using C++, Third Edition2 Objectives Use C++ binary arithmetic operators Learn about the precedence and associativity of arithmetic operations Examine shortcut arithmetic operators Use other unary operators Evaluate Boolean expressions Perform operations on struct fields

Object-Oriented Programming Using C++, Third Edition3 Using C++ Binary Arithmetic Operators Five simple arithmetic operators: – the addition operator (+) – the subtraction operator (–) – the multiplication operator (*) – the division operator (/) – the modulus operator (%) Note that they are all binary operators

Object-Oriented Programming Using C++, Third Edition4 Using C++ Binary Arithmetic Operators (continued)

Object-Oriented Programming Using C++, Third Edition5 Using C++ Binary Arithmetic Operators (continued) Addition, subtraction, multiplication, division, or modulus of any two integers results in an integer –For example, 7 / 3 evaluates to 3 Mixed expression: operands have different data types –For example, 3.2 * 2 Unifying type: –Data type of the value that occupies more memory –All types in the expression are temporarily converted to a unifying type

Object-Oriented Programming Using C++, Third Edition6 Using C++ Binary Arithmetic Operators (continued) The order of precedence of unifying types from highest to lowest –long double –double –float –unsigned long –long –unsigned int –int –short –char

Object-Oriented Programming Using C++, Third Edition7 Explicit cast

Object-Oriented Programming Using C++, Third Edition8

9 Using C++ Binary Arithmetic Operators (continued) Cast: transform a value to another data type Implicit cast: automatic cast, or transformation, that occurs when you assign a value of one type to a type with higher precedence –int answer = 2.0 * 7 Explicit cast: deliberate cast –intResult = static_cast (doubleVariable); –intResult = (int)doubleVariable; static_cast ('A') // is 65 Preferred method in C++

Object-Oriented Programming Using C++, Third Edition10 Using Modulus Modulus (%) gives the remainder of integer division –7 % 3 results in 1 –-10 % 8 produces -2 –-10 % -8 produces -2 Can be used only with integers Can be used to extract digits from numbers –6,543 % 10 is 3 –6,789 % 10 is 9

Object-Oriented Programming Using C++, Third Edition11 Using Modulus (continued) Check digit: digit added to a number that validates the authenticity of the number A simple algorithm: 1.Assume an account number named acctNum is Remove the last digit 3.Perform modulus on the new number with an arbitrary value, say 7 ( remainder = shortAcctNum % 7 ) 4.Compare the last digit of the original account number ( lastDigit = acctNum % 10 ) with the remainder from the check digit calculation

Object-Oriented Programming Using C++, Third Edition12 Precedence and Associativity of Arithmetic Operators Multiplication, division, and modulus are said to have higher arithmetic precedence–that is, they are performed first in an arithmetic statement with multiple operations Associativity: rule that dictates the order in which an operator works with its operands –In C++, most operators have left-to-right associativity

Object-Oriented Programming Using C++, Third Edition13 Precedence and Associativity of Arithmetic Operators (continued) When C++ evaluates a mixed arithmetic expression, the following steps occur: 1.The leftmost operation with the highest precedence is evaluated a)If operands are the same type, the result is the same data type b)If the operands are different types, C++ performs an implicit cast and the result is the same type as the one that occupies more memory 2.Each subsequent *, /, or % operation is evaluated in the same manner from left to right

Object-Oriented Programming Using C++, Third Edition14 Precedence and Associativity of Arithmetic Operators (continued) Steps (continued): 3.The leftmost operation with the lower precedence (+ and –) is evaluated a)If operands are the same type, the result is the same type b)If operands are different types, C++ performs an implicit cast and the result is the same type as the one that occupies more memory 4.Each subsequent + or – operation is evaluated in the same manner from left to right Use parentheses to override precedence rules

Object-Oriented Programming Using C++, Third Edition15 Precedence and Associativity of Arithmetic Operators (continued)

Object-Oriented Programming Using C++, Third Edition16 Shortcut Arithmetic Operators Two categories of shortcut arithmetic operators are: –Compound assignment operators –Increment and decrement operators

Object-Oriented Programming Using C++, Third Edition17 Compound Assignment Operators Add and assign operator (+=) Subtract and assign operator (– =) Multiply and assign operator (*=) Divide and assign operator (/=) Modulus and assign operator (%=)

Object-Oriented Programming Using C++, Third Edition18 Increment and Decrement Operators Prefix increment operator ++count Postfix increment operator count++ Prefix decrement operator --count Postfix decrement operator count--

Object-Oriented Programming Using C++, Third Edition19 Using Shortcut Arithmetic Operators

Object-Oriented Programming Using C++, Third Edition20 Other Unary Operators Positive value operator +5 Negative value operator -8 Address operator (&) &x

Object-Oriented Programming Using C++, Third Edition21 Hexadecimal number Other Unary Operators

Object-Oriented Programming Using C++, Third Edition22 Evaluating Boolean Expressions Relational operators: evaluate the relationship between operands –Used to evaluate Boolean expressions Boolean expression: interpreted as true or false

Object-Oriented Programming Using C++, Third Edition23 Evaluating Boolean Expressions (continued) The unary operator ! is the not operator: reverses the true/false value of an expression –cout 2); displays a 1 –cout 2); displays a 0 –!0 is 1 –!1 is 0 –!5 is 0 –!-5 is 0 Don’t confuse = with ==

Object-Oriented Programming Using C++, Third Edition24 Performing Operations on struct Fields

Object-Oriented Programming Using C++, Third Edition25 Performing Operations on struct Fields (continued)

Object-Oriented Programming Using C++, Third Edition26 You Do It: Using Arithmetic Operators int a,b,c; double x,y,z; a = 13; b = 4; x = 3.3; y = 15.78; c = a + b; cout<<“a + b is ”<<c<<endl; z = x + y; cout <<“x + y is ”<<z<<endl; c = a / b; cout<<“a / b is ”<<c<<endl; c = a % b; cout<<“a% b is “<<c<<endl;

Object-Oriented Programming Using C++, Third Edition27 You Do It: Using Arithmetic Operators (continued)

Object-Oriented Programming Using C++, Third Edition28 Using Prefix and Postfix Increment and Decrement Operators a = 2; c = ++a; cout<<“a is ”<<a<<“ and c is ”<<c<<endl; a = 2; c = a++; cout<<“a is ”<<a<<“ and c is ”<<c<<endl;

Object-Oriented Programming Using C++, Third Edition29 Using Prefix and Postfix Increment and Decrement Operators (continued)

Object-Oriented Programming Using C++, Third Edition30 Using Operators with struct Fields cout<<“Please enter a student's credit hours ”; cin>>oneSophomore.creditHours; cout<<“Please enter the student's grade point average ”; cin>>oneSophomore.gradePointAverage; cout<<“The number of credit hours is ” << oneSophomore.creditHours<<endl; cout<<“The grade point average is ”<< oneSophomore.gradePointAverage<<endl; hoursRemaining = HOURS_REQUIRED_TO_GRADUATE oneSophomore.creditHours; cout<<“This student needs ”<<hoursRemaining<< “ more credit hours to graduate”<<endl;

Object-Oriented Programming Using C++, Third Edition31 Using Operators with struct Fields (continued)

Object-Oriented Programming Using C++, Third Edition32 Summary There are five simple binary arithmetic operators: addition (+), subtraction (–), multiplication (*), division (/), and modulus (%) When you mix data types in a binary arithmetic expression, the result is always the same type as the type that takes the most memory to store Several shortcut operators for arithmetic exist, such as +=, prefix ++, and postfix ++

Object-Oriented Programming Using C++, Third Edition33 Summary (continued) Boolean expression evaluates as true or false –In C++, the value 0 is always interpreted as false; all other values are interpreted as true Fields contained within structures are used in arithmetic and Boolean expressions in the same manner as are primitive variables