Language: EN

programacion-operadores-y-expresiones

Operators and expressions in programming

In the world of programming, expressions and operators are fundamental concepts that allow us to manipulate and combine values and data, and generate new results.

We had seen that our programs are made up of statements and blocks. Each statement is an executable action of our program.

Operators and expressions are basic building blocks with which we create the statements. The nomenclature varies from one language to another, and even from one textbook to another. But in general, I’m going to say that:

  • A statement does not return a result
  • An expression returns a result
// a, b and c with data
// + e = are operators
// a + b is an expression
// c = a + b is a statement
c = a + b;

Whether in languages like C#, C++, JavaScript, Python expressions and operators play a crucial role in the development of programs.

What are expressions

An expression is a combination of values, variables, constants, operators, and functions that are evaluated to obtain a result.

In other words, expressions are the way blocks of code are constructed to perform calculations or manipulate data. They can be as simple as a numeric constant or as complex as an expression involving multiple operations and variables.

Expressions can be arithmetic, relational, logical, or other types, depending on the purpose to be achieved. Let’s see some examples of expressions,

For example, this is how operators would be used in C#

var x = 5 + 3;  // Arithmetic expression
var y = (x > 7);  // Relational expression
var z = (x > 7) && (y == true);  // Logical expression

Which is basically identical to JavaScript

let x = 5 + 3  // Arithmetic expression
let y = (x > 7)  // Relational expression
let z = (x > 7) && (y == true)  // Logical expression

Or in Python

x = 5 + 3  ## Arithmetic expression
y = (x > 7)  ## Relational expression
z = (x > 7) and (y == True)  ## Logical expression

As we can see, expressions, apart from some small syntax differences, are basically similar in all languages.

What are operators

Operators are symbols or keywords that are used to manipulate and combine expressions. They allow different actions, such as performing arithmetic operations, comparing values, or assigning values to variables.

Essentially, operators are the fundamental tools for forming expressions in programming. For example, in the following examples of Python

x = 5
y = 3

result = 5 + 3;            # Sum operator +
result = x > y;            # Greater than comparison operator >
result = x > 4 and x > y;  # Logical and operator

There are various types of operators used in programming languages, including:

Internal operation of operators

Internally, each operator is translated into a set of instructions that perform the corresponding specific function.

At a lower level, operators are translated into a sequence of specific instructions that are executed by the microprocessor. Let’s take as an example the C++ language and suppose we have the following code:

int a = 5;
int b = 3;
int result = a + b;

When this code is compiled and executed, the addition operator + is translated into a series of microprocessor-level instructions that perform the addition operation.

Below is a simplified example of how the translation to microprocessor instructions could look:

  • Load the value of a into a microprocessor register.
  • Load the value of b into another microprocessor register.
  • Add the values of the two registers.
  • Store the result in a memory location assigned to the result variable.

It is important to note that this is a simplified representation and may vary depending on the microprocessor and architecture used.

Modern microprocessors are designed to execute multiple instructions simultaneously and optimize performance, using techniques such as pipelining and out-of-order execution.

At each step of the translation from the addition operator to microprocessor instructions, operations are being performed at the level of bits and internal processor registers to carry out the addition efficiently.

However, these low-level details are usually hidden from the programmer, as they are taken care of by the compiler and the operating system.