Skip to main content

Overview

Conditional statements allow your programs to make decisions based on boolean expressions. Expresiones supports if-else statements with logical operators for complex conditions.

Logical Operators

OperatorDescriptionExample
==Equal tox == 5
!=Not equal tox != 0
>Greater thanx > 10
<Less thanx < 20
>=Greater than or equalx >= 15
<=Less than or equalx <= 100
&&Logical ANDx > 0 && y < 10
``Logical OR`x == 1y == 2`
!Logical NOT!activo

Simple Conditional

Here’s a basic if-else statement:
program {
    int valorUno; 
    int valorDos; 
    int resultado;
    int exito; 
    int error;

    valorUno = 5; 
    valorDos = 10; 
    resultado = valorUno + valorDos;

    if(resultado > 10){
        exito = 1; 
    } else {
        error = 0; 
    }
}

Expected Output

Condition evaluated: true
exito = 1

Symbol Table

┌─────────────┬──────────┬───────────┐
│ Identifier  │ Type     │ Value     │
├─────────────┼──────────┼───────────┤
│ valorUno    │ int      │ 5         │
│ valorDos    │ int      │ 10        │
│ resultado   │ int      │ 15        │
│ exito       │ int      │ 1         │
│ error       │ int      │ (unset)   │
└─────────────┴──────────┴───────────┘
1
Step 1: Calculate Result
2
resultado = valorUno + valorDos evaluates to 15.
3
Step 2: Evaluate Condition
4
The condition resultado > 10 is checked:
5
  • 15 > 10 evaluates to true
  • 6
    Step 3: Execute True Branch
    7
    Since the condition is true, the if block executes:
    8
  • exito = 1 is assigned
  • 9
    Step 4: Skip Else Branch
    10
    The else block is skipped, so error remains uninitialized.

    Compound Conditions with AND

    Use && to require multiple conditions to be true:
    program {
        int x = 10;
        int y = 5;
        bool activo = 1;
        int resultado;
        
        resultado = x + y * 2;  // 10 + 10 = 20
        
        if (resultado > 15 && activo == 1) {
            x = x + 100;
        } else {
            resultado = 999;
        }
    }
    

    Expected Output

    Condition: 20 > 15 && 1 == 1
    Both conditions true - executing if block
    x = 110
    
    1
    Step 1: Evaluate First Condition
    2
    resultado > 1520 > 15true
    3
    Step 2: Evaluate Second Condition
    4
    activo == 11 == 1true
    5
    Step 3: Combine with AND
    6
    true && truetrue
    7
    Step 4: Execute If Block
    8
    x = x + 100x = 110

    Compound Conditions with OR

    Use || when any condition can trigger the branch:
    program {
        int temperatura = 25;
        int humedad = 80;
        int alerta;
        
        if (temperatura > 30 || humedad > 75) {
            alerta = 1;
        } else {
            alerta = 0;
        }
    }
    

    Expected Output

    Condition: 25 > 30 || 80 > 75
    Second condition true - executing if block
    alerta = 1
    
    1
    Step 1: Evaluate First Condition
    2
    temperatura > 3025 > 30false
    3
    Step 2: Evaluate Second Condition
    4
    humedad > 7580 > 75true
    5
    Step 3: Combine with OR
    6
    false || truetrue
    7
    Step 4: Execute If Block
    8
    alerta = 1

    Nested Conditionals

    You can nest if-else statements for complex logic:
    program {
        int x = 110;
        int y = 5;
        
        if (x >= 110) {
            y = y * y;  // 5 * 5 = 25
        } else {
            y = 0;
        }
    }
    

    Expected Output

    Condition: 110 >= 110
    Condition true
    y = 25
    
    1
    Step 1: Evaluate Outer Condition
    2
    x >= 110110 >= 110true
    3
    Step 2: Execute True Branch
    4
    Calculate y * y:
    5
  • 5 * 5 = 25
  • Assign result to y
  • 6
    Step 3: Skip Else Branch
    7
    The else branch (y = 0) is not executed.

    Boolean Variables

    Expresioes uses 1 for true and 0 for false:
    program {
        bool activo = 1;  // true
        bool inactivo = 0;  // false
        int resultado;
        
        if (activo == 1) {
            resultado = 100;
        }
        
        if (inactivo == 0) {
            resultado = resultado + 50;
        }
    }
    

    Expected Output

    First condition: true
    Second condition: true
    resultado = 150
    

    Common Patterns

    Check if a value falls within a range:
    if (x >= 0 && x <= 100) {
        // x is between 0 and 100
    }
    
    Remember to use == for comparison, not =:
    // Wrong - this is assignment!
    if (x = 5) { }
    
    // Correct - this is comparison
    if (x == 5) { }
    
    Use else to handle all other cases:
    if (condicion) {
        resultado = 1;
    } else {
        resultado = 0;  // Default case
    }
    
    Combine multiple logical operators:
    if ((x > 0 && x < 10) || y == 100) {
        // Complex condition
    }
    

    Best Practices

    Be careful with nested conditions - too many levels can make code hard to understand. Consider refactoring complex logic into simpler conditions.
    Use parentheses to make complex conditions more readable:
    // Less readable
    if (a > 5 && b < 10 || c == 0) { }
    
    // More readable
    if ((a > 5 && b < 10) || c == 0) { }
    

    Next Steps

    Basic Arithmetic

    Review arithmetic operations and expressions

    Complex Programs

    See how arithmetic and conditionals work together

    Build docs developers (and LLMs) love