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++】基礎を学ぶ⑤~複合代入演算子、インクリメント~

Last updated at Posted at 2022-07-09

複合代入演算子とは

プログラムを短く書くことができる演算子のこと
複合代入演算子を使うことによってx = x + 1x += 1のように書くことができる

複合代入演算子+=を使ったプログラム

#include <iostream>

using namespace std;

int main(){
  int x = 1;
  x += 1;
  cout << x << endl;
  return 0;
}
2

+=以外の複合代入演算子

他の演算子についても同様に書くことができる

#include <iostream>

using namespace std;

int main(){
  int a = 5;
  a -= 2;
  cout << a << endl;
 
  int b = 3;
  b *= 1 + 2;
  cout << b << endl;
 
  int c = 4;
  c /= 2;
  cout << c << endl;
 
  int d = 5;
  d %= 2;
  cout << d << endl;
  return 0;
}
3
9
2
1

インクリメント・デクリメント

インクリメントは1増加させる操作
x++または++xと書く
デクリメントは1減少させる操作
x--または--xと書く

#include <iostream>

using namespace std;

int main(){
  int x = 5;
  x++;
  cout << x << endl;
 
  int y = 5;
  y--;
  cout << y << endl;
  return 0;
}
6
4

糖衣構文(シンタックス・シュガー)

プログラミング言語において、読み書きしやすくするために導入される書き方のこと
「複合代入演算子」、「インクリメント」もそのひとつである

次の記事

【C++】基礎を学ぶ⑥~const定数~

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?