Skip to main content

Overview

Expresiones supports conditional execution through if-else statements, allowing your program to make decisions based on conditions.

If Statement

The if statement executes a block of code when a condition is true:
if (condition) {
    // Code to execute when condition is true
}

Basic If Example

program {
    int x = 10;
    
    if (x > 5) {
        x = x + 100;
    }
    // x is now 110
}

If-Else Statement

The else clause provides an alternative block when the condition is false:
if (condition) {
    // Code when condition is true
} else {
    // Code when condition is false
}

If-Else Example

program {
    int valorUno = 5;
    int valorDos = 10;
    int resultado = valorUno + valorDos;
    int exito;
    int error;
    
    if (resultado > 10) {
        exito = 1;
    } else {
        error = 0;
    }
}
The else clause is optional. You can use if without else when you don’t need an alternative action.

Conditions

Conditions are expressions that evaluate to true or false. Expresiones supports:
  • Relational operators
  • Logical operators
  • Parenthesized conditions

Relational Conditions

Compare two expressions:
if (x > 10) { }          // Greater than
if (x < 10) { }          // Less than
if (x == 10) { }         // Equal to
if (x != 10) { }         // Not equal to
if (x >= 10) { }         // Greater than or equal
if (x <= 10) { }         // Less than or equal

Logical Conditions

Combine multiple conditions:
program {
    int x = 10;
    bool activo = 1;
    
    // AND operator (&&)
    if (x > 5 && activo == 1) {
        x = x + 100;
    }
    
    // OR operator (||)
    if (x < 5 || activo == 1) {
        x = x + 50;
    }
    
    // NOT operator (!)
    if (!(x > 100)) {
        x = 0;
    }
}
Logical operators allow you to create complex conditions by combining simpler ones.

Nested If Statements

You can nest if statements inside other if blocks:
program {
    int x = 10;
    int y = 5;
    bool activo = 1;
    int resultado = x + y * 2;
    
    if (resultado > 15 && activo == 1) {
        // Outer if block
        x = x + 100;
        
        // Nested if-else
        if (x >= 110) {
            y = y * y;  // 5 * 5 = 25
        } else {
            y = 0;
        }
    } else {
        // Outer else block
        resultado = 999;
    }
}
Be careful with deeply nested conditions as they can make code harder to understand.

Control Flow Grammar

The formal grammar for control flow:
instrucciones
    : SI PAR_IZQ condicion PAR_DER bloque (SINO bloque)?

bloque : LLAVE_IZQ instrucciones* LLAVE_DER

condicion
    : condicion O_LOGICO condicion           // OR (||)
    | condicion Y_LOGICO condicion           // AND (&&)
    | NO_LOGICO condicion                    // NOT (!)
    | expr op expr                           // Relational
    | PAR_IZQ condicion PAR_DER             // Parentheses

Condition Operators

Logical Operators

AND (&&)

Both conditions must be true

OR (||)

At least one condition must be true

NOT (!)

Inverts the condition

Logical Operator Examples

program {
    int x = 10;
    int y = 5;
    
    // AND: both must be true
    if (x > 5 && y < 10) {
        // Executes: x > 5 is true AND y < 10 is true
    }
    
    // OR: at least one must be true
    if (x < 5 || y < 10) {
        // Executes: x < 5 is false BUT y < 10 is true
    }
    
    // NOT: inverts the result
    if (!(x < 5)) {
        // Executes: x < 5 is false, ! makes it true
    }
}

Relational Operators

OperatorMeaningExample
>Greater thanx > 10
<Less thanx < 10
==Equal tox == 10
!= or <>Not equal tox != 10
>=Greater than or equalx >= 10
<=Less than or equalx <= 10
Expresiones supports both != and <> for “not equal” comparison.

Complete Examples

Example 1: Simple Decision

program {
    int valorUno = 5;
    int valorDos = 10;
    int resultado = valorUno + valorDos;
    int exito;
    int error;
    
    if (resultado > 10) {
        exito = 1;
    } else {
        error = 0;
    }
}

Example 2: Complex Conditions

program {
    int x = 10;
    int y = 5;
    float pi = 3.14;
    bool activo = 1;
    int resultado = 0;
    
    // Arithmetic with precedence
    resultado = x + y * 2;  // 20
    
    // Complex condition with AND
    if (resultado > 15 && activo == 1) {
        x = x + 100;
        
        // Nested conditional
        if (x >= 110) {
            y = y * y;  // 25
        } else {
            y = 0;
        }
    } else {
        resultado = 999;
    }
    
    // Use updated values
    int calculoFinal = (x - 90 + y) / 5;
    float area = pi * 10;
}

Example 3: Multiple Conditions

program {
    int score = 85;
    bool passed = 0;
    bool failed = 0;
    
    // OR condition
    if (score >= 90 || score >= 60 && score < 90) {
        passed = 1;
    } else {
        failed = 1;
    }
}

Best Practices

Write conditions that are easy to understand:
// Good: Clear intent
if (resultado > 10) {
    exito = 1;
}

// Avoid: Complex nested logic without clear purpose
Add parentheses to complex conditions:
// Clear grouping
if ((x > 5 && y < 10) || activo == 1) {
    // ...
}
Avoid too many nested if statements:
// Good: Two levels
if (resultado > 15) {
    if (x >= 110) {
        y = y * y;
    }
}

// Avoid: Too many levels become hard to read

Control Flow Tips

1

Evaluate Conditions

Understand what condition you’re testing before writing the if statement
2

Consider Edge Cases

Think about boundary values (e.g., what happens at exactly 10?)
3

Test Both Branches

Ensure both the if and else blocks work correctly
4

Keep Blocks Focused

Each block should have a clear, single purpose

Build docs developers (and LLMs) love