Session_3

Session_3 - Lesson 5 Expressions Statements and Operators...

• Notes
• 12

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

Lesson 5: Expressions, Statements, and Operators All C++ statements end with a semi-colon. Remember that the semi-colon is itself a statement (the null statement). Here’s an example, x = a + b; The above is not like Algebra. It means, assign the sum of a and b to x. Also, x = a + b ; works, because whitespace is ignored. Whitespace is tabs, spaces and newlines and is generally ignored instatements. Still, best to use readable syntax. Blocks and Compound Statements Any place you put a single statement, you can put a compound statement, also called a block. A block begins with an opening brace ( { ) and ends with a closing brace ( } ). Although every statement in the block must end with a semicolon, the block itself does not end with a semicolon as shown the following example: { temp = a; a = b; b = temp; } Expressions Anything that evaluates to a value is an expression in C++. Here are some examples: 3.2 // returns the value 3.2 PI // float const that returns the value 3.14 SecondsPerMinute // int const that returns 60 The above assumes that PI is a constant equal to 3.14 and SecondsPerMinute is a constant equal to 60. The expression x = a + b; not only adds a and b and assigns the result to x, but returns the value of that assignment (the value of x) as well. That is, the statement is also an expression. Because it is an expression, it can be on the right side of an assignment operator: y = x = a + b; The line is evaluated in order: Add a to b Assigns the result of a+ b to x Assigns x to y Page 1 of 12

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

Operators Operators are assignment operators and mathematical operators. The assignment operator is the single equal sign (=). That is x = a + b; An operand that can legally be put on the left side of an assignment operator is called an l-value. Constants are not l-values . #include <iostream> using namespace std; int main() { const int Not_lvalue = 99; Not_lvalue = 24; // cannot change the value of a constant! return 0; } The above will not compile. Nor the following: #include <iostream> using namespace std; int main() { int x,y,z; z = 99; x + y = z; // cannot assign a value to an expression! return 0; } Mathematical Operators Five mathematical operators are addition (+), subtraction (-), multiplication (*), division (/) and modulus (%). Example from textbook shows overflow (actually underflow) with subtraction: #include <iostream> using namespace std; int main() { unsigned int difference; unsigned int bigNumber = 100; unsigned int smallNumber = 50; difference = bigNumber - smallNumber; cout << "Difference is: " << difference; difference = smallNumber - bigNumber; cout << "\nNow difference is: " << difference << endl; return 0; } Page 2 of 12
Integer Division and Modulus Integer division is somewhat different from everyday division. When you divide 21 by 4

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

This is the end of the preview. Sign up to access the rest of the document.
• Fall '06
• Mike
• Equals sign, relational operator, Ternary Operator, METS

{[ 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