Language: EN

programacion-condicional-if

The IF conditional

The conditional IF 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 conditional IF means,

If this happens 🡆 do this

Which represented in diagram form would look something like this:

programacion-if

IF Scheme

This translated into code would look like this:

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

The condition is an expression that must be able to be evaluated 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.

The valid expression options as conditions are,

  • false or true
  • A boolean variable
  • An expression that evaluates as boolean
  • The call of a function 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

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

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

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

In reality, it’s the same. Remember that a block counts as a single statement for the compiler or interpreter’s purposes)

Examples of IF conditionals in different languages

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

  • We are going to evaluate a variable that contains a person’s height
  • If it is greater than 1.90, the person is very tall

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

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

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!' 😮
}

While in Python it would look like this

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

To give another example, in VB a conditional looks like this,

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

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

Internal workings Advanced

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

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

programacion-if-saltos

Conditional jump generated by IF

Therefore, there are several ways to execute the conditional jump. For example, the one I have put in the scheme 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 have said that an IF conditional has.