programacion-condicionales

What are conditionals

  • 2 min

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 create branches in the program’s flow, 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 executed 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 easy-to-understand structures. In fact, we use them constantly in our daily lives. For example, when we do.

If it rains, then I take an umbrella

In this case, we are dealing with a simple conditional. If the condition is met (it rains) 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
Copied!

Another example of a conditional could require an alternative action. This action will only be executed 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, besides 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 be something like this.

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

Types of Conditionals

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