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

From Free Pascal wiki
Jump to navigationJump to search
m (bypass language bar/categorization template redirect [cf. discussion])
 
(7 intermediate revisions by 5 users not shown)
Line 1: Line 1:
 +
{{Basic Pascal Tutorial/Chapter 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:
<syntaxhighlight>
+
 
 +
<syntaxhighlight lang="pascal">
 
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 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:
<syntaxhighlight>
+
 
 +
<syntaxhighlight lang="pascal">
 
some_real := 385.385837;
 
some_real := 385.385837;
 
</syntaxhighlight>
 
</syntaxhighlight>
 +
 
or it can be an arithmetic sequence:
 
or it can be an arithmetic sequence:
<syntaxhighlight>
+
 
 +
<syntaxhighlight lang="pascal">
 
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:
 
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 36: Line 46:
  
 
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:
<syntaxhighlight>
+
 
 +
<syntaxhighlight lang="pascal">
 
var
 
var
 
   some_int : integer;
 
   some_int : integer;
 
   some_real : real;
 
   some_real : real;
 
</syntaxhighlight>
 
</syntaxhighlight>
When the following block of statements executes,
+
 
<syntaxhighlight>
+
When the following block of statements executes:
 +
 
 +
<syntaxhighlight lang="pascal">
 
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> 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:
<syntaxhighlight>
+
 
 +
<syntaxhighlight lang="pascal">
 
some_real := 37.5 * -2;
 
some_real := 37.5 * -2;
 
</syntaxhighlight>
 
</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:
<syntaxhighlight>
+
 
 +
<syntaxhighlight lang="pascal">
 
some_real := 37.5 * (-2);
 
some_real := 37.5 * (-2);
 
</syntaxhighlight>
 
</syntaxhighlight>
Line 66: 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]]
 
|}
 
 
 
[[Category: Object Pascal Introduction]]
 

Latest revision as of 15:17, 20 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.

 ◄   ▲   ►