++C, C++ For and Do While
++C, C++ For and Do While
Preincrement
– Variable changed before used in expression
• Operator before variable (++c or --c)
Postincrement
– Incremented changed after expression
• Operator after variable (c++, c--)
Increment and Decrement Operators
If c = 5, then
– cout << ++c;
• c is changed to 6, then printed out
– cout << c++;
• Prints out 5 (cout is executed before the increment.
• c then becomes 6
Increment and Decrement Operators
5
5
6
5
6
6
Essentials of Counter-Controlled Repetition
Sum is 2550
Examples Using the for Structure
true
condition
false
1 // Fig. 2.24: fig02_24.cpp
2 // Using the do/while repetition structure.
3 #include <iostream>
4
5 using std::cout;
6 using std::endl;
7
8 // function main begins program execution
9 int main()
10 {
11 int counter = 1; // initialize counter
12
13 do {
14 cout << counter << " "; // display counter
15 } while ( ++counter <= 10 ); // end do/while
16
17 cout << endl;
18
19 return 0; // indicate successful termination
20
21 } // end function main
1 2 3 4 5 6 7 8 9 10
Logical Operators
|| (logical OR)
– true if either of condition is true
if ( semesterAverage >= 90 || finalExam >= 90 )
cout << "Student grade is A" << endl;
Logical Operators