Operators and Expressions in C++
In C++, operators allow you to perform calculations, make decisions, and manipulate data. Expressions are combinations of values, variables, and operators that produce a new value. Let's explore the different types of operators and see them in action.
Arithmetic Operators
Arithmetic operators perform basic mathematical operations:
+
(Addition): Adds two values. Example:a + b
-
(Subtraction): Subtracts the second value from the first. Example:a - b
*
(Multiplication): Multiplies two values. Example:a * b
/
(Division): Divides the first value by the second. Example:a / b
%
(Modulus): Gives the remainder of a division. Example:a % b
(only for integers)
Real-Life Example: Calculating the total cost of items in a shopping cart.
Relational Operators
Relational operators compare values and return true
or false
:
==
(Equal to): Checks if two values are equal.!=
(Not equal to): Checks if two values are not equal.>
(Greater than): Checks if the first value is greater.<
(Less than): Checks if the first value is less.>=
(Greater than or equal to): Checks if the first value is greater or equal.<=
(Less than or equal to): Checks if the first value is less or equal.
Real-Life Example: Determining if a student has passed a test.
Here, >=
checks if the student’s score meets the passing score.
Logical Operators
Logical operators are used to combine multiple conditions:
&&
(Logical AND): Returnstrue
if both conditions are true.||
(Logical OR): Returnstrue
if at least one condition is true.!
(Logical NOT): Reverses the result of the condition.
Real-Life Example: Checking if a user is eligible for a senior discount.
&&
to check both age and membership status.Bitwise Operators
Bitwise operators perform operations on the binary representation of numbers:
&
(AND): Sets each bit to 1 if both bits are 1.|
(OR): Sets each bit to 1 if one of the bits is 1.^
(XOR): Sets each bit to 1 if only one of the bits is 1.~
(NOT): Inverts all the bits.<<
(Left shift): Shifts bits to the left.>>
(Right shift): Shifts bits to the right.
Real-Life Example: In encryption, bitwise XOR is often used to scramble data.
The bitwise XOR ^
encrypts and decrypts data when applied with the same key.
Assignment and Compound Assignment Operators
Assignment operators assign values to variables:
=
(Simple Assignment): Assigns the right-hand value to the variable.- Compound Assignment: Combines an operation with assignment.
+=
: Adds and assigns. Example:x += 5;
(equivalent tox = x + 5;
)-=
: Subtracts and assigns. Example:y -= 2;
*=
: Multiplies and assigns. Example:z *= 3;
/=
: Divides and assigns. Example:w /= 4;
%=
: Modulus and assigns. Example:a %= 5;
Real-Life Example: Adjusting a bank account balance after a deposit.
+=
operator is used to add the deposit amount to the balance.Increment and Decrement Operators
Increment and decrement operators increase or decrease a variable’s value by 1:
++
(Increment): Increases value by 1. Example:count++
--
(Decrement): Decreases value by 1. Example:count--
These can be used in two forms:
- Prefix:
++count
(increments before using the value). - Postfix:
count++
(increments after using the value).
Real-Life Example: Counting down items left in inventory.
itemsLeft--
decreases the item count after displaying the initial count.Operator Precedence and Associativity
Operators have a predefined precedence, which determines the order in which operations are performed. Associativity defines the direction in which operators of the same precedence level are evaluated (left-to-right or right-to-left).
Example: Consider the following expression:
Here, 3 * 2
is evaluated first, then added to 10
, giving result = 16
.
Real-Life Example: Consider calculating a final price after adding tax and applying a discount.
This program first applies tax, then a discount, demonstrating the order of operations.