- Increases the value of a variable by one
Variable++;
|
num++; is equivalent to num=num+1; (if num=20, then num++; will result in num=21)
- Both statements behave in the same way when they are used to increment a variable in a separate statement
- If we use them in the middle of a larger expression (e.g., alpha=num++ * 3) they can yield different results
- Decreases the value of a variable by one
Variable--;
|
num--; is equivalent to num=num-1; (if num=20, then num--; will result in num=19)
- Both statements behave in the same way when they are used to increment a variable in a separate statement
- If we use them in the middle of a larger expression (e.g., alpha=num-- * 3f1 they can yield different results
- It instructs the computer to print a message or the value of an expression on the screen
|
- As the syntax template shows, the insertion operator (<<) can appear several times in a single cout1
- Example
cout << "The sum is " << sum;
or equivalently
cout << "The sum is ";
cout << sum;
- Strings must always be enclosed in double quotes (" ")
- More examples
<Table from page 69)>
- To instruct the computer to print something on a separate line, we must use the dentifier endl (i.e., end of line)
- Examples
cout << "Hi";
cout << "there";
will print:
Hithere
cout << "Hi" << endl;
cout << "there";
will print:
Hi
there
Debugging your program
<Figure 2-6, page 80)
Recommended exercises
- All Quick Check excersices (pages 85,86)
- Excersices 3 (page 88), 5, 6, 10 (page 89), 11 (page 90)