programacion-condicional-if

The IF conditional

  • 4 min

The IF conditional is the simplest of conditionals. It is a control structure that evaluates a condition and executes a block of code if that condition is true.

In natural language, the IF conditional means,

If this happens 🡆 do this

Represented as a diagram, it would look something like this:

programacion-if

IF Diagram

Translated into code, it would look like this:

if(condition)
{
    // Code to execute if the condition is true
}
Copied!

The condition is an expression that must be evaluable as true or false. If the condition is true, the block of code inside the IF conditional is executed. Otherwise, the block is skipped and the program continues with the next instruction.

Valid expression options for conditions are,

  • false or true
  • A boolean variable
  • An expression that evaluates to a boolean
  • A function call that returns a boolean

Here are some examples of valid conditions to use in IF conditionals,

if(true) // with a literal. It doesn't make much sense, but it works

if(a < 3)  // examples like comparison
if(a == 3)
if(a != 3)

var isValid = true
if(isValid) // boolean variable

if(1)  // in some languages, certain literals are automatically evaluated to true or false

if(calcularIsValid())   // call to a function that returns a boolean
Copied!

On the other hand, the code to execute if the condition is true can generally be,

  • A single instruction
  • A block
if(condition)
    // a single statement to execute

if(condition) 
{
    // a block with actions to execute
}
Copied!

Actually, it’s the same. Remember that a block counts as a single instruction for the compiler or interpreter)

Examples of IF conditionals in different languages

Virtually all programming languages have an IF conditional. Let’s see it with an example.

  • We will evaluate a variable containing a person’s height
  • If it is greater than 1.90, the person is very tall

This IF conditional, written in languages like C++, C#, Java, Kotlin or JavaScript, is identical, and looks like this,

if (height > 1.90)
{
    // show message 'Wow! You are very tall!' 😮
}
Copied!

It is very similar in the case of PHP, with the tiny peculiarity that in PHP variables start with ’$’.

if ($height > 1.90)
{
    // show message 'Wow! You are very tall!' 😮
}
Copied!

While in Python it would have the following form

if height > 1.90:
    # show message 'Congratulations! You have passed' 😮
Copied!

As another example, in VB a conditional looks like this,

If height > 1.90 Then
    ' show message 'Congratulations! You have passed' 😮
End If
Copied!

As we can see, in general, all IF conditionals have the same structure and functionality, with small syntax differences between languages.

Internal Functioning Advanced

Under the hood, an IF conditional is a bifurcation of the normal program flow. For this, a conditional jump instruction is used. This is similar to a GO-TO jump instruction but is only executed if a condition is met.

Depending on the architecture, different conditional jumps are available. For example, if two values in memory are equal, or different, etc.

programacion-if-saltos

Conditional jump generated by the IF

Therefore, there are several ways to execute the conditional jump. For example, the one I put in the diagram is the simplest.

  • If the condition is true, the program continues, executing the code
  • If the condition is false, the program jumps, and does not execute the body of the code

Which, as we see, is exactly the behavior we said an IF conditional has.