Sotheresultisalsoaninteger 22evaluatesto1

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: agazine C++ Expressions C++ Expressions Expressions consist of operators, literals and identifiers. Literals are direct representations of data such as 2.4, 5 or 1.0e­5 (which is the way a computer writes 1.0×10­5). Identifiers are names given to data objects in declarations. In the previous program x and y were identifiers. Operators transform data. These include +(add), * (multiply), =(assign), plus many others. Operators Operators Operators are the most interesting of the three since they transform data. Lite r als operator s ifier t den I new data Operators that operate on one operand (data object) are unary, those that operate on two operands are called binary. Mathematical Operators Mathematical Operators Operator Meaning sign change multiplication real division integer division remainder addition subtraction Arity unary binary binary binary binary binary binary Placement prefix infix infix infix infix infix infix Precedence 15 13 13 13 13 12 12 – * / / % + – The Minus Operators – The Minus Operators – Two applications of “–” The difference is determined by whether the “–” is in front of a single literal or identifier or between two. int x, y, z; x = 5; y = – x; // here the “–” is a sign change z = x – 3; // here the “–” is a subtraction The Division Operators / The Division Operators / There are two “/” operators Integer division occurs if both operands are integers In this case the result is only the whole number part of the result. So the result is also an integer. 2/2 evaluates to 1; 5/2 evaluates to 2; 4/2 evaluates to 2 2/5 evaluates to 0 The Division Operators / The Division Operators / There are two “/” operators Real division occurs if any of the operands are not integers. Any of the operands that are not integers are converted to doubles before dividing 2/2 evaluates to 1; evaluates to 2 2.0/2.0 evalua...
View Full Document

This note was uploaded on 03/12/2010 for the course ENGIN 101 taught by Professor Jeffringenberg during the Fall '07 term at University of Michigan.

Ask a homework question - tutors are online