Language: EN


The IF conditional

The conditional IF is the simplest of the 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 mode would look something like this:


This translated into code, would look like this:

    // 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 options of valid expressions as conditions are,

  • false or true
  • A boolean variable
  • An expression that evaluates as boolean
  • Calling 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 as 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(calculateIsValid())   // calling a function that returns a boolean

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

  • A single instruction
  • A block
	// a single instruction to execute

	// a block with actions to execute

In reality, 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

Practically all programming languages have an IF conditional. Let’s see 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 languages such as 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 passed' 😮

For example, in VB a conditional looks like this,

If height > 1.90 Then
	' show message 'Congratulations! You 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 Operation Advanced

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

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


Therefore, there are several ways to execute the conditional jump. For example, the one I have 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

As we can see, it is exactly the behavior that we have said an IF conditional has.