Session_3 - Lesson 5: Expressions, Statements, and...

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

View Full Document Right Arrow Icon
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
Background image of page 1

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

View Full DocumentRight Arrow Icon
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
Background image of page 2
Integer Division and Modulus Integer division is somewhat different from everyday division. When you divide 21 by 4 you are doing integer division and you get 5: #include <iostream> using namespace std; int main() { int i = 21,j = 4; cout << "i/j = " << i / j << endl;
Background image of page 3

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

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 12

Session_3 - Lesson 5: Expressions, Statements, and...

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

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