Home » C++ » Operator C++ : Operator Penugasan (Assignment Operators)

Operator C++ : Operator Penugasan (Assignment Operators)

by Hanifah Nurbaeti
by Hanifah Nurbaeti

Operator Penugasan(Assignment Operator)

Operator penugasan(Assignment Operator) digunakan untuk menetapkan nilai ke variabel.

Dalam contoh di bawah ini, kita akan menggunakan operator penugasan(assignment operator) (=) untuk menetapkan nilai 10 ke variabel yang disebut x :

Contoh :

#include <iostream>
using namespace std;

int main() {
  int x = 10;
  cout << x;
  return 0;
}

Operator penugasan(assignment operator) tambahan (+ =) menambahkan nilai ke variabel:

Contoh :

#include <iostream>
using namespace std;

int main() {
  int x = 10;
  x += 5;
  cout << x;
  return 0;
}

Output :

Daftar operator penugasan(Assignment Operator):

  • Operator : =
  • Contoh : x = 5
  • Sama Seperti : x = 5
#include <iostream>
using namespace std;

int main() {
  int x = 5;
  cout << x;
  return 0;
}

Output :

  • Operator : +=
  • Contoh : x+=3
  • Sama Seperti : x = x + 3
#include <iostream>
using namespace std;

int main() {
  int x = 5;
  x += 3;
  cout << x;
  return 0;
}

Output :

  • Operator : -+
  • Contoh : x-=3
  • Sama Seperti : x = x – 3
#include <iostream>
using namespace std;

int main() {
  int x = 5;
  x -= 3;
  cout << x;
  return 0;
}

Output :

  • Operator : *=
  • Contoh : x *= 3
  • Sama Seperti : x = x * 3
#include <iostream>
using namespace std;

int main() {
  int x = 5;
  x *= 3;
  cout << x;
  return 0;
}

Output :

  • Operator : /=
  • Contoh : x /= 3
  • Sama Seperti : x = x / 3
#include <iostream>
using namespace std;

int main() {
  double x = 5;
  x /= 3;
  cout << x;
  return 0;
}

Output :

  • Operator : %=
  • Contoh : x %= 3
  • Sama Seperti : x = x % 3
#include <iostream>
using namespace std;

int main() {
  int x = 5;
  x %= 3;
  cout << x;
  return 0;
}

Output :

  • Operator : &=
  • Contoh : x&= 3
  • Sama Seperti : x = x & 3
#include <iostream>
using namespace std;

int main() {
  int x = 5;
  x &= 3;
  cout << x;
  return 0;
}

Output :

  • Operator : |=
  • Contoh : x | = 3
  • Sama Seperti : x = x | 3
#include <iostream>
using namespace std;

int main() {
  int x = 5;
  x |= 3;
  cout << x;
  return 0;
}

Output :

  • Operator : ^=
  • Contoh : x ^= 3
  • Sama Seperti : x = x ^ 3
#include <iostream>
using namespace std;

int main() {
  int x = 5;
  x ^= 3;
  cout << x;
  return 0;
}

Output :

  • Operator : >>=
  • Contoh : x >>= 3
  • Sama Seperti : x = x >> 3
#include <iostream>
using namespace std;

int main() {
  int x = 5;
  x >>= 3;
  cout << x;
  return 0;
}

Output :

  • Operator : <<=
  • Contoh : x <<= 3
  • Sama Seperti : x = x << 3
#include <iostream>
using namespace std;

int main() {
  int x = 5;
  x <<= 3;
  cout << x;
  return 0;
}

Output :

You may also like