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 have seen that our programs are composed of statements and blocks. Each statement is an executable action of our program.

Operators and expressions are the basic building blocks with which we create statements. The nomenclature varies from one language to another (and even from one textbook to another). But in general, I will 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 program development.

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 code blocks 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 of other types, depending on the purpose you want to achieve. Let’s look at 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 in 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, aside from some small syntax differences, are basically similar in all languages.

What are operators

Operators are symbols or keywords used to manipulate and combine expressions. They allow for 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 from Python

x = 5
y = 3

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

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

Internal functioning of operators Advanced

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 the language C++ as an example 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 instructions at the microprocessor level that perform the addition operation.

Let’s see it with a simplified example of what the translation to microprocessor-level instructions might look like:

  • 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 for the variable result.

It is worth noting 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 of the operator + to microprocessor instructions, bit-level operations and internal processor registers are being performed to carry out the addition.

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