Operators and Operands

What are operators, what are operands and how they work in programming languages. We will see the types of operators and the execution priorities when we have several in the same expression.

In this article we are going to introduce one of the main topics of programming languages, such as operators and operands, which are used to carry out all kinds of expressions in which we work with data, with different types of operations.

We will approach this subject from a generic point of view, without dealing with a particular programming language, explaining which are the most common operators in programming languages. However, you should know that each language can apply certain symbols to the operators, which can change between them. There is a well-defined pattern in most cases, like symbols in mathematics, but in general you should know that they can change from language to language.

What are operators

Before beginning to see what the operators are, it is convenient to explain what they are, in a general way, although I think that most people have it in mind from the study of subjects such as mathematics.

The operators are elements that relate in a different way, the values ​​with which we work in programming languages. In programming languages ​​we use operators to manipulate values ​​and transform them, in order to achieve the goals of programs.

What are operands

The operands are the values ​​that are used to feed the operators. For example, in the operator addition we need two operands to add both values.

Operands can be stored in variables or constants, but they can also simply be values ​​that are written literally in the program code.

Types of operators

Next we are going to describe the most common types of operators that we will find in most programming languages.

Most languages ​​have other types of operators besides these. For the moment and for simplicity we are going to stick in this article to the arithmetic, relational operators and the logical operators, which are the most common when programming.

Arithmetic Operators

Arithmetic operators allow you to perform mathematical operations with literal values, with variables, or with constants.

Arithmetic operators can be used with integer or real data types. If both are integers, the result is an integer; if either of them is real, the result is real.

We now present a list of arithmetic operators, which are generally used with the same symbols in all languages, since they are inherited from mathematics.

  • + sum
  • – Subtraction
  • * Multiplication
  • / Division
  • mod Modulo (remainder or remainder of integer division)
  • ^ Raise to a power
See also  CSS sprites

Many languages ​​such as C, Java, Javascript, C#, and others use the percent symbol (%) for the modulo operator, or remainder of integer division. Many languages ​​use the operator with two asterisks (**) to raise to powers.

Examples of using arithmetic operators:

Below you can find some expressions with arithmetic operators and the results they would get.

Expression: 7 / 2

Result: 3.5

Expression: 12 mod 7

Result: 5

Expression: 4 + 2 * 5

Result: 14

Arithmetic Operators Priority

When we find several operators in the same expression, programming languages ​​will have to evaluate them in a certain order. We know this order as priority or precedence of operators.

When it comes to arithmetic operators, it is very easy to imagine the priority of some with respect to others, since they work the same as in mathematics. For example, a multiplication will always be evaluated before an addition.

However, it is not always so easy to deduce how the associativity of the operators is going to be solved, so you have to learn some rules of precedence that we are going to summarize at this point.

Within the same expression, the operators are evaluated in the following order:

  1. ^ exponentiation (in languages ​​where this operator exists)
  2. *, /, mod (Multiplication, division, modulo)
  3. +, – (add and subtract)

In the case in which operators with the same level of priority are associated in the same expression, they are evaluated from left to right.

This is how it works in most programming languages, but you should examine how it does it in yours or consult the documentation.

How to break operator precedence rules with parentheses

In the case that you want to break the rules of precedence of the operators you can use the parentheses. They work with any type of operators and behave just like in mathematics. You can define using the parentheses which operators are to be associated with which operands, regardless of the rules mentioned above.

  • All expressions in parentheses are evaluated first
  • Expressions with nested parentheses are evaluated from inside to outside
  • The innermost parenthesis is evaluated first.

Now let’s look at examples of operator associativity in more complex statements, using parentheses to mark how we want precedence to occur:

4 + 2 * 5 = 14 23 * 2 / 5 = 9.2 3 + 5 * (10 – (2 + 4)) = 23 2.1 * (1.5 + 12.3) = 2.1 * 13.8 = 28.98

alphanumeric operators

