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...")
 
m (bypass language bar/categorization template redirect [cf. discussion])
 
(7 intermediate revisions by 5 users not shown)
Line 1: Line 1:
1E - Assignment and Operations (author: Tao Yue, state: unchanged)
+
{{Basic Pascal Tutorial/Chapter 1/Assignment and Operations}}
  
Once you have declared a variable, you can store values in it. This is called assignment.
+
1E - Asignaciones y operaciones (autor: Tao Yue, state: ''cambiado'')
  
To assign a value to a variable, follow this syntax:
+
   Una vez declaramos una variable, podemos almacenar valores en ella. A esto lo llamamos asignación.
<syntaxhighlight>
 
variable_name := expression;
 
</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.
 
  
The expression can either be a single value:
+
&nbsp;&nbsp;&nbsp;Para asignar un valor a una variable, se sigue la siguiente sintaxis:
<syntaxhighlight>
+
 
some_real := 385.385837;
+
<syntaxhighlight lang="pascal"> variable_name := expresión;</syntaxhighlight>
</syntaxhighlight>
+
 
or it can be an arithmetic sequence:
+
&nbsp;&nbsp;&nbsp;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.
<syntaxhighlight>
+
 
some_real := 37573.5 * 37593 + 385.8 / 367.1;
+
&nbsp;&nbsp;&nbsp;La expresion puede ser un valor simple:
</syntaxhighlight>
+
 
The arithmetic operators in Pascal are:
+
<syntaxhighlight lang="pascal"> un_numero_real := 385.385837;</syntaxhighlight>
 +
 
 +
&nbsp;&nbsp;&nbsp;o puede ser una expresión aritmetica:  
 +
 
 +
<syntaxhighlight lang="pascal"> un_numero_real := 37573.5 * 37593 + 385.8 / 367.1;</syntaxhighlight>
 +
 
 +
&nbsp;&nbsp;&nbsp;Los operadores aritmeticos en Pascal son:
 
{| 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).
+
&nbsp;&nbsp;&nbsp;'''<tt>div</tt>''' y '''<tt>mod</tt>''' sólo trabajan con números enteros (''integer''). '''<tt>/</tt>''' 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 <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).
 +
 
 +
&nbsp;&nbsp;&nbsp;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:
 +
 
 +
<syntaxhighlight lang="pascal">
 +
var
 +
  un_entero : integer;
 +
  un_real : real;</syntaxhighlight>
 +
 
 +
&nbsp;&nbsp;&nbsp;Cuando el siguiente bloque de instrucciones se ejecuta,
 +
 
 +
<syntaxhighlight lang="pascal">
 +
some_int := 375;
 +
un_real := un_entero;</syntaxhighlight>
 +
 
 +
&nbsp;&nbsp;&nbsp;<tt>un_real</tt> tiene el valor <tt>375.0</tt>.
 +
 
 +
&nbsp;&nbsp;&nbsp;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.
  
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:
+
&nbsp;&nbsp;&nbsp;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.
<syntaxhighlight>
 
var
 
  some_int : integer;
 
  some_real : real;
 
</syntaxhighlight>
 
When the following block of statements executes,
 
<syntaxhighlight>
 
some_int := 375;
 
some_real := some_int;
 
</syntaxhighlight>
 
<tt>some_real</tt> will have a value of <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.
+
&nbsp;&nbsp;&nbsp;Do not attempt to use two operators side by side, like in:
  
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.
+
<syntaxhighlight lang="pascal"> un_real := 37.5 * -2;</syntaxhighlight>
  
Do not attempt to use two operators side by side, like in:
+
&nbsp;&nbsp;&nbsp;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:
<syntaxhighlight>
 
some_real := 37.5 * -2;
 
</syntaxhighlight>
 
  
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:
+
<syntaxhighlight lang="pascal"> un_real := 37.5 * (-2);</syntaxhighlight>
<syntaxhighlight>
 
some_real := 37.5 * (-2);
 
</syntaxhighlight>
 
  
The computer follows an order of operations similar to the one that you follow when you do arithmetic. Multiplication and division (<tt>* / div mod</tt>) come before addition and subtraction (<tt>+ -</tt>), and parentheses always take precedence. So, for example, the value of: <tt>3.5*(2+3)</tt> will be <tt>17.5.</tt>
+
&nbsp;&nbsp;&nbsp;The computer follows an order of operations similar to the one that you follow when you do arithmetic. Multiplication and division (<tt>* / div mod</tt>) come before addition and subtraction (<tt>+ -</tt>), and parentheses always take precedence. So, for example, the value of: <tt>3.5*(2+3)</tt> will be <tt>17.5.</tt>
  
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.
+
&nbsp;&nbsp;&nbsp;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.
  
 
{|style=color-backgroud="white" cellspacing="20"
 
{|style=color-backgroud="white" cellspacing="20"
|[[Variables_and_Data_Types|previous]]   
+
|[[Basic Pascal Tutorial/Chapter 1/Variables and Data Types/es|previo]]   
|[[Contents|contents]]  
+
|[[Basic Pascal Tutorial/Contents/es|índice]]  
|[[Standard_Functions|next]]
+
|[[Basic Pascal Tutorial/Chapter 1/Standard Functions/es|siguiente]]
 
|}
 
|}

Latest revision as of 16:17, 20 August 2022

български (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