Difference between revisions of "Basic Pascal Tutorial/Chapter 1/Assignment and Operations/es"

From Lazarus wiki
Jump to navigationJump to search
(Created page with "1E - Assignment and Operations (author: Tao Yue, state: unchanged) Once you have declared a variable, you can store values in it. This is called assignment. To assign a valu...")
 
Line 1: Line 1:
1E - Assignment and Operations (author: Tao Yue, state: unchanged)
+
1E - Asignaciones y operaciones (author: Tao Yue, state: unchanged)
  
Once you have declared a variable, you can store values in it. This is called assignment.
+
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:
To assign a value to a variable, follow this syntax:
 
 
<syntaxhighlight>
 
<syntaxhighlight>
 
variable_name := expression;
 
variable_name := expression;
 
</syntaxhighlight>
 
</syntaxhighlight>
Note that unlike other languages, whose assignment operator is just an equals sign, Pascal uses a colon followed by an equals sign, similarly to how it's done in most computer algebra systems.
+
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.
  
The expression can either be a single value:
+
La expresion puede ser un valor simple:
 
<syntaxhighlight>
 
<syntaxhighlight>
 
some_real := 385.385837;
 
some_real := 385.385837;
 
</syntaxhighlight>
 
</syntaxhighlight>
or it can be an arithmetic sequence:
+
o puede ser una secuencia aritmetica:  
 
<syntaxhighlight>
 
<syntaxhighlight>
 
some_real := 37573.5 * 37593 + 385.8 / 367.1;
 
some_real := 37573.5 * 37593 + 385.8 / 367.1;
 
</syntaxhighlight>
 
</syntaxhighlight>
The arithmetic operators in Pascal are:
+
Los operadores aritmeticos en Pascal som:
 
{| style="background-color:#f5f5f5" cellspacing=5
 
{| style="background-color:#f5f5f5" cellspacing=5
!Operator !!Operation !!Operands !!Result
+
!Operador !!Operación !!Operandos !!Resultado
 
|-
 
|-
| + ||Addition or unary positive ||real or integer ||real or integer
+
| + ||Adición o operador unario positivo ||real o integer ||real o integer
 
|-
 
|-
| - ||Subtraction or unary negative ||real or integer ||real or integer
+
| - ||Substración o operador unario negativo  ||real o integer ||real o integer
 
|-
 
|-
| * ||Multiplication ||real or integer ||real or integer
+
| * ||Multiplicación ||real o integer ||real o integer
 
|-
 
|-
| / ||Real division ||real or integer ||real
+
| / ||División real ||real o integer ||real
 
|-
 
|-
|div ||Integer division ||integer ||integer
+
|div ||División con enteros ||integer ||integer
 
|-
 
|-
|mod ||Modulus (remainder division) ||integer ||integer
+
|mod ||Modulo (residuo de una división) ||integer ||integer
 
|}
 
|}
'''<tt>div</tt>''' and '''<tt>mod</tt>''' only work on ''integers''. '''<tt>/</tt>''' works on both ''reals'' and ''integers'' but will always yield a ''real'' answer. The other operations work on both ''reals'' and ''integers''. When mixing ''integers'' and ''reals'', the result will always be a ''real'' since data loss would result otherwise. This is why Pascal uses two different operations for division and integer division. <tt>7 / 2 = 3.5</tt> (real), but <tt>7 div 2 = 3</tt> (and <tt>7 mod 2 = 1</tt> since that's the remainder).
+
'''<tt>div</tt>''' y'''<tt>mod</tt>''' sólo trabaja con ''integers''. '''<tt>/</tt>''' 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 <tt>7 / 2 = 3.5</tt> (real), pero <tt>7 div 2 = 3</tt> (y <tt>7 mod 2 = 1</tt> 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:
  
Each variable can only be assigned a value that is of the same data type. Thus, you cannot assign a real value to an integer variable. However, certain data types will convert to a higher data type. This is most often done when assigning integer values to real variables. Suppose you had this variable declaration section:
 
 
<syntaxhighlight>
 
<syntaxhighlight>
 
var
 
var
Line 41: Line 41:
 
   some_real : real;
 
   some_real : real;
 
</syntaxhighlight>
 
</syntaxhighlight>
When the following block of statements executes,
+
Cuando el siguiente bloque de instrucciones se ejecuta,
 
<syntaxhighlight>
 
<syntaxhighlight>
 
some_int := 375;
 
some_int := 375;
 
some_real := some_int;
 
some_real := some_int;
 
</syntaxhighlight>
 
</syntaxhighlight>
<tt>some_real</tt> will have a value of <tt>375.0</tt>.
+
<tt>some_real</tt> tiene el valor <tt>375.0</tt>.
  
 
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.
 
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.

Revision as of 16:43, 10 August 2012

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