When we work with strings we can also perform operations, although it would really be just one in most languages ​​which we call “concatenation”.

We mainly use the “+” symbol to concatenate strings, which consists of placing one string after another.

Then, depending on the language, the symbol “-” may or may not work to concatenate strings, eliminating the white spaces.

String Concatenation Example

a = “hello” b = “world” c = a + b

After the above code c will be “helloworld”.

Notice that there is no space in between because the computer does not know that they are two words. You are simply asking it to put them together and as a result you have the two strings one after the other. If you wanted to put them together you could have done something like this: c = a + ” ” + b

Relational Operators

They are used to establish a relationship between two values. It then compares these values ​​with each other and this comparison produces a true or false result (true or false).

You will find that many times we refer to “true” or “false” as “true” or “false”, which is the English translation. In fact true and false form the set of values ​​that define one of the usual types of programming, the boolean type.

Relational operators generally compare values ​​of the same type (numeric with numeric or string with string). But there are also programming languages, such as those with dynamic typing (Javascript, PHP and others) where it is allowed to compare values ​​of different types.

All relational operators have the same priority level in their evaluation. In general, relational operators have lower precedence than arithmetic ones.

Types of Relational operators

  • > greater than
  • < less than
  • >= Greater than or equal to
  • <= Less than or equal to
  • < > Different
  • == Same

The symbol “==” compares two values. You should not confuse it with the “=” sign that is commonly used to assign a value to a variable.

The symbol “<>” for distinct, in many programming languages ​​is expressed as “!=”. You will see it like this in languages ​​like C or Java, Javascript and many others. The other signs are fairly standard, such as “<", ">“, “<>“, “<=", ">=”, which are used as is in most languages.

Now we are going to see several examples of the use of relational operators. To make things easier, let’s assume that we have three variables:

a = 10 b = 20 c = 30

Given the values ​​of the above variables, let’s look at these examples and the results that would be produced by combining multiple operators.

a + b > c False a – b < c True a - b == c False a * b < > c True

To understand the above examples remember that the arithmetic operators will be solved before the relational operators, so you have to solve the mathematical operations first and then check how the relational operators would be evaluated.

As we have pointed out, in many programming languages, generally those with strong types, you cannot compare values ​​of different types, so the following examples would not be valid:

a < b < c 10 < 20 < 30 T > 5 < 30

Logical operators

These operators are used to establish relationships between logical values. The logical values ​​are the boolean values:

  • true
  • false

Furthermore, since relational operators result in a logical operator, which is deduced by comparing the operands, logical operators can have as operands the result of a relational expression.

The types of Logical operators can also change between programming languages, but generally we will find the following:

  • And (And operation)
  • Or (OR operation)
  • Not

As logical operators we find ourselves in many languages ​​with the symbol “!” for denial. The symbol “&&” for the operation of “and” and !||” for the “or”.

Example tables using logical operators

Now we are going to see examples of the use of logical operators. We are going to represent them by means of tables with the possible operations and the results that the different combinations of values ​​would have.

For the following examples, to be more concise, T means true and F false.

not operator

The NOT operation makes the change to the opposite value. If it was true it becomes false and vice versa.

NOT T = F NOT F = T

AND operator

The AND operation evaluates to true when both related operators are true. If one of the two is false, or both are false, then the result is false.

T AND T = T T AND F = F F AND T = F F AND F = F

Or operator Or operator

The OR operation returns true when at least one of the two operators is true. If both operators are false, then returns false.

T OR T = T T OR F = T F OR T = T F OR F = F

Logical Operators Priority

The order of precedence of the logical operators among themselves is as follows, from most precedent to least:

  1. Not
  2. and
  3. gold

Priority of Operators in General

As you have seen, there are several types of operators and each of them has its precedence or priority, which defines how the operands will be associated. The languages ​​support expressions in which various types of operators are related, in which case we will have to know the operator precedence table of the…

Loading Facebook Comments ...
Loading Disqus Comments ...