Operátory C ++

V tomto tutoriáli sa pomocou príkladov dozvieme o rôznych druhoch operátorov v C ++. V programovaní je operátor symbol, ktorý pracuje s hodnotou alebo premennou.

Operátory sú symboly, ktoré vykonávajú operácie s premennými a hodnotami. Napríklad +je operátor používaný na sčítanie, zatiaľ čo -je operátor používaný na odčítanie.

Operátory v C ++ možno rozdeliť do 6 typov:

  1. Aritmetické operátory
  2. Prevádzkovatelia pridelenia
  3. Relační operátori
  4. Logickí operátori
  5. Bitoví operátori
  6. Ostatní prevádzkovatelia

1. Aritmetické operátory C ++

Aritmetické operátory sa používajú na vykonávanie aritmetických operácií s premennými a údajmi. Napríklad,

 a + b; 

Tu sa +operátor používa na pridanie dvoch premenných a a b. Podobne existujú rôzne ďalšie aritmetické operátory v C ++.

Prevádzkovateľ Prevádzka
+ Dodatok
- Odčítanie
* Násobenie
/ Divízia
% Modulová prevádzka (zvyšok po rozdelení)

Príklad 1: Aritmetické operátory

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

Výkon

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

Tu sú operátori +, -a *výpočtovej sčítanie, odčítanie, násobenie a v tomto poradí, ako sme mohli očakávať.

Operátor divízie

Všimnite si operáciu (a / b)v našom programe. /Operátor je operátor delenia.

Ako vidíme z vyššie uvedeného príkladu, ak je celé číslo vydelené iným celým číslom, dostaneme podiel. Ak je však deliteľ alebo dividenda číslo s pohyblivou rádovou čiarkou, dostaneme výsledok v desatinných číslach.

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

% Modulo operátor

Zvyšok %počíta operátor modulo . Keď a = 9sa vydelí b = 4, zvyšok je 1 .

Poznámka:% Operátor môže byť použitý iba s celými číslami.

Operátory prírastku a zníženia

C ++ tiež prírastok a dekrement operátormi: ++a --resp. ++zvyšuje hodnotu operandu o 1 , zatiaľ čo --znižuje o 1 .

Napríklad,

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

Tu sa hodnota num zvýši na 6 z pôvodnej hodnoty 5 .

Príklad 2: Operátory prírastku a zníženia

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

Výkon

 result_a = 11 result_b = 99 

Vo vyššie uvedenom programe sme použili ++aj --operátora predponami . Tieto operátory môžeme použiť aj ako postfix .

Existuje malý rozdiel, keď sa tieto operátory používajú ako predpona, a keď sa používajú ako predpona.

Ak sa chcete dozvedieť viac informácií o týchto operátoroch, navštívte operátory zvyšovania a znižovania.

2. Prevádzkovatelia priradenia v C ++

V C ++ sa na priradenie hodnôt k premenným používajú operátory priradenia. Napríklad,

 // assign 5 to a a = 5; 

Tu sme priradili hodnotu 5premennej a.

Prevádzkovateľ Príklad Rovná sa
= 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;

Príklad 2: Prevádzkovatelia priradenia

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

Výkon

 teplota = 2 a = 9 

3. C ++ relačné operátory

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)vyhodnotí na 0, pretože operand (5 == 5)je 1 (true).

5. Bitové operátory C ++

V C ++ sa bitové operátory používajú na vykonávanie operácií s jednotlivými bitmi. Môžu byť použité len po boku chara intdátové typy.

Prevádzkovateľ Popis
& Binárne AND
| Binárne ALEBO
^ Binárne XOR
~ Binárny doplnok
<< Binárny posun vľavo
>> Binárny posun doprava

Ak sa chcete dozvedieť viac, navštívte bitové operátory C ++.

Okrem prevádzkovateľov diskutovaných vyššie, existuje niekoľko ďalších operátorov, ako sú sizeof, ?, ., &, atď., Ktoré nemôžu byť úhľadne zaradená do jednej alebo iného typu. Viac sa o týchto operátoroch dozvieme v ďalších tutoriáloch.

Zaujímavé články...