Operator C ++

Dalam tutorial ini, kita akan belajar tentang berbagai jenis operator di C ++ dengan bantuan contoh. Dalam pemrograman, operator adalah simbol yang beroperasi pada nilai atau variabel.

Operator adalah simbol yang melakukan operasi pada variabel dan nilai. Misalnya, +operator digunakan untuk penjumlahan, sedangkan -operator digunakan untuk pengurangan.

Operator di C ++ dapat diklasifikasikan menjadi 6 jenis:

  1. Operator Aritmatika
  2. Operator Penugasan
  3. Operator Relasional
  4. Operator Logis
  5. Operator Bitwise
  6. Operator Lain

1. Operator Aritmatika C ++

Operator aritmatika digunakan untuk melakukan operasi aritmatika pada variabel dan data. Sebagai contoh,

 a + b; 

Di sini, +operator digunakan untuk menambahkan dua variabel a dan b. Demikian pula ada berbagai operator aritmatika lainnya di C ++.

Operator Operasi
+ Tambahan
- Pengurangan
* Perkalian
/ Divisi
% Operasi Modulo (Sisa setelah pembagian)

Contoh 1: Operator Aritmatika

  #include using namespace std; int main() ( int a, b; a = 7; b = 2; // printing the sum of a and b cout << "a + b = " << (a + b) << endl; // printing the difference of a and b cout << "a - b = " << (a - b) << endl; // printing the product of a and b cout << "a * b = " << (a * b) << endl; // printing the division of a by b cout << "a / b = " << (a / b) << endl; // printing the modulo of a by b cout << "a % b = " << (a % b) << endl; return 0; ) 

Keluaran

 a + b = 9 a - b = 5 a * b = 14 a / b = 3 a% b = 1 

Di sini, operator +, -dan *menghitung penjumlahan, pengurangan, dan perkalian masing-masing seperti yang kita harapkan.

/ Operator Divisi

Perhatikan operasi (a / b)dalam program kami. The /operator adalah operator divisi.

Seperti yang bisa kita lihat dari contoh di atas, jika bilangan bulat dibagi dengan bilangan bulat lain, kita akan mendapatkan hasil bagi. Namun, jika pembagi atau pembilang adalah bilangan floating-point, kita akan mendapatkan hasilnya dalam desimal.

 Di C ++, 7/2 adalah 3 7.0 / 2 adalah 3.5 7 / 2.0 adalah 3.5 7.0 / 2.0 adalah 3.5 

% Operator Modulo

Operator modulo %menghitung sisanya. Saat a = 9dibagi dengan b = 4, sisanya adalah 1 .

Catatan: The %Operator hanya dapat digunakan dengan bilangan bulat.

Operator Increment dan Decrement

C ++ juga menyediakan operator increment dan decrement: ++dan --masing - masing. ++meningkatkan nilai operan sebesar 1 , sedangkan --menurunkannya sebesar 1 .

Sebagai contoh,

 int num = 5; // increasing num by 1 ++num; 

Di sini, nilai num ditingkatkan menjadi 6 dari nilai awalnya 5 .

Contoh 2: Operator Increment dan Decrement

 // Working of increment and decrement operators #include using namespace std; int main() ( int a = 10, b = 100, result_a, result_b; // incrementing a by 1 and storing the result in result_a result_a = ++a; cout << "result_a = " << result_a << endl; // decrementing b by 1 and storing the result in result_b result_b = --b; cout << "result_b = " << result_b << endl; return 0; ) 

Keluaran

 result_a = 11 result_b = 99 

Dalam program di atas, kami menggunakan ++dan --operator sebagai prefiks . Kami juga dapat menggunakan operator ini sebagai postfix .

Ada sedikit perbedaan saat operator ini digunakan sebagai prefiks versus saat digunakan sebagai postfix.

Untuk mempelajari lebih lanjut tentang operator ini, kunjungi operator increment dan decrement.

2. Operator Penugasan C ++

Di C ++, operator penugasan digunakan untuk menetapkan nilai ke variabel. Sebagai contoh,

 // assign 5 to a a = 5; 

Di sini, kami telah menetapkan nilai 5ke variabel a.

Operator Contoh Setara dengan
= a = b; a = b;
+= a += b; a = a + b;
-= a -= b; a = a - b;
*= a *= b; a = a * b;
/= a /= b; a = a / b;
%= a %= b; a = a % b;

Contoh 2: Operator Penugasan

 #include using namespace std; int main() ( int a, b, temp; // 2 is assigned to a a = 2; // 7 is assigned to b b = 7; // value of a is assigned to temp temp = a; // temp will be 2 cout << "temp = " << temp << endl; // assigning the sum of a and b to a a += b; // a = a +b cout << "a = " << a << endl; return 0; ) 

Keluaran

 suhu = 2 a = 9 

3. Operator Relasional C ++

A relational operator is used to check the relationship between two operands. For example,

 // checks if a is greater than b a> b; 

Here, > is a relational operator. It checks if a is greater than b or not.

If the relation is true, it returns 1 whereas if the relation is false, it returns 0.

Operator Meaning Example
== Is Equal To 3 == 5 gives us false
!= Not Equal To 3 != 5 gives us true
> Greater Than 3> 5 gives us false
< Less Than 3 < 5 gives us true
>= Greater Than or Equal To 3>= 5 give us false
<= Less Than or Equal To 3 <= 5 gives us true

Example 4: Relational Operators

 #include using namespace std; int main() ( int a, b; a = 3; b = 5; bool result; result = (a == b); // false cout << "3 == 5 is " << result << endl; result = (a != b); // true cout << "3 != 5 is " << result < b; // false cout < 5 is " << result << endl; result = a < b; // true cout << "3 < 5 is " << result <= b; // false cout <= 5 is " << result << endl; result = a <= b; // true cout << "3 <= 5 is " << result << endl; return 0; ) 

Output

 3 == 5 is 0 3 != 5 is 1 3> 5 is 0 3 = 5 is 0 3 <= 5 is 1 

Note: Relational operators are used in decision making and loops.

4. C++ Logical Operators

Logical operators are used to check whether an expression is true or false. If the expression is true, it returns 1 whereas if the expression is false, it returns 0.

Operator Example Meaning
&& expression1 && expression2 Logical AND.
True only if all the operands are true.
|| expression1 || expression2 Logical OR.
True if at least one of the operands is true.
! !expression Logical NOT.
True only if the operand is false.

In C++, logical operators are commonly used in decision making. To further understand the logical operators, let's see the following examples,

 Suppose, a = 5 b = 8 Then, (a> 3) && (b> 5) evaluates to true (a> 3) && (b 3) || (b> 5) evaluates to true (a> 3) || (b < 5) evaluates to true (a < 3) || (b 3) evaluates to false 

Example 5: Logical Operators

 #include using namespace std; int main() ( bool result; result = (3 != 5) && (3 < 5); // true cout << "(3 != 5) && (3 < 5) is " << result << endl; result = (3 == 5) && (3 < 5); // false cout << "(3 == 5) && (3 < 5) is " << result < 5); // false cout < 5) is " << result << endl; result = (3 != 5) || (3 < 5); // true cout << "(3 != 5) || (3 < 5) is " << result < 5); // true cout < 5) is " << result < 5); // false cout < 5) is " << result << endl; result = !(5 == 2); // true cout << "!(5 == 2) is " << result << endl; result = !(5 == 5); // false cout << "!(5 == 5) is " << result << endl; return 0; ) 

Output

 (3 != 5) && (3 < 5) is 1 (3 == 5) && (3 5) is 0 (3 != 5) || (3 5) is 1 (3 == 5) || (3 < 5) is 0 !(5 == 2) is 1 !(5 == 5) is 0 

Explanation of logical operator program

  • (3 != 5) && (3 < 5) evaluates to 1 because both operands (3 != 5) and (3 < 5) are 1 (true).
  • (3 == 5) && (3 < 5) evaluates to 0 because the operand (3 == 5) is 0 (false).
  • (3 == 5) && (3> 5) evaluates to 0 because both operands (3 == 5) and (3> 5) are 0 (false).
  • (3 != 5) || (3 < 5) evaluates to 1 because both operands (3 != 5) and (3 < 5) are 1 (true).
  • (3 != 5) || (3> 5) evaluates to 1 because the operand (3 != 5) is 1 (true).
  • (3 == 5) || (3> 5) evaluates to 0 because both operands (3 == 5) and (3> 5) are 0 (false).
  • !(5 == 2) evaluates to 1 because the operand (5 == 2) is 0 (false).
  • !(5 == 5)mengevaluasi ke 0 karena operannya (5 == 5)adalah 1 (true).

5. Operator C ++ Bitwise

Di C ++, operator bitwise digunakan untuk melakukan operasi pada bit individu. Mereka hanya dapat digunakan bersama chardan inttipe data.

Operator Deskripsi
& Biner DAN
| Biner ATAU
^ XOR biner
~ Pelengkap Biner Satu
<< Pergeseran Biner Kiri
>> Pergeseran Biner Kanan

Untuk mempelajari lebih lanjut, kunjungi operator bitwise C ++.

Terlepas dari operator dibahas di atas, ada beberapa operator lain, seperti sizeof, ?, ., &, dll, yang tidak dapat rapi diklasifikasikan ke dalam satu atau jenis lain. Kami akan mempelajari lebih lanjut tentang operator ini di tutorial selanjutnya.

Artikel yang menarik...