#07:変数にデータを入れる
ここでは、プログラムで「変数」にデータを入れてみます。変数は、データを入れておく入れ物で、データを入れて、何度も使うことができます。
- 変数:データの入れ物
- 変数名:入れ物の名前
- 変数名でデータを操作できる
- C++では、変数ごとにデータ型を指定する
#include <iostream>
using namespace std;
int main() {
string greeting = "Hello world";
cout << greeting << endl;
}
#include <iostream>
using namespace std;
int main() {
string greeting = "Hello world";
cout << greeting << endl;
cout << greeting << endl;
}
#include <iostream>
using namespace std;
int main() {
string greeting = "Hello paiza";
cout << greeting << endl;
cout << greeting << endl;
}
・変数の値を変更する
#include <iostream>
using namespace std;
int main() {
string greeting = "Hello paiza";
cout << greeting << endl;
greeting = "Hello C++";
cout << greeting << endl;
}
・変数に整数を代入する
#include <iostream>
using namespace std;
int main() {
int number = 100;
cout << number << endl;
}
- 1 文字目 :アルファベット、アンダーバー
- 2 文字目以降 :アルファベット、アンダーバー、数字
慣習
- 日本語は使わない
- 先頭は小文字にする