1
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++】基礎を学ぶ⑨~switch文~

Last updated at Posted at 2022-07-09

switch

条件分岐を書くときに使う
3つ以上に条件分岐させたいときに使う

switchの使い方

switch (条件) {
  case 1 :
    処理1 // 条件が値1と一致したときの処理
    break;
  case 2 :
    処理2 // 条件が値2と一致したときの処理
    break;
  default:
    処理3 // 条件がどのcaseの値とも一致しないときの処理
    break;
}

実際のプログラム

#include <iostream>

using namespace std;

int main(){
	int num = 2;

	switch (num) {
		case 1:
			cout << "1つめ";
			break;
		case 2:
			cout << "2つめ";
			break;
		default:
			cout << "3つめ";
			break;
	}
	return 0;
}
2つめ

次の記事

【C++】基礎を学ぶ➉~for文~

1
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
1
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?