C++ Tutorial/Operators statements/prefix postfix increment decrement operators

Материал из C\C++ эксперт
Перейти к: навигация, поиск

prefix, postfix, increment, decrement operators

#include <iostream>
 
 int main()
 {
     int intValue = 39;      // initialize two integers
     int yourAge = 39;
     std::cout << intValue << "\n";
     std::cout << yourAge << "\n";
     intValue++;         // postfix increment
     ++yourAge;       // prefix increment
     std::cout << intValue << "\n";
     std::cout << yourAge << "\n";
     std::cout << intValue++ << "\n";
     std::cout << ++yourAge << "\n";
     std::cout << intValue << "\n";
     std::cout << yourAge << "\n";
     return 0;
 }
39
39
40
40
40
41
41
41

Preincrementing and postincrementing

#include <iostream>
using std::cout;
using std::endl;
int main()
{
   int c;
   c = 5; 
   cout << c << endl; 
   cout << c++ << endl;
   cout << c << endl; 
   cout << endl; 
   c = 5;
   cout << c << endl; 
   cout << ++c << endl;
   cout << c << endl; 
   return 0; 
}
5
5
6
5
6
6