# C++ Programming Code Examples

## C++ > Beginners Lab Assignments Code Examples

### Operators Precedence in C++ Programming

/* Operators Precedence in C++ Programming
Operator precedence determines how an expression is evaluated. Some operators will have higher precedence than others. For example, multiplication operator will have higher precedence than addition operator.
For example a = 2 + 3 * 5; here, a will be assigned 17, not 25 because operator * has higher precedence than +, so 3*5 gets multiplied first and then 2 gets added to the multiplication result of 3*5.
In the table below, operators with the highest precedence appear at the top of the table and operators with the lowest precedence appear at the bottom. In the expression, higher precedence operators will be evaluated first. */
Operator Category Associativity Precedence
() [] -> . ++ - - Postfix Left to right Highest
+ - ! ~ ++ - - (type)* & sizeof Unary Right to left
* / % Multiplicative Left to right
+ - Additive Left to right
<< >> Shift Left to right
< <= > >= Relational Left to right
== != Equality Left to right
& Bitwise AND Left to right
^ Bitwise XOR Left to right
| Bitwise OR Left to right
&& Logical AND Left to right
|| Logical OR Left to right
?: Conditional Right to left
= += -= *= /= %=>>= <<= &= ^= |= Assignment Right to left
, Comma Left to right Lowest

The C++ comments are statements that are not executed by the compiler. The comments in C++ programming can be used to provide explanation of the code, variable, method or class. If we write comments on our code, it will be easier for us to understand the code in the future. Also, it will be easier for your fellow developers to understand the code. By the help of comments, you can hide the program code also. There are two types of comments in C++: • Single Line comment. • Multi Line comment

As the name already suggests, these operators help in assigning values to variables. These operators help us in allocating a particular value to the operands. The main simple assignment operator is '='. We have to be sure that both the left and right sides of the operator must have the same data type. We have different levels of operators. Assignment operators are used to assign the value, variable and function to another variable. Assignment operators in C are some of the C Programming Operator, which are useful to assign the values to the declared variables. Let's discuss the various types of the assignment operators such as =, +=, -=, /=, *= and %=. The following table lists the assignment operators supported by the C language:

The bitwise operators are the operators used to perform the operations on the data at the bit-level. When we perform the bitwise operations, then it is also known as bit-level programming. It consists of two digits, either 0 or 1. It is mainly used in numerical computations to make the calculations faster. We have different types of bitwise operators in the C++ programming language. The following is the list of the bitwise operators: Bitwise AND operator is denoted by the single ampersand sign (&). Two integer operands are written on both sides of the (&) operator. If the corresponding bits of both the operands are 1, then the output of the bitwise AND operation is 1; otherwise, the output would be 0. This is one of the most commonly used logical bitwise operators. It is represented by a single ampersand sign (&). Two integer expressions are written on each side of the (&) operator.

A relational operator is used to check the relationship between two operands. C++ Relational Operators are used to relate or compare given operands. Relational operations are like checking if two operands are equal or not equal, greater or lesser, etc. Relational Operators are also called Comparison Operators.

Arithmetic Operator is used to performing mathematical operations such as addition, subtraction, multiplication, division, modulus, etc., on the given operands. For example: 6 + 3 = 9, 5 - 3 = 2, 3 * 4 = 12, etc. are the examples of arithmetic operators. Let's discuss the different types of Arithmetic Operators in the C programming. Plus Operator is a simple Plus (+) Operator used to add two given operands. We can use Plus Operator with different data types such as integer, float, long, double, enumerated and string type data to add the given operand. The minus operator is denoted by the minus (-) symbol. It is used to return the subtraction of the first number from the second number. The data type of the given number can be different types, such as int, float, double, long double, etc., in the programing language.

Logical Operators are used to compare and connect two or more expressions or variables, such that the value of the expression is completely dependent on the original expression or value or variable. We use logical operators 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. Assume variable A holds 1 and variable B holds 0:

Using Fibonacci numbers we calculate mid of data array to search the data item. 'Calculate' the mid of the array. Divide the array into two subarray. 'Compare the item' by mid element

C++ Program perform LU Decomposition of a matrix. LU decomposition factors a matrix as the product of a Lower Triangular Matrix and an upper 'triangular matrix'. Code sometimes