Arithmetic Operators

# Arithmetic Operators - Arithmetic Operations and Flow of...

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

Copyright Dr. David A. Gaitros, FSU, 2011 Arithmetic Operations and Flow of Control

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

View Full Document
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
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

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

View Full Document
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
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

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

View Full Document
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
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

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

View Full Document
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
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

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

View Full Document
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
Operator "=" vs. operator "==“ Remember that C/C++ will try to

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

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

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern