Home | API | MFC | C++ | C | Previous | Next

Programming With C++

Expressions and Operators

All C++ programs are made up of statements, which are commands that are executed one after another. All statements in C++ end with a semicolon. Each statement takes up one line by convention, but this is not a requirement. Multiple statements can be put on a line as long as each ends with a semicolon. 

To spread a statement over two or more lines, you can do it by adding a backslash ( \ ) to the end of the line -

cout << "Hello \ World" << endl; // split to two lines is OK

Whitespace

Whitespace is a term that refers to characters that are used for formatting purposes. In C++, this refers primarily to spaces, tabs, and (sometimes) newlines. The C++ compiler generally ignores whitespace, with a few minor exceptions. Whitespace serves the purpose of making the code more readable to programmers.

Compound Statements

A compound statement consists of one or more statements enclosed within a bracket.{}

Although every statement in a compound statement must end with a semicolon, the compound statement itself does not end with a semicolon. Variables declared within the statement are local to that compound statement.

Operators

An operator is a symbol that tells the compiler to perform a specific mathematical or logical operation

Arithmetic Operators

assuming a=2 and b=1

Operator Description Example
+ Adds two operands A + B will give 3
- Subtracts the second operand from the first A - B will give 1
* Multiplies both operands A * B will give 2
/ Divides numerator by de-numerator A / B will give 2
% Modulus Operator and the remainder of after an integer division A % B will give 0
++ increases operator value by one A++ will give 3
-- decreases operator value by one A-- will give 1

Postfix or Prefix?

In C++, use of the increment ++ operator increases the value of a variable by 1, and the use of the decrement -- operator decreases the value of a variable by 1 however the position of the operator and where it is placed has an important effect on the way the operation is evaluated

Placing the operator before the operand ie ++var causes the variable value to be increased then it returns a value

Placing the operator after the operand ie var++ causes the variable value to be returned then the variable value is increased.

Relational Operators

assuming a=2 and b=1

Operator Description Example
== returns true (1) if two operands have the same value otherwise returns false (0) (A == B) is not true.
!= returns true (1) if two operands don't have the same value otherwise returns false (0) (A != B) is true.
> returns true (1) if the left operand is greater than the value of the right operand otherwise returns false (0) (A > B) is true.
< returns true (1) if the left operand is less than the value of the right operand otherwise returns false (0) (A < B) is false.
>= returns true (1) if the left operand is greater than or equal to the value of the right operand otherwise returns false (0) (A >= B) is true.
<= returns true (1) if the left operand is less than or equal to the value of the right operand otherwise returns false (0) (A <= B) is false.

Logical Operators

Assume variable A holds 2 and variable B holds 1, then −

Operator Description Example
&& the logical AND operator evaluates two expressions.If both expressions are true, the logical AND expression is true. A==2 && B==1 result is TRUE
|| The logical OR operator evaluates two expressions. If either is true, the expression is true A==2 || B==1 result is TRUE
! A logical NOT statement reverses a normal expression, returning true if the expression is false and false if the expression is true. !(A==2) result is FALSE

Bitwise Operators

Bitwise operator works on bits and performs bit-by-bit operations. the difference between logical and bitwise operators is that bitwise operators don’t return a boolean result but manipulate individual bits.

The Bitwise operators supported by C++ language are listed in the following table. Assume variable A holds 50 and variable B holds 25, then −

A = 0011 0010

B = 000 11001

Operator Description Example
& Binary AND Operator copies a bit to the result if it exists in both operands. (A & B) will give 16 which is 0001 0000
| Binary OR Operator copies a bit if it exists in either operand. (A | B) will give 59 which is 0011 1011
^ Binary XOR Operator copies the bit if it is set in one operand but not both. ">(A ^ B) will give 43 which is 00010 1011
~ is a unary operator and has the effect of 'flipping' the bits. (~A ) will give -51 which is 1111111111001101 in 2's complement form.
<< Binary Left Shift Operator. The left operand value is moved left by the number of bits specified by the right operand. A << 1 will give 100 which is 0110 0100
>> Binary Right Shift Operator. The left operand value is moved right by the number of bits specified by the right operand. A >> 2 will give 12 which is 0000 1100

