KAIHATSUGIKEN GROUP

C++ PROGRAMMING LANGUAGE




********************************************************************************
5 インクリメントとデクリメント
********
変数の値に1を加えることをインクリメントという
count = count + 1;
この場合は変数countの値を得てそれに1を加えます。
その結果を変数countに記憶します。
********
逆に変数の値に1を減らす事をデクリメントという
count = count + 1;
この場合は変数count の値を得てそれに1を減じます。
その結果を変数count に記憶します。
***********************************
#include <iostream.h>
void main(void)
{
	int count = 1000;			//int型の変数coutに1000を代入

	cout << "count's starting value is " << count << endl;
							//この段階ではまだ1000
	cout << "count + 1;				//ここで1加算される
	cout << "count's ending value is " << count << endl; 
						//変数cout の値は1001に変わる
}
*********
c:\inccount <enter>
count's starting value is 1000
count's ending value is 1001
*********
count = count + 1;
count++;
は どちらも同じ変数count の値を1ずつインクリメント(増加)します。
********************************************************************************
接頭インクリメントと接尾インクリメント
********
current_count = ++count; 				//接頭インクリメント
変数countの値を変数current_countへ代入するようにしていてる。
さらに接尾インクリメント演算子はcountの値を増やすことを指定しています。
この場合次の文と同じ事になります。

current_cout = count;
count = count + 1;

********
current_count = count++;  				//接尾インクリメント
この場合最初にcountの値をインクリメントしてから、その結果を変数current_countに
代入するように指定しています。
この場合次の文と同じ事になります。

count = count + 1;
current_count_count = count;

********************************************************************************
デクリメント
********
インクリメント(++)は因数の値を1ずつ加算する演算子です。
デクリメント(--)は逆に因数の値を1ずつ減算する演算子です。
********
#include <iostram.h>
void main(void)
{
	int small_count = 0;		//int型整数small_countに0を代入
	int big_count = 1000;		//int型整数big_countに1000を代入
	
	cout << "small_count is " << small_count << endl;
					//small_countに代入された0を表示
	cout << "small_count++ yields " << small_count++ << endl;
					//small_countの値に接尾インクリメントする
	cout << "small_count's ending value is " << small_count << endl;
					//インクリメントされた値を表示
	cout << "big_coutnt is " << big_count << endl;
					//big_countに代入された1000を表示
	cout << "--big_count yields " << --big_count << endl;
					//接頭デクリメントされた値を表示
	cout << "big_count's ending value is " << big_count << endl;
					//接頭デクリメントされた値を表示
}
*********
small_count is 0
small_count++ yields 0
small_count's ending value is 1
big_count is 1000
big count yields 999
big count 's ending value is 999
*********