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

From Lazarus wiki
Jump to navigationJump to search
(New page: 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 value to a variab...)
 
(bypass redirects [cf. discussion])
 
(9 intermediate revisions by 8 users not shown)
Line 1: Line 1:
 +
{{Assignment_and_Operations}}
 +
{{TYNavigator|Chapter 1/Variables and Data Types|Chapter 1/Standard Functions}}
 +
 
1E - Assignment and Operations (author: Tao Yue, state: unchanged)
 
1E - Assignment and Operations (author: Tao Yue, state: unchanged)
  
Line 4: Line 7:
  
 
To assign a value to a variable, follow this syntax:
 
To assign a value to a variable, follow this syntax:
variable_name <font color="#000000"><strong>:=</strong></font> expression<font color="#000000"><strong>;</strong></font>
+
 
 +
<syntaxhighlight lang="pascal">
 +
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.
 
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:
 
The expression can either be a single value:
some_real <font color="#000000"><strong>:=</strong></font> <font color="#ff0000">385</font><font color="#000000"><strong>.</strong></font><font color="#ff0000">385837</font><font color="#000000"><strong>;</strong></font>
+
 
 +
<syntaxhighlight lang="pascal">
 +
some_real := 385.385837;
 +
</syntaxhighlight>
  
 
or it can be an arithmetic sequence:
 
or it can be an arithmetic sequence:
some_real <font color="#000000"><strong>:=</strong></font> <font color="#ff0000">37573</font><font color="#000000"><strong>.</strong></font><font color="#ff0000">5</font> <font color="#000000"><strong>*</strong></font> <font color="#ff0000">37593</font> <font color="#000000"><strong>+</strong></font> <font color="#ff0000">385</font><font color="#000000"><strong>.</strong></font><font color="#ff0000">8</font> <font color="#000000"><strong>/</strong></font> <font color="#ff0000">367</font><font color="#000000"><strong>.</strong></font><font color="#ff0000">1</font><font color="#000000"><strong>;</strong></font>
+
 
 +
<syntaxhighlight lang="pascal">
 +
some_real := 37573.5 * 37593 + 385.8 / 367.1;
 +
</syntaxhighlight>
  
 
The arithmetic operators in Pascal are:
 
The arithmetic operators in Pascal are:
 +
 
{| style="background-color:#f5f5f5" cellspacing=5
 
{| style="background-color:#f5f5f5" cellspacing=5
 
!Operator !!Operation !!Operands !!Result
 
!Operator !!Operation !!Operands !!Result
Line 34: Line 47:
 
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:
 
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:
  
<font color="#006699"><strong>var</strong></font>
+
<syntaxhighlight lang="pascal">
  some_int <font color="#000000"><strong>:</strong></font> <font color="#0099ff"><strong>integer</strong></font><font color="#000000"><strong>;</strong></font>
+
var
  some_real <font color="#000000"><strong>:</strong></font> <font color="#0099ff"><strong>real</strong></font><font color="#000000"><strong>;</strong></font>
+
  some_int : integer;
 +
  some_real : real;
 +
</syntaxhighlight>
  
When the following block of statements executes,
+
When the following block of statements executes:
some_int := <font color="#ff0000">375</font>;
+
 
some_real := some_int;
+
<syntaxhighlight lang="pascal">
 +
some_int := 375;
 +
some_real := some_int;
 +
</syntaxhighlight>
  
 
<tt>some_real</tt> will have a value of <tt>375.0</tt>.
 
<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.
+
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 [http://www.gnu-pascal.de/gpc-es/Type-Casts.html 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.
 
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:
 
Do not attempt to use two operators side by side, like in:
some_real := <font color="#ff0000">37.5</font> * <font color="#ff0000">-2</font>;
+
 
 +
<syntaxhighlight lang="pascal">
 +
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:
 
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 := <font color="#ff0000">37.5</font> * (<font color="#ff0000">-2</font>);
+
 
 +
<syntaxhighlight lang="pascal">
 +
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>
 
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>
Line 58: Line 82:
 
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.
 
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"
+
{{TYNavigator|Chapter 1/Variables and Data Types|Chapter 1/Standard Functions}}
|[[Variables_and_Data_Types|previous]] 
 
|[[Contents|contents]]
 
|[[Standard_Functions|next]]
 
|}
 

Latest revision as of 02:09, 6 August 2022

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

 ◄   ▲   ► 

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 value to a variable, follow this syntax:

variable_name := expression;

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:

some_real := 385.385837;

or it can be an arithmetic sequence:

some_real := 37573.5 * 37593 + 385.8 / 367.1;

The arithmetic operators in Pascal are:

Operator Operation Operands Result
+ Addition or unary positive real or integer real or integer
- Subtraction or unary negative real or integer real or integer
* Multiplication real or integer real or integer
/ Real division real or integer real
div Integer division integer integer
mod Modulus (remainder division) integer integer

div and mod only work on integers. / 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. 7 / 2 = 3.5 (real), but 7 div 2 = 3 (and 7 mod 2 = 1 since that's the remainder).

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:

var
  some_int : integer;
  some_real : real;

When the following block of statements executes:

some_int := 375;
some_real := some_int;

some_real will have a value of 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.

 ◄   ▲   ►