ArithmeticOperators

ArithmeticOperators - PART 2 Arithmetic Operations and Flow...

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

View Full Document Right Arrow Icon
Copyright Dr. David A. Gaitros, FSU, 2011 PART 2 Arithmetic Operations and Flow of Control
Background image of page 1

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

View Full DocumentRight Arrow Icon
ARITHMETIC OPERATORS C++ Operator Sybmol Expresseion C++ Expression Addition + f+7 g=f+7; Subtraction - f-7 g=f-7; Multiplication * bk g=b*k; Division / x y g=x/y; Modulus % r mod s g=r%s; 2-2 Integer Division Occurs when two operands are integer Any fraction part is discarded Value is truncated Example int i=7; int j=3; int k; k = i/j; //result is k will contain 2
Background image of page 2
ARITHMETIC OPERATORS Modulus Only integers can be used Returns the remainder of integer division Example: X = 6 % 3; // results is zero (0) X = 7 % 3; // results is 1 Copyright Dr. David A. Gaitros, FSU, 2011 2-3
Background image of page 3

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

View Full DocumentRight Arrow Icon
BOOLEAN EXPRESSIONS: Logical Operators Logical AND (&&) Logical OR (||) < Less than > Greater than >= Greater than or equal to <= Less than or equal to == Equal to != Not equal to 2-4
Background image of page 4
EVALUATING BOOLEAN EXPRESSIONS: 2-5 1 st op 2 nd op result True True True True False False False True False False False False 1 st op 2 nd op result True True True True False True False True True False False False Logical And Logical Or 1 st op 2 nd op result True True False True False True False True True False False False Logical Xor
Background image of page 5

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

View Full DocumentRight Arrow Icon
COMBING LOGIC AND ARITHMETIC EXPRESSIONS Arithmetic before logical x + 1 > 2 || x + 1 < -3 means: (x + 1) > 2 || (x + 1) < -3 Short-circuit evaluation (x >= 0) && (y > 1) Be careful with increment operators! (x > 1) && (y++) C and C++ will try to evaluate any expression as a true or false. Any integer can be expressed as true or false. All non-zero values true Zero value false 2-6
Background image of page 6
BRANCHING MECHANISMS if-else statements Choice of two alternate statements based on condition expression Example: if (hrs > 40) grossPay = rate*40 + 1.5*rate*(hrs- 40); else grossPay = rate*hrs; 2-7
Background image of page 7

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

View Full DocumentRight Arrow Icon
IF-ELSE STATEMENT SYNTAX syntax: if (<boolean_expression>) <single_statement> else <single_statement> Note each alternative is only ONE statement! To have multiple statements execute in either branch use compound statements grouped together with brackets { } 2-8
Background image of page 8
COMPOUND/BLOCK STATEMENT Only "get" one statement per branch Must use compound statement { } for multiples Also called a "block" stmt Each block should have block statement Even if just one statement Enhances readability 2-9
Background image of page 9

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

View Full DocumentRight Arrow Icon
COMPOUND STATEMENT IN ACTION Note indenting in this example: if (myScore > yourScore) { cout << "I win!\n"; wager = wager + 100; } else { cout << "I wish these were golf scores.\n"; wager = 0; } 2-10
Background image of page 10
Image of page 11
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 04/02/2012 for the course COP 3014 taught by Professor Gaitros during the Fall '11 term at Florida State College.

Page1 / 36

ArithmeticOperators - PART 2 Arithmetic Operations and Flow...

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

View Full Document Right Arrow Icon
Ask a homework question - tutors are online