Assignment Operators

Operator Description Example
= Assigns values from right side operands to left side operands. C = A + B will assign value of A + B into C
+= add the value of the expression to the value of the variable to the left of the operator, and assign the result to that variable C += A is equivalent to C = C + A
-= subtract the value of the expression from the value of the variable to the left of the operator, and assign the result to that variable C -= A is equivalent to C = C - A
*= Multiply the right operand with the left operand and assign the result to the left operand. C *= A is equivalent to C = C * A
/= Divide the left operand with the right operand and assign the result to the left operand. C /= A is equivalent to C = C / A
%= Take modulus using two operands and assign the result to the left operand. C %= A is equivalent to C = C % A
<<= Left shift AND assignment operator. C <<= 2 is same as C = C << 2
>>= Right shift AND assignment operator. C >>= 2 is same as C = C >> 2
&= Bitwise AND assignment operator. C &= 2 is same as C = C & 2
^= Bitwise exclusive OR and assignment operator. C ^= 2 is same as C = C ^ 2
|= Bitwise inclusive OR and assignment operator. C |= 2 is same as C = C | 2

Misc Operators

?----known as a ternary operator because it requires three operands. Can be used to replace if-else statements

example:y = (x > 10) ? 10 : 20;

Here, y is assigned the value of 10 if x is greater than 10 and 20 if not

. ->(dot and arrow)---- used to reference individual members of classes, structures, and unions.

&(ampersand) ---- returns the address of a variable. For example &x; returns the location of the variable x in memory

*(asterisk)---- Used to Declare and Dereference a pointer

,(Comma operator)---causes a sequence of operations to be performed. The value of the entire comma expression is the value of the last expression of the comma-separated list. The value of a comma-separated list of expressions is the value of the right-most expression This means that the expression on the right side will become the value of the entire comma-separated expression

example: var = (value1 = 1, value2 = 2, Value1+1);

This statement first assigns the 1 to value1, assigns the 2 to value 2, increments value1 by 1 and finally, assigns the value of value1 to var. The parentheses are necessary because the comma operator has lower precedence than the assignment operator.

Operator Precedence in C++

The order in which operators are evaluated in a compound expression is called operator precedence.  In C++, when the compiler encounters an expression, it must similarly analyse the expression and determine how it should be evaluated. To assist with this, all operators are assigned a level of precedence. Those with the highest precedence are evaluated first.

In the table below, operators with the highest precedence appear at the top of the table, and those with the lowest appear at the bottom. Within an expression, higher precedence operators will be evaluated first.

for example

int myNumber = 20 * 50 + 25 – 25 * 25 << 2;

simplifies to 1000+25-625>>2

simplifies to 400>>2=100

Rank Name Operator
1 Scope resolution. ::
2 Member selection, subscription, increment, and decrement. . ->
2 Member selection, subscription, increment, and decrement. . ->()++ --
3 sizeof, prefix increment and decrement, complement, and, not, unary minus and plus, address-of and dereference, new, new[] , delete, delete[], casting, sizeof() ++ --^ !- +& *()
4 Member selection for pointer. .* ->*
5 Multiply, divide, modulo. * / %
6 Add, subtract. + -
7 Shift (shift left, shift right). << = >>=
8 Inequality relational. == !=
9 Equality, inequality. == !=
10 Bitwise AND. &
11 Bitwise exclusive OR. ^
12 Bitwise OR |Rank Name Operator. |
13 Logical AND &&. &&
14 Logical OR ||
15 Conditional ?:
16 Assignment operators = *= /= %= += -= <<= >>= &= |= ^=
17 comma ,

Home | API | MFC | C++ | C | Previous | Next
The Basics | Variables and Constants | Arrays | C-strings | Expressions and Operators | Controlling Program Flow | C++ Functions | Pointers and References | Memory Map and Free Store | Smart Pointers | Classes | Structures | Inheritance | Polymorphism | Templates | The Standard Template Library | The STL String Class | Namespace | Type Conversions | Input and Output Streams | The C++ Preprocessor | Exception Handling

Last Updated: 15 September 2022