Last updated on January 1st, 2024 at 11:33 pm
As with any other programming language, Pascal variables are meant to hold data of different types that are expected to change while the program is being run. The types of variables used in Pascal programs are char, string, integer, real and boolean. To initialize a variable, we write the variable name followed by a colon and the variable type like this: age : integer or myName : string. Variable declarations limit the type of data that can be stored in the memory location.
Pascal is not case sensitive, so num1 and NUM1 or myName and myname are interpreted as the same thing.
In Pascal, the naming convention allows the use of letters, numbers, and underscores for variable names. When programming, initializing variables when they are first created is a good habit to develop. This is because when this is not done, a computer often assigns random values to them. To avoid unpredictable results, variables are assigned values at the time of creation.
Our discussion on variables would be incomplete without exploring arrays. Arrays allow us to extend the way variables work when we work with repetitions. To look at what arrays are and how to use them, visit how to declare and use arrays in Pascal.
Before you begin to program, it is important to learn the steps in implementing a program. Additionally, if you are new to Pascal programming and would like to learn the basic syntax of the Pascal programming language, visit lesson one of our Pascal tutorials.
Pascal variable types and their descriptions
Each variable type indicates the size of memory the data will occupy. Here are the descriptions of each variable type:
- Character (char) – Is a variable type that holds a single character, e.g., 1, e, *, etc.
- Integer – These are whole numbers, e.g. -2, -1, 0, 1, 2.
- Real – These are floating-point numbers or numbers that contain decimal places, e.g., 1.2, 1.4, etc.
- String – String variables are a combination of characters, such as names e.g.: Prince Donahue III or Asha.
- Bolean – These variables contain logic values, true or false.
If you are interested in learning more about variables and how they differ from consonants, visit our article on variables vs. consonants. Variables typically are not used in isolation. So you should visit our article on Pascal programming operators to learn about the operators used to control how variables are manipulated.
How to declare and initialize Pascal variables
The following program was written to show how to demonstrate how to initialize and use five different variable types. See the blue highlights. The first part demonstrates variable declarations, while the second show the initialization.
Program variables; var grade : char; myName: string; totalScore : integer; averageGrade : real; begin grade := 'A'; myName := 'Odelle'; totalScore := 145; averageGrade := 0; averageGrade := totalScore/4; writeln('My name is ', myName, '. I am an ', grade, ' grade student.'); writeln('My total score is ', totalScore, '.'); writeln('My term average is ', averageGrade:2:2, '.'); end.
If you are interested in learning to program like an expert, be sure to visit our training on pseudocode algorithms, flowcharts, and trace tables.
Before you go
We try our best to be as thorough as possible in the information we provide. However, if you have any questions or comments, be sure to leave them in the section provided below.