Priority table. Unary, binary, ternary and n-ary operations. Prefix and postfix operator form

Priority table. Unary, binary, ternary and n-ary operations. Prefix and postfix operator form


Contents


1. Table of priority of operations (operators) of C# language

Below is a table of the priority of operations (operators) in C#. Operations and operators follow in descending order of priority.

C# Table priority operations

Based on the table, the operations or operators that have the highest priority are processed first. For example, in the expression

x = a + b * (c – 5);

operations are performed in sequence, as shown in the figure.

C# Priority of operations

Figure 1. Priority of operations in accordance with the table of priority

Explanation to Figure 1. Operations are performed in the following sequence:

  • 1 – the expression is in brackets (c-5);
  • 2 – a binary operation ‘‘ is performed, which receives two operands: the variable c (left) and the number 5 (right);
  • 3 – multiply operation is being performed *;
  • 4 – binary operator + (addition) is performed;
  • 5 – the assignment operator = is executed. This operator has the lowest priority (in accordance with the table).

 

2. What operations (operators) are called unary, binary and n-ary?

Any operation or operator to perform calculations requires a certain number of operands. By the number of operands, operators are divided into the following groups:

  • unary. To perform calculations unary operators require one operand in its syntax. For example, the operation of designation of a negative number  (minus) is unary and requires the presence of an operand to the right of the operation sign (-8, -2.85);
  • binary. To perform calculations, binary operations require two operands. As a rule, these operands are placed to the left and right of the operation sign. For example, the operation * (multiplication) is binary;
  • ternary. These are special operations (operators) that require three operands to perform calculations. In C#, there is a ternary operation?:;
  • n-ary. These are operations that require more than three operands to perform calculations.

There are operators that by their designation are found in more than one group. For example, the minus () operation can be unary (designation of negative numbers) and binary (subtraction operation of numbers).

 

3. What does the concept of prefix and postfix form mean?

Some operators in C# may have prefix and postfix form. These operators include the increment operators (++) and decrement operators (– –).

Increment operators (++) increase by 1 the value of an integer value. Accordingly, the decrement operators decrease by 1 the value of the integer value. In the prefix form of the increment and decrement operators, the operator ++ or – – is placed before the variable that is being processed. In the postfix form of the increment and decrement operators, the corresponding operator is placed after the variable being processed.

For example.

int t = 5;
++t; // t = 6; - the prefix form of operator ++
t++; // t = 7; - the postfix form of operator ++
--t; // t = 6; - the prefix form of operator --
t--; // t = 5; - the postfix form of operator --

 


Related topics