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.
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.
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.
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 }}
program { int valorUno = 5; int valorDos = 10; int resultado = valorUno + valorDos; int exito; int error; if (resultado > 10) { exito = 1; } else { error = 0; }}
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;}