In diesem Tutorial lernen wir anhand von Beispielen die verschiedenen Arten von Operatoren in C ++ kennen. Bei der Programmierung ist ein Operator ein Symbol, das einen Wert oder eine Variable bearbeitet.
Operatoren sind Symbole, die Operationen an Variablen und Werten ausführen. Beispielsweise +
wird ein Operator für die Addition verwendet, während -
ein Operator für die Subtraktion verwendet wird.
Operatoren in C ++ können in 6 Typen eingeteilt werden:
- Rechenzeichen
- Zuweisungsoperatoren
- Vergleichsoperatoren
- Logische Operatoren
- Bitweise Operatoren
- Andere Betreiber
1. C ++ - Arithmetikoperatoren
Arithmetische Operatoren werden verwendet, um arithmetische Operationen an Variablen und Daten auszuführen. Beispielsweise,
a + b;
Hier wird der +
Operator verwendet, um zwei Variablen a und b hinzuzufügen. Ebenso gibt es in C ++ verschiedene andere arithmetische Operatoren.
Operator | Operation |
---|---|
+ | Zusatz |
- | Subtraktion |
* | Multiplikation |
/ | Teilung |
% | Modulo-Betrieb (Rest nach Teilung) |
Beispiel 1: Arithmetische Operatoren
#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; )
Ausgabe
a + b = 9 a - b = 5 a * b = 14 a / b = 3 a% b = 1
Dabei sind die Betreiber +
, -
und *
Compute - Addition, Subtraktion, Multiplikation und jeweils als wir zu erwarten haben.
/ Abteilungsbetreiber
Beachten Sie die Operation (a / b)
in unserem Programm. Der /
Operator ist der Divisionsoperator.
Wie wir aus dem obigen Beispiel sehen können, erhalten wir den Quotienten, wenn eine Ganzzahl durch eine andere Ganzzahl geteilt wird. Wenn jedoch entweder Divisor oder Dividende eine Gleitkommazahl ist, erhalten wir das Ergebnis in Dezimalstellen.
In C ++ ist 7/2 3 7,0 / 2 ist 3,5 7 / 2,0 ist 3,5 7,0 / 2,0 ist 3,5
% Modulo Operator
Der Modulo-Operator %
berechnet den Rest. Wenn a = 9
geteilt durch b = 4
, ist der Rest 1 .
Hinweis: Der %
Operator kann nur mit ganzen Zahlen verwendet werden.
Inkrementierungs- und Dekrementierungsoperatoren
C ++ bietet auch Inkrement- und Dekrement - Operatoren: ++
und --
jeweils. ++
erhöht den Wert des Operanden um 1 und --
verringert ihn um 1 .
Beispielsweise,
int num = 5; // increasing num by 1 ++num;
Hier wird der Wert von num von seinem Anfangswert von 5 auf 6 erhöht .
Beispiel 2: Inkrementierungs- und Dekrementierungsoperatoren
// 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; )
Ausgabe
result_a = 11 result_b = 99
Im obigen Programm haben wir ++
und --
operator als Präfixe verwendet . Wir können diese Operatoren auch als Postfix verwenden .
Es gibt einen kleinen Unterschied, wenn diese Operatoren als Präfix verwendet werden, und wenn sie als Postfix verwendet werden.
Weitere Informationen zu diesen Operatoren finden Sie unter Inkrementierungs- und Dekrementierungsoperatoren.
2. C ++ - Zuweisungsoperatoren
In C ++ werden Zuweisungsoperatoren verwendet, um Variablen Werte zuzuweisen. Beispielsweise,
// assign 5 to a a = 5;
Hier haben wir 5
der Variablen a den Wert a zugewiesen .
Operator | Beispiel | Gleichwertig |
---|---|---|
= | 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; |
Beispiel 2: Zuweisungsoperatoren
#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; )
Ausgabe
temp = 2 a = 9
3. C ++ - Vergleichsoperatoren
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)
Ergibt 0 , weil der Operand(5 == 5)
ist 1 (TRUE).
5. Bitweise C ++ - Operatoren
In C ++ werden bitweise Operatoren verwendet, um Operationen an einzelnen Bits auszuführen. Sie können nur neben char
und int
Datentypen verwendet werden.
Operator | Beschreibung |
---|---|
& | Binär UND |
| | Binär ODER |
^ | Binäres XOR |
~ | Komplement von Binary One |
<< | Binäre Verschiebung nach links |
>> | Binäre Verschiebung nach rechts |
Weitere Informationen finden Sie unter bitweise C ++ - Operatoren.
Neben den Betreibern oben diskutiert, gibt es ein paar anderen Operatoren wie sizeof
, ?
, .
, &
usw., die nicht ordentlich in der einen oder anderen Art klassifiziert werden. Wir werden in späteren Tutorials mehr über diese Operatoren erfahren.