Operators and Expressions in C++
In C++, operators are special symbols that perform specific operations on one, two, or three operands and return a result. The operands can be variables, literals, or expressions. Some examples of operators in C++ are:
- Arithmetic operators: +, -, *, /, % (modulus)
- Comparison operators: ==, !=, >, <, >=, <=
- Logical operators: && (and), || (or), ! (not)
- Assignment operators: =, +=, -=, *=, /=, %=
- Conditional operator: ?:
Expressions in C++ are combinations of operators and operands that evaluate to a value. For example, the expression 3 + 4 evaluates to 7, and the expression x < y compares the values of the variables x and y and returns true or false. Expressions can also be nested, such as the expression (x + y) * (z - 3).
It's also worth noting that C++ has some built-in functions that can help in handling expressions, like pow(a,b)
which raises a to the power of b.
In addition to the operators and expressions I mentioned earlier, C++ also has some other features that can be used in expressions:
- Increment and decrement operators: ++ (increment by 1), -- (decrement by 1)
- Bitwise operators: & (and), | (or), ^ (xor), ~ (not), << (left shift), >> (right shift)
- Ternary operator: a ? b : c (evaluates to b if a is true, otherwise c)
It's also important to note the precedence and associativity of operators in C++, which determine the order in which expressions are evaluated. For example, the multiplication operator (*) has higher precedence than the addition operator (+), so the expression 3 + 4 * 5 is evaluated as 3 + (4 * 5) = 3 + 20 = 23. The associativity of an operator determines the order in which operations are performed when two or more operators of the same precedence appear in an expression. For example, the associativity of the assignment operator (=) is right-to-left, so the expression a = b = c is evaluated as a = (b = c).
It's also worth noting that C++ has some built-in functions that can help in handling expressions, like pow(a,b)
which raises a to the power of b, abs(x)
which returns the absolute value of x and sqrt(x)
which returns the square root of x.
In addition to the built-in functions and operators I mentioned earlier, C++ also allows you to create your own functions, which can be used in expressions as well. Functions are blocks of code that perform a specific task, and can take zero or more input arguments (also called parameters) and return zero or one value.
Functions are declared with the keyword "function", followed by the return type, the name of the function and the list of parameters, enclosed in parentheses. For example, the following is a function that takes two integers as input, and returns the sum of them:
int add(int a, int b) {
return a + b;
}
Functions can be called by providing the necessary arguments in the parentheses after the function name. For example, we can call the add function we just defined like this:
int result = add(3, 4);
You can also call the functions in expressions, like this:
int x = 10;
int y = 20;
int z = add(x, y) * 2;
It's also worth noting that C++ supports function overloading, which means that you can have multiple functions with the same name but different parameter lists. This allows you to write more versatile functions that can operate on different types or numbers of arguments.
Also, C++ has a lot of libraries that provide additional functionality, like the <cmath>
library which provides mathematical functions like sin
, cos
, tan
, log
, exp
and others.