TURBO PASCAL TUTORIAL
- Variable Definitions.
integer - a number that does not have a decimal part. 12 is an integer.
real - a number which has a decimal part. 3.25 would be a real.
string - a section of text. "Hello world!" would be a string.
char - one part of a string. "G" is a character, while "GG" is not.
Example
Var
age : integer;
answer : real;
name : string;
- Assigning Variables
We use the := to assign a value to a variable.
Examples of that would be:
world_stmt := 'Hello world.'; { a definition of a string. The
' s must be there on each side }
choice_char := 'a'; { a definition of a character The
' s must be there on each side }
money := 3.25; { a definition of a real number}
coins := 10; { a definition of an integer }
- Arithmetic computations.
We often have to do arithmetic to program and solve a problem.
x := 3 + 2; { we're telling the computer to add 3 and 2 and then
place 5 in an integer called x.}
y := 10 - 7; { we're telling the computer to subtract 7 from 10
and then place 3 in an integer called y. }
z := 3 * 2; { we're telling the computer to multiply 3 by 2. }
a := 10 / 2; {dividing 10 by 2 }
Any of these can be combined in one statement, with the order of operations
being brackets first, then division, multiplication, subtraction and then +.
For example:
answer := 3 + (2 + 3) * 4; {answer would be equal to 23}
Sample Program
program assign1;
uses WinCrt;
var
number1, number2: integer;
result: integer;
begin
number1 := 3; {assign first number the value of 3}
number2 := number1 * 2; {assign 2nd number-multiply first number by 2}
number1 := number2 - 5; {assign 1st number-old value of 1st number - 5}
result := number1 + number2; {assign result to be 1st number + 2nd number}
end.
Read this to find out what the value of all the variables would be at the end of
the code. The first statement is a simple assign statement,
assigning first number the value of 3. The second statement assigns second
number the value of first number (3) * 2 which is 6. The third statement
then assigns the first number the value of second number (6) - 5 which is
1. And then the final statement assigns result to the addition of first and
second number. So, the values of all the variables would be
first number := 1; second number := 6; and result := 7;.
- Assignment
Modify the program shown above so that when it is run, it explains what
you have done and gives the result in user friendly output