Overview
This guide showcases a complete Expresiones program that combines arithmetic operations, nested conditionals, logical operators, and multiple data types to demonstrate the full capabilities of the language.Complete Example Program
Here’s a comprehensive program fromentrada.txt that demonstrates all major features:
Program Execution Walkthrough
┌─────────────┬──────────┬───────────┐
│ Identifier │ Type │ Value │
├─────────────┼──────────┼───────────┤
│ x │ int │ 10 │
│ y │ int │ 5 │
│ pi │ float │ 3.14 │
│ activo │ bool │ 1 (true) │
│ resultado │ int │ 0 │
└─────────────┴──────────┴───────────┘
Final Symbol Table
After complete execution, the symbol table contains:Key Concepts Demonstrated
1. Multiple Data Types
1. Multiple Data Types
The program uses three different data types:
- int: For whole numbers (
x,y,resultado,calculoFinal) - float: For decimal numbers (
pi,area) - bool: For true/false values (
activo)
2. Operator Precedence
2. Operator Precedence
The program demonstrates proper evaluation order:
- Parentheses
() - Multiplication and Division
*/ - Addition and Subtraction
+- - Comparison operators
><>=<===!= - Logical AND
&& - Logical OR
||
x + y * 2 evaluates multiplication before addition.3. Nested Control Flow
3. Nested Control Flow
The program features a conditional inside another conditional:This allows complex decision-making based on multiple criteria.
4. Compound Logical Expressions
4. Compound Logical Expressions
The program uses Both conditions must be true for the block to execute.
&& (AND) to combine multiple conditions:5. Expression Complexity
5. Expression Complexity
The program shows how parentheses control evaluation in complex expressions:Without parentheses, the division would only apply to
y.Control Flow Diagram
Expected Console Output
When this program is compiled and executed, the output would show:Variations and Experiments
Experiment 1
Change the initial values:Expected changes:
resultado = 5 + 3 * 2 = 11- Condition
11 > 15is false - Else block executes:
resultado = 999 xremains5,yremains3
Experiment 2
Disable activo flag:Expected changes:
- First condition becomes false (due to AND)
- Else block executes
- No nested conditional runs
xremains10,yremains5
Experiment 3
Modify the nested condition:Expected changes:
x = 110, but condition needs 200- Else branch executes:
y = 0 calculoFinal = (110 - 90 + 0) / 5 = 4
Experiment 4
Use OR instead of AND:Expected changes:
- Condition is true if EITHER part is true
- More permissive than AND
- If block executes more often
Best Practices Illustrated
Common Pitfalls
Forgetting Operator Precedence
Forgetting Operator Precedence
Problem:Solution:
Remember multiplication happens first. Use parentheses if unsure:
Wrong Logical Operator
Wrong Logical Operator
Problem:Using AND when you meant OR (or vice versa) changes the logic completely.Solution:
- Use
&&when ALL conditions must be true - Use
||when ANY condition can be true
Integer Division Truncation
Integer Division Truncation
Problem:Solution:
If you need decimal results, use float division or cast to float.
Next Steps
Basic Arithmetic
Review fundamental arithmetic operations
Conditionals
Deep dive into if-else statements
Language Reference
Complete syntax reference
Architecture Overview
Understand the compiler internals