Assignment operation. The ternary operation ?:. Priority of operations
- What is the purpose of assignment operation? Examples
- What is the general form of a ternary operation ‘? :‘ ?
- Examples of the use of a ternary operation ? :
- Table of priorities of operations
1. What is the purpose of assignment operation? Examples
Assignment operation denoted by the symbol “=“.
The assignment operation is one of the most used in programs.
Operation of assignment is intended to assign a value to a variable.
General view of assignment operation:
variable = expression;
The assignment operation functions as follows: first, is calculated value of the expression on the right side, then this value is assigned to the variable, which is located on the left side of the sign ‘=‘.
When assigning, the type of variable and expression must be compatible. More about the type conversion in expressions is described here.
The assignment operation can take the form of a chain.
For example, it may be a chain of two assignment operations:
variable1 = variable2 = expression;
In this case, is performed the following algorithm:
- at first, the value of expression is calculated;
- result of the expression is assigned to variable2;
- result of the expression is assigned to variable1.
The chain (sequence) of the assignment operations can contain any number of assignments.
Example of the assignment operation.
// assignment operation int a; double x, y, z; x = -5.88; y = z = x; // y = -5.88, z = -5.88 x = y = z = 3.9; // x = 3.9, y = 3.9, z = 3.9 // assignment and types casting x = -8.6; a = (int)x; // a = -8 y = a; // y = -8.0 char c = 'T'; int code = (int)c; // code = 84 - the code of character 'T' // logical type boolean b; b = false && true; // b = false
2. What is the general form of a ternary operation ‘? :‘ ?
Ternary operation ? : replaces construction
if ... then ... else
The general form of operation
expression1 ? expression2 : expression3
In the above form the expression1 – any logical expression (boolean), the result of which is the value of true or false. If the expression value of expression1 is true, then expression2 is calculated, otherwise calculated expression3.
3. Examples of the use of a ternary operation ? :
Example 1. Using the ternary operator ? : to search for the maximum between two numbers. A fragment of program code.
// ternary operation ?: double a, b; double max; // maximum // inputting the values of a and b // ... // search the maximum between two numbers max = a>b ? a : b;
Example 2. Using the ternary operator ?: for searching the maximum value of between the three numbers a, b, c.
// ternary operation ?: double a, b, c; double max; // maximum // inputting the values of a, b, c // ... // Search a maximum between three numbers max = a>b ? a : b; max = max>c ? max : c;
4. Table of priorities of operations
The priority of operations in descending order -------------------------------------------------------------- ++ --(postfix) ++ --(prefix) ~ ! +(unary) -(unary) (types cast) * / % + - >> >>> << > >= < <= instanceof == != & ^ | && || ?: -> = += -= *= /= %= >>= <<= >>>=