Language: EN

programacion-condicionales

What are conditionals

Conditionals are control flow structures that allow a program to make decisions based on the evaluation of one or more conditions.

Conditionals allow us to branch the flow of the program, executing certain instructions only if a specific condition is met.

Conditionals are essential for programming. In fact, they are the foundation of programming. Without conditionals, programs would be a flow of instructions that execute from start to finish. We could only make “calculators”.

Thanks to conditionals, a program can be like one of those games or books where we have to make decisions. Depending on the decision we make, we can have one ending or another.

Well, thanks to conditionals, our program can have many possible execution paths and many possible “endings”.

We can represent a conditional using the following symbol, which is more or less standard in flowcharts.

programacion-condicional-simbolo

Basic conditional

Understanding Conditionals

Conditionals are structures that are easy to understand. In fact, we use them constantly in our daily lives. For example, when we say:

If it rains, then I take an umbrella.

In this case, we are facing a simple conditional. If the condition is met (raining), we take the umbrella. If not, we do nothing. We could represent it like this.

programacion-condicional-sencillo

If it rains, I take an umbrella

This type of conditional is usually called IF, and in code, it would look something like this,

if it rains:
    // take umbrella

Another example of a conditional could be needing an alternative action. This action will be executed only if the condition is not true. For example,

If it rains, then I take an umbrella

If not, I take a cap.

In this case, in addition to having an action if the condition is met, we have an action to perform if the condition is not met.

programacion-condicional-doble

If it rains, I take an umbrella, if not I take a cap

We call this type of structure IF-ELSE, and seen in code, it would look something like this.

if it rains:
    // take umbrella
else:
    // take cap

Types of Conditionals

In programming, there are usually these and other types of conditionals. The most common are: