0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

【自主学習】ひたすらコードで覚えるC++[#2]

0
Last updated at Posted at 2024-10-26

はじめに

今回もC++の理解を深めるためにいろんなコードを見ていきます。

使用する環境

環境はVisual Studio 2022を使用します。

if文をつかう

数字の10が入力されると「10が入力されました。」となり、それ以外の入力の場合、if文内の処理は実行されません。

#include <iostream>
using namespace std;

int main()
{
	int a;

	cout << "数字の10を入力してください。\n";

	cin >> a;

	if (a == 10) {
		cout << "10が入力されました。\n";
	}

	cout << "処理を終了します。\n";

	return 0;
}

複数の分岐をつくる

if-else if-else文をつかって条件による分岐を行います。
100か200を入力することで場合に応じた処理となります。また、それ以外の入力の場合は100か200を入れるように表示されるプログラムになっています。

#include <iostream>
using namespace std;

int main()
{
	int a;

	cout << "100or200を入力してください\n";

	cin >> a;

	if (a == 100) {
		cout << "100が入力されました。\n";
	}
	else if (a == 200) {
		cout << "200が入力されました。\n";
	}
	else {
		cout << "100か200を入力してください\n";
	}
	return 0;
}

switch文を使った場合

switch文でも先ほどのif-else if-elseと同じ処理ができます。
switch文を使用する場合は変数aを整数型にする必要があります。また基本的にはbreak文をセットで使う必要があります。

#include <iostream>
using namespace std;

int main()
{
	int a;

	cout << "100or200を入力してください\n";

	cin >> a;

	switch (a) {
	case 100:
		cout << "100が入力されました。\n";
		break;
	case 200:
		cout << "200が入力されました。\n";
		break;
	default:
		cout << "100か200を入力してください\n";
		break;
	}

	return 0;
}

for文をつかう

このコードでは繰り返しの処理を行っています。文章を何度も表示させることができます。(Hello Worldを5回表示させる)

#include <iostream>
using namespace std;

int main()
{
	for (int i = 1; i <= 5; i++) {
		cout << "Hello World!\n";
	}

	cout << "終了です。\n";

	return 0;
}

入力した値をつかう

入力した値をつかってfor文による繰り返し処理を行うこともできます。このプログラムでは入力した数値までを足し合わせて表示します。(※int型なので入力値は整数のみ)

#include <iostream>
using namespace std;

int main()
{
	int num;
	int sum = 0;
	
	cout << "いくつまでの合計を求めますか?\n";

	cin >> num;


	for (int i = 1; i <= num; i++) {
		sum += i;
	}

	cout << "1から" << num << "までの合計値は" << sum << "です。\n";

	return 0;
}

do-while文をつかう

do-while文はwhile文と違い、条件を判断する前に、最低一回はブロック内の処理を行います。

#include <iostream>
using namespace std;

int main()
{
	int a = 1;

	do {
		cout << a << "回繰り返しています。\n";
		a++;
	} while (a <= 5);

	cout << "繰り返しが終了しました。\n";

	return 0;
}

for文を使ってネストする

for文をネストすると、多重の繰り返し処理が行われます。

#include <iostream>
using namespace std;

int main()
{
	for (int i = 0; i < 5; i++) {
		for (int j = 0; j < 5; j++) {
			cout << "iは" << i << "jは" << j << "\n";
		}
	}

	return 0;
}

処理を飛ばすcontinue文

このプログラムでは番号を入力するとその番号(ここではaの値)の処理を飛ばすことができます。

#include <iostream>
using namespace std;

int main(){

	int a;

	cout << "処理を飛ばす番号を教えてください";

	cin >> a;

	for (int i = 1; i <= 10; i++) {
		if (i == a)
			continue;
		cout << i << "番目の処理です。\n";
	}

	return 0;
}

参考文献

この記事は以下の情報を参考にして執筆しました。
やさしいC++ 第4版 (「やさしい」シリーズ)

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?