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

From Free Pascal wiki
Jump to navigationJump to search

български (bg) Deutsch (de) English (en) español (es) français (fr) 日本語 (ja) 한국어 (ko) русский (ru) 中文(中国大陆)‎ (zh_CN)

1E - Asignaciones y operaciones (autor: Tao Yue, state: cambiado)

   Una vez declaramos una variable, podemos almacenar valores en ella. A esto lo llamamos asignación.

   Para asignar un valor a una variable, se sigue la siguiente sintaxis:

 variable_name := expresión;

   Resaltar que a diferencia de otros lenguajes, en los que el operador de asignacion es exactamente un signo igual, Pascal usa dos puntos seguidos del signo igual, de manera similar a como se hace en muchos sistemas de álgebra computacional.

   La expresion puede ser un valor simple:

 un_numero_real := 385.385837;

   o puede ser una expresión aritmetica:

 un_numero_real := 37573.5 * 37593 + 385.8 / 367.1;

   Los operadores aritmeticos en Pascal son:

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 y mod sólo trabajan con números enteros (integer). / trabaja con números reales y enteros pero siempre se obtiene un real. Las otras operaciones trabajan sobre ambos tipos. Cuando se mezclan enteros y reales, el resultado siempre será un número real, de otro modo habría perdida de datos. Esa es la razón de 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 se puede 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 entero a variables de tipo real. Suponga que tiene esta sección de declaración de variables:

 var
  un_entero : integer;
  un_real : real;

   Cuando el siguiente bloque de instrucciones se ejecuta,

 some_int := 375;
 un_real := un_entero;

   un_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:

 un_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:

 un_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.

previo índice siguiente