Basic Pascal Tutorial/Chapter 1/Assignment and Operations/es

From Lazarus wiki
Revision as of 17:43, 10 August 2012 by Aguila3000 (talk | contribs)
Jump to navigationJump to search

1E - Asignaciones y operaciones (author: Tao Yue, state: unchanged)

Una vez declaras una variable, tu puedes almacenar valores en este. Esto es llamado asignación. Para asignar un valor a una variable, sigue la siguiente sintaxis:

variable_name := expression;

Note que a diferencia de otros lenguajes, su operador de asignacion es exactamente un signo igual, pero Pascal usa dos puntos siguientes seguido del signo igual, de manera similar como se hace en muchos sistemas de álgebra computacional.

La expresion puede ser un valor simple:

some_real := 385.385837;

o puede ser una secuencia aritmetica:

some_real := 37573.5 * 37593 + 385.8 / 367.1;

Los operadores aritmeticos en Pascal som:

Operador Operación Operandos Resultado
+ Adición o operador unario positivo real o integer real o integer
- Substración o operador unario negativo real o integer real o integer
* Multiplicación real o integer real o integer
/ División real real o integer real
div División con enteros integer integer
mod Modulo (residuo de una división) integer integer

div ymod sólo trabaja con integers. / trabaja con ambos reals y integers pero siempre se obtienne un real. Las otras operaciones trabajan sobre ambos reals y integers. Cuando se mezclan integers y reals, el resultado siempre será un real de otro modo se obtendría una perdida de datos. Esa es la razón del porqué Pascal usa dos diferentes operaciones para la division. Es decir 7 / 2 = 3.5 (real), pero 7 div 2 = 3 (y 7 mod 2 = 1 ya que es el residuo).

A cada variable sólo se le puede asignar un valor que es del mismo tipo de dato. Por lo tanto, no puedes asignar un valor real una variable de tipo integer. Sin embargo, ciertos tipos de datos se pueden convertir a otro tipo de dato. Esto sucede cuando se asigna valores de tipo integer a variables de tipo real. Suponga que tiene esta sección de declaración de variables:

var
  some_int : integer;
  some_real : real;

Cuando el siguiente bloque de instrucciones se ejecuta,

some_int := 375;
some_real := some_int;

some_real tiene el valor 375.0.

Changing one data type to another is referred to as typecasting. Modern Pascal compilers support explicit typecasting in the manner of C, with a slightly different syntax. However, typecasting is usually used in low-level situations and in connection with object-oriented programming, and a beginning programming student will not need to use it. Here is information on typecasting from the GNU Pascal manual.

In Pascal, the minus sign can be used to make a value negative. The plus sign can also be used to make a value positive, but is typically left out since values default to positive.

Do not attempt to use two operators side by side, like in:

some_real := 37.5 * -2;

This may make perfect sense to you, since you're trying to multiply by negative-2. However, Pascal will be confused — it won't know whether to multiply or subtract. You can avoid this by using parentheses to clarify:

some_real := 37.5 * (-2);

The computer follows an order of operations similar to the one that you follow when you do arithmetic. Multiplication and division (* / div mod) come before addition and subtraction (+ -), and parentheses always take precedence. So, for example, the value of: 3.5*(2+3) will be 17.5.

Pascal cannot perform standard arithmetic operations on Booleans. There is a special set of Boolean operations. Also, you should not perform arithmetic operations on characters.

previous contents next