lecture5

# lecture5 - CMPT 128 Introduction to Computing Science for...

This preview shows pages 1–13. Sign up to view the full content.

1 © Janice Regan, CMPT 128, Sept 2007 CMPT 128: Introduction to Computing Science for Engineering Students Arithmetic Expressions and Arithmetic Operators

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
© Janice Regan, CMPT 128, Sept 2007 2 Components of a C++ Program More types of tokens (smallest individual units of a program) include: Other separators blanks, tabs, line feed, blank lines … Operators actions used to combine constants = and + in the expression y = x + THREE;
© Janice Regan, CMPT 128, Sept 2007 3 C++: Binary Arithmetic Operators A Binary Operator operates on two arguments + addition - subtraction * multiplication / division % modulus or remainder (only for integers, 5%2=1) Evaluated left to right

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
© Janice Regan, CMPT 128, Sept 2007 4 Unary Arithmetic Operators in C++ A Unary Operator operates on one argument + positive - negative ++ increment -- decrement ~ ones complement Evaluated right to left
© Janice Regan, CMPT 128, Sept 2007 5 Expressions in C++ An expression can be a single variable, or can be a series of variables combined using operators Arithmetic expressions manipulate numeric variables following the rules of algebra Relational expressions compare numerical values and give a logical answer (more later) The two variables operated on by a binary arithmetic or relational operator should both have the same type If they have different types one must have it’s type converted to the type of the other before the operation is attempted

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
© Janice Regan, CMPT 128, Sept 2007 6 Precedence of operators in C++ 1. ( ) [] . innermost first static_cast 2. ++ -- (pre) + - ! ~ & *(unary operators) 3. * / % 4. + - 5. = += -= *= /= %= Evaluate 2 and 5 right to left Evaluate 1, 3, and 4 left to right
© Janice Regan, CMPT 128, Sept 2007 7 Expressions: arithmetic operators A + B + C X + C Let A=10, B=5, C=2 + A B X C + 10 15 5 2 17 A B C + + Value of expression

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
© Janice Regan, CMPT 128, Sept 2007 8 Expressions: arithmetic operators Order of operations is determined by operator precedence rules / before + A + B / C A + X Let A=10, B=5, C=2 + A B X C / 10 2 5 2 12 A B C / + Value of expression
© Janice Regan, CMPT 128, Sept 2007 9 Importance of order of operations Order of operations is determined by operator precedence rules () before / (A + B) / C X / C Let A=10, B=5, C=2 2 A B C + / + A B X C + 10 15 5 2 7 Value of expression

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
© Janice Regan, CMPT 128, Sept 2007 10 Expressions: arithmetic operators Types of operands: float vs. integer divide An operator always operates on two operands of the same type A + B / C A + X Let A=23.7, B=55.4, C=1.2 + A B X C / 23.7 46.2 55.4 1.2 69.9 Value of expression A B C / +
© Janice Regan, CMPT 128, Sept 2007 11 Expressions: arithmetic operators Let A=27, B=5, C=2, D=3, E=8 ((A + B) / C) – D % E ( X / C) – D % E Y – D % E Y – Z + A B X C / 27 5 32 2 16 Y D 3 8 % E 3 - 13 A B C + % D E / _

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
© Janice Regan, CMPT 128, Sept 2007 12 Expressions: arithmetic operators
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 36

lecture5 - CMPT 128 Introduction to Computing Science for...

This preview shows document pages 1 - 13. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online