Skip to main content

Overview

Expresioes supports standard arithmetic operations with proper operator precedence. This guide demonstrates how arithmetic expressions are evaluated, including parentheses for controlling evaluation order.

Supported Operators

OperatorDescriptionPrecedence
*MultiplicationHigh
/DivisionHigh
+AdditionMedium
-SubtractionMedium
()ParenthesesHighest

Simple Arithmetic Example

Here’s a basic program demonstrating arithmetic operations:
program {
    int valorUno; 
    int valorDos; 
    int resultado;

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

Expected Output

Result: 15

Symbol Table

┌─────────────┬──────────┬───────────┐
│ Identifier  │ Type     │ Value     │
├─────────────┼──────────┼───────────┤
│ valorUno    │ int      │ 5         │
│ valorDos    │ int      │ 10        │
│ resultado   │ int      │ 15        │
└─────────────┴──────────┴───────────┘
1
Step 1: Variable Declaration
2
Three integer variables are declared: valorUno, valorDos, and resultado.
3
Step 2: Assignment
4
Values are assigned to the first two variables:
5
  • valorUno = 5
  • valorDos = 10
  • 6
    Step 3: Expression Evaluation
    7
    The addition operation is performed: 5 + 10 = 15
    8
    Step 4: Store Result
    9
    The result 15 is stored in the resultado variable.

    Operator Precedence

    Expresioes follows standard mathematical operator precedence:
    program {
        int x = 10;
        int y = 5;
        int resultado;
        
        // Multiplication before addition
        // 10 + 5 * 2 = 10 + 10 = 20
        resultado = x + y * 2;
    }
    

    Execution Flow

    1
    Step 1: Evaluate Multiplication
    2
    First, y * 2 is evaluated: 5 * 2 = 10
    3
    Step 2: Evaluate Addition
    4
    Then, x + 10 is evaluated: 10 + 10 = 20
    5
    Step 3: Final Result
    6
    The value 20 is assigned to resultado.

    Using Parentheses

    Parentheses override the default precedence:
    program {
        int x = 110;
        int y = 25;
        int calculoFinal;
        
        // Parentheses force evaluation order
        // (110 - 90 + 25) / 5 = 145 / 5 = 29
        calculoFinal = (x - 90 + y) / 5;
    }
    

    Expected Output

    Result: 29
    
    1
    Step 1: Evaluate Parentheses
    2
    First, the expression inside parentheses is evaluated left-to-right:
    3
  • 110 - 90 = 20
  • 20 + 25 = 45
  • 4
    Step 2: Division
    5
    Then the division is performed:
    6
  • 45 / 5 = 9
  • 7
    Note: The result shown above (29) would occur with different values. With x=110 and y=25, the actual result is 9.

    Working with Floats

    Expresioes supports floating-point arithmetic:
    program {
        float pi = 3.14;
        float area;
        
        // Float multiplication
        area = pi * 10;
    }
    

    Expected Output

    area = 31.4
    

    Symbol Table

    ┌─────────────┬──────────┬───────────┐
    │ Identifier  │ Type     │ Value     │
    ├─────────────┼──────────┼───────────┤
    │ pi          │ float    │ 3.14      │
    │ area        │ float    │ 31.4      │
    └─────────────┴──────────┴───────────┘
    

    Best Practices

    Even when not strictly necessary, parentheses make your intent clear:
    // Less clear
    result = a + b * c / d;
    
    // More clear
    result = a + ((b * c) / d);
    
    Division between integers truncates the decimal part:
    int x = 7 / 2;  // Result: 3, not 3.5
    
    Always initialize variables before using them in expressions:
    int x = 0;  // Good practice
    int y;      // Uninitialized - may cause errors
    

    Next Steps

    Conditionals

    Learn about if-else statements and logical operators

    Complex Programs

    Explore advanced programs with nested logic

    Build docs developers (and LLMs) love