Operaterji C ++

V tej vadnici bomo s pomočjo primerov spoznali različne vrste operaterjev v jeziku C ++. Pri programiranju je operater simbol, ki deluje na vrednost ali spremenljivko.

Operatorji so simboli, ki izvajajo operacije nad spremenljivkami in vrednostmi. Na primer, +je operator, ki se uporablja za seštevanje, medtem ko -je operator, ki se uporablja za odštevanje.

Operaterje v jeziku C ++ lahko razvrstimo v 6 vrst:

  1. Aritmetični operaterji
  2. Operatorji dodelitve
  3. Relacijski operaterji
  4. Logični operaterji
  5. Bitni operaterji
  6. Drugi operaterji

1. Aritmetični operaterji C ++

Aritmetični operatorji se uporabljajo za izvajanje aritmetičnih operacij nad spremenljivkami in podatki. Na primer

 a + b; 

Tu +uporabimo operator za dodajanje dveh spremenljivk a in b. Podobno obstajajo v C ++ različni drugi aritmetični operatorji.

Operater Delovanje
+ Dodatek
- Odštevanje
* Množenje
/ Divizija
% Modulo Operation (Preostanek po delitvi)

Primer 1: Aritmetični operatorji

  #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; ) 

Izhod

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

Tukaj, operaterji +, -in *izračunati seštevanje, odštevanje, množenje in sicer, kot smo lahko pričakovali.

/ Operater oddelka

Upoštevajte delovanje (a / b)v našem programu. /Operater je operater delitev.

Kot lahko vidimo iz zgornjega primera, če celo število delimo z drugim celo število, bomo dobili količnik. Če pa je delitelj ali dividenda število s plavajočo vejico, bomo rezultat dobili v decimalnih številkah.

 V jeziku C ++ je 7/2 3 7,0 / 2 3,5 3,5 / 2,0 3,5 3,5 7,0 / 2,0 3,5 

% Modulo Operator

Operator modula %izračuna preostanek. Ko a = 9je deljeno z b = 4, je ostanek 1 .

Opomba:% operater se lahko uporablja samo s celimi števili.

Operatorji inkrementa in dekrementa

C ++ ponuja tudi operatorje prirastka in zmanjšanja: ++in --. ++poveča vrednost operanda za 1 , medtem ko jo --zmanjša za 1 .

Na primer

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

Tu se vrednost num poveča na 6 z začetne vrednosti 5 .

Primer 2: Operatorji inkrementa in dekrementa

 // 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; ) 

Izhod

 rezultat_a = 11 rezultat_b = 99 

V zgornjem programu smo kot predpono uporabili operator ++in --operator . Te operatorje lahko uporabimo tudi kot postfix .

Majhna razlika je, če se ti operaterji uporabljajo kot predpona v primerjavi s tem, ko se uporabljajo kot postfiksi.

Če želite izvedeti več o teh operaterjih, obiščite operatorje prirastka in zmanjšanja.

2. Operaterji dodelitve C ++

V C ++ se operaterji dodelitve uporabljajo za dodelitev vrednosti spremenljivkam. Na primer

 // assign 5 to a a = 5; 

Tu smo 5spremenljivki a dodelili vrednost od .

Operater Primer Enakovreden
= 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;

Primer 2: Operatorji dodelitve

 #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; ) 

Izhod

 temp = 2 a = 9 

3. Relacijski operaterji 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)ovrednoten kot 0 , ker je operand (5 == 5)je 1 (res).

5. Bitni operaterji C ++

V C ++ se bitni operatorji uporabljajo za izvajanje operacij nad posameznimi biti. Prav tako se lahko uporablja samo skupaj charin intpodatkovnih tipov.

Operater Opis
& Binarni IN
| Binarno ALI
^ Binarni XOR
~ Binarno dopolnilo
<< Binarni premik levo
>> Binarni premik desno

Če želite izvedeti več, obiščite bitne operatorje C ++.

Poleg operaterjev razpravljali zgoraj, obstaja nekaj drugih operaterjev, kot so sizeof, ?, ., &, itd, da ne more biti lepo uvrstiti v eno ali drugo vrsto. Več o teh operaterjih bomo izvedeli v poznejših vajah.

Zanimive Članki...