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

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

View Full Document Right Arrow Icon
1 © Janice Regan, CMPT 128, Sept 2007 CMPT 128: Introduction to Computing Science for Engineering Students Arithmetic Expressions and Arithmetic Operators
Background image of page 1

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

View Full DocumentRight Arrow Icon
© 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;
Background image of page 2
© 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
Background image of page 3

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

View Full DocumentRight Arrow Icon
© 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
Background image of page 4
© 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
Background image of page 5

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

View Full DocumentRight Arrow Icon
© 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
Background image of page 6
© 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
Background image of page 7

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

View Full DocumentRight Arrow Icon
© 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
Background image of page 8
© 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
Background image of page 9

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

View Full DocumentRight Arrow Icon
© 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 / +
Background image of page 10
© 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 / _
Background image of page 11

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

View Full DocumentRight Arrow Icon
© Janice Regan, CMPT 128, Sept 2007 12 Expressions: arithmetic operators
Background image of page 12
Image of page 13
This is the end of the preview. Sign up to access the rest of the document.

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 Right Arrow Icon
Ask a homework question - tutors are online