Learn Pascal Programming Tutorial Lesson 3 - Variables and Constants
What are variables?
Variables are names given to blocks of the computer's memory. The names are used to store values in these blocks of memory.Variables can hold values which are either numbers, strings or Boolean. We already know what numbers are. Strings are made up of letters. Boolean variables can have one of two values, either True or False.
Using variables
You must always declare a variable before you use it. We use the var statement to do this. You must also choose what type of variable it is. Here is a table of the different variable types:Byte | 0 to 255 |
Word | 0 to 65535 |
ShortInt | -128 to 127 |
Integer | -32768 to 32767 |
LongInt | -4228250000 to 4228249000 |
Real | floating point values |
Char | 1 character |
String | up to 255 characters |
Boolean | true or false |
program Variables;
var
i: Integer;
begin
end.
To assign a value to a variable we use :=.
program Variables;
var
i: Integer;
begin
i := 5;
end.
You can create 2 or more variables of the same type if you seperate their names with commas. You can also create variables of a different type without the need for another var statemtent.
program Variables;
var
i, j: Integer;
s: String;
begin
end.
When you assign a value to a string variable, you must put it between single quotes. Boolean variables can only be assigned the values True and False.
program Variables;
var
i: Integer;
s: String;
b: Boolean;
begin
i := -3;
s := 'Hello';
b := True;
end.
Calculations with variables
Variables can be used in calculations. For example you could assign the value to a variable and then add the number 1 to it. Here is a table of the operators that can be used:+ | Add |
- | Subtract |
* | Multiply |
/ | Floating Point Divide |
div | Integer Divide |
mod | Remainder of Integer Division |
program Variables;
var
Num1, Num2, Ans: Integer;
begin
Ans := 1 + 1;
Num1 := 5;
Ans := Num1 + 3;
Num2 := 2;
Ans := Num1 - Num2;
Ans := Ans * Num1;
end.
Strings hold characters. Characters include the the letters of the alphabet as well as special characters and even numbers. It is important to understand that integer numbers and string numbers are different things. You can add strings together as well. All that happens is it joins the 2 strings. If you add the strings '1' and '1' you will get '11' and not 2.
program Variables;
var
s: String;
begin
s := '1' + '1';
end.
You can read vales from the keyboard into variables using Readln and ReadKey. ReadKey is from the crt unit and only reads 1 character. You will see that ReadKey works differently to Readln
program Variables;
uses crt;
var
i: Integer;
s: String;
c: Char;
begin
Readln(i);
Readln(s);
c := ReadKey;
end.
Printing variables on the screen is just as easy. If you want to print variables and text with the same Writeln then seperate them with commas.
program Variables;
var
i: Integer;
s: String;
begin
i := 24;
s := 'Hello';
Writeln(i);
Writeln(s,' world');
end.
Constants
Constants are like variables except that their values can't change. You assign a value to a constant when you create it. const is used instead of var when declaring a constant. Constants are used for values that do not change such as the value of pi. program Variables;
const
pi: Real = 3.14;
var
c, d: Real;
begin
d := 5;
c := pi * d;
end.
- Learn Pascal Programming Tutorial Lesson 1 - Introduction to Pascal
- Learn Pascal Programming Tutorial Lesson 2 - Colors, Coordinates, Windows and Sound
- Learn Pascal Programming Tutorial Lesson 3 - Variables and Constants
- Learn Pascal Programming Tutorial Lesson 4 - String Handling and Conversions
- Learn Pascal Programming Tutorial Lesson 5 - Decisions
- Learn Pascal Programming Tutorial Lesson 6 - Loops
- Learn Pascal Programming Tutorial Lesson 7 - Arrays
- Learn Pascal Programming Tutorial Lesson 8 - Types, Records and Sets
- Learn Pascal Programming Tutorial Lesson 9 - Procedures and Functions
- Learn Pascal Programming Tutorial Lesson 10 - Text Files
- Learn Pascal Programming Tutorial Lesson 11 - Data Files
- Learn Pascal Programming Tutorial Lesson 12 - Units
- Learn Pascal Programming Tutorial Lesson 13 - Pointers
- Learn Pascal Programming Tutorial Lesson 14 - Linked Lists
No comments:
Post a Comment