Variables and their declaration in Pascal

If youre new to Pascal, then take a look at the variables and declaration methods below to gain more useful knowledge, helping you understand and use Pascal easily and effectively.

Variables are values ​​that can be changed within a program. Each variable in Pascal has a specific data type, which defines its size and memory layout, the range of values ​​that can be stored in that memory, and the operators that can be applied to the variable.

Variable names can include letters, numbers, and underscores, and must begin with a letter or an underscore. Pascal is not case-sensitive, so using uppercase or lowercase makes no difference.

How to declare variables in Pascal

Pascal has many different functions; mastering them all is not something that can be done quickly, but requires considerable study. Many Pascal functions are simple, while others are complex, much like variables.
 

Article contents:
1. Basic variables in Pascal.
2. Declaring variables in Pascal.
3. Initializing variable values ​​in Pascal.


Variables and their declaration in Pascal


1. Basic variables in Pascal

The Pascal programming language also allows you to define different variable types. However, in this article, TipsMake will only introduce you to the basic variables.


2. Declaring variables in Pascal

All variables must be declared before using a Pascal program. All variable declarations follow the prefix `var` . A declaration specifies a list of variables, followed by a colon (:) and the variable type. The syntax for declaring variables is:

var

variable_list : type;

In this case, `type` must be a valid Pascal variable type, including characters, integers, real numbers, booleans, or any user-specified data type, etc. And `variable_list` can include one or more identifiers separated by commas.

Here are some valid variable declarations:

var
age, weekdays : integer;
taxrate, net_income: real;
choice, isready: boolean;
initials, grade: char;
name, surname : string;

In previous articles, TipsMake mentioned that Pascal allows type declaration. The declared type can be defined by name or identifier. Additionally, type declarations can be used to define variable types.

For example:

type
days, age = integer;
yes, true = boolean;
name, city = string;
fees, expenses = real;

Type declarations can be used in variable declarations.

var
weekdays, holidays : days;
choice: yes;
student_name, emp_name : name;
capital: city;
cost: expenses;

Note that there is a difference between type declaration and variable declaration. Type declarations specify types such as integer, real, etc. Variable declarations, on the other hand, specify the values ​​that a variable can represent.

You can compare type declarations in Pascal with typedefs in C. The most important difference is that variable names refer to the memory location where the variable's value will be stored, whereas type declarations do not.


3. Initializing variable values ​​in Pascal

Variables are assigned values ​​using a colon (:) and an equals sign (=), followed by a constant expression. The general formula for assigning a value is:

variable_name := value;

By default, variables in Pascal are not initialized to zero and may contain garbage values. Therefore, it is better to initialize variables within the program.

Variables can be initialized (assigned initial values) in the variable declaration. Variable initialization follows the word `var` and the initialization syntax is as follows:

var
variable_name : type = value;

Some examples include:

age: integer = 15;
taxrate: real = 0.5;
grade: char = 'A';
name: string = 'John Smith';

Below is an example of a complete program using variables and Pascal's declaration method:

program Greetings;
const
message = ' Welcome to the world of Pascal ';
type
name = string;
var
firstname, surname: name;
begin
writeln('Please enter your first name: ');
readln(firstname);
writeln('Please enter your surname: ');
readln(surname);
writeln;
writeln(message, ' ', firstname, ' ', surname);
end.

After being compiled and executed, the above code will produce the following result:

Please enter your first name:
John
Please enter your surname:
Smith
Welcome to the world of Pascal John Smith

Enumerated variables

You've seen how to use simple variable types like Integer, Real, and Boolean. Enumerated variables can be declared as follows:

var
var1, var2, . : enum-identifier;

When declaring enumeration variables, you can use type declarations. For example:

type
months = (January, February, March, April, May, June, July, August, September, October, November, December);
Var
m: months;
.
M := January;
The following example illustrates the concept −
program exEnumeration;
type
beverage = (coffee, tea, milk, water, coke, limejuice);
var
drink:beverage;
begin
writeln('Which drink do you want?');
drink := limejuice;
writeln('You can drink ', drink);
end.

After being compiled and executed, the above code will produce the following result:

Which drink do you want?
You can drink limejuice

Subdomain variables

Subdomain variables are declared as follows:

var
subrange-name : lowerlim . upperlim;

Examples of declaring subdomain variables:

var
marks: 1 . 100;
grade: 'A' . 'E';
age: 1 . 25;

The specific program uses variables of subdomain type:

program exSubrange;
var
marks: 1 . 100;
grade: 'A' . 'E';
begin
writeln( 'Enter your marks(1 - 100): ');
readln(marks);
writeln( 'Enter your grade(A - E): ');
readln(grade);
writeln('Marks: ' , marks, ' Grade: ', grade);
end.

After being compiled and executed, the above code will produce the following result:

Enter your marks(1 - 100):
100
Enter your grade(A - E):
A
Marks: 100 Grade: A


In summary, a variable is a value that can change within a program. Each variable in Pascal has a specific data type. Above, TipsMake has introduced you to variables and how to declare them in Pascal, so you can grasp how to write functions in Pascal more easily and quickly become proficient. If you have any questions or need clarification, you can leave your comments in the section below the article.

Other Program articles
  • How to use Pascal Online, learn programming online.

    nếu bạn đang cảm thấy khó khăn trong việc cài đặt môi trường và phần mềm để lập trình pascal, hoặc bạn chỉ cần chạy thử một đoạn code nhỏ, không muốn cài quá nhiều thứ vào máy của mình. taimienphi có một cách sử dụng pascal online trực tiếp trên website, không cần cài đặt.
  • Designing a Chrome logo using CorelDraw

    coreldraw là phần mềm thiết kế chuyên nghiệp dành cho những người làm việc và học tập với bộ môn đồ họa, nơi bạn có thể thỏa sức thiết kế ra những tác phẩm độc đáo nhất. và hướng dẫn thiết kế logo chrome bằng coreldraw trong bài viết này chính là một trong những thứ coreldraw làm dễ dàng.
  • Getting familiar with the Pick Tool in CorelDraw

    pick tool là công cụ quan trọng nhất trong coreldraw. công cụ này được sử dụng để chọn đối tượng. cùng tham khảo tiếp bài viết dưới đây của tipsmake để làm quen với pick tool trong coreldraw.
  • What's new in CorelDRAW Graphics Suite 2018?

    coreldraw graphics suite 2018 là phiên bản phần mềm thiết kế đồ họa nổi tiếng mới nhất hãng corel, với các tính năng nổi bật hứa hẹn sẽ giúp các chuyên gia đồ họa biến ý tưởng sáng tạo của mình thành các thiết kế trực quan và chuyên nghiệp. Để tìm hiểu coreldraw graphics suite 2018 có gì mới? bạn đọc cùng tham khảo bài viết dưới đây của tipsmake.
  • Install and use Corel, design professional images with CorelDraw.

    phần mềm thiết kế đồ họa coreldraw cho phép người dùng thực hiện những công việc như thêm các hiệu ứng đặc biệt cho đường viền hay hình ảnh, ngoài ra còn cho phép điều chỉnh độ tương phản, cân bằng màu sắc và hơn thế nữa. cách cài đặt và sử dụng coreldraw cũng khá đơn giản phù hợp với nhiều đối tượng người dùng.
  • Adobe Illustrator keyboard shortcuts for faster Illustrator operation.

    phím tắt là một phần không thể thiếu cho các phần mềm, giúp bạn sử dụng các thao tác nhanh hơn. và với các phím tắt illustrator thì việc thao tác thiết kế ảnh sẽ được rút ngắn thời gian lại.
Category

System

Windows XP

Windows Server 2012

Windows 8

Windows 7

Windows 10

Wifi tips

Virus Removal - Spyware

Speed ​​up the computer

Server

Security solution

Mail Server

LAN - WAN

Ghost - Install Win

Fix computer error

Configure Router Switch

Computer wallpaper

Computer security

Mac OS X

Mac OS System software

Mac OS Security

Mac OS Office application

Mac OS Email Management

Mac OS Data - File

Mac hardware

Hardware

USB - Flash Drive

Speaker headset

Printer

PC hardware

Network equipment

Laptop hardware

Computer components

Advice Computer

Game

PC game

Online game

Mobile Game

Pokemon GO

information

Technology story

Technology comments

Quiz technology

New technology

British talent technology

Attack the network

Artificial intelligence

Technology

Smart watches

Raspberry Pi

Linux

Camera

Basic knowledge

Banking services

SEO tips

Science

Strange story

Space Science

Scientific invention

Science Story

Science photo

Science and technology

Medicine

Health Care

Fun science

Environment

Discover science

Discover nature

Archeology

Life

Travel Experience

Tips

Raise up child

Make up

Life skills

Home Care

Entertainment

DIY Handmade

Cuisine

Christmas

Application

Web Email

Website - Blog

Web browser

Support Download - Upload

Software conversion

Social Network

Simulator software

Online payment

Office information

Music Software

Map and Positioning

Installation - Uninstall

Graphic design

Free - Discount

Email reader

Edit video

Edit photo

Compress and Decompress

Chat, Text, Call

Archive - Share

Electric

Water heater

Washing machine

Television

Machine tool

Fridge

Fans

Air conditioning

Program

Unix and Linux

SQL Server

SQL

Python

Programming C

PHP

NodeJS

MongoDB

jQuery

JavaScript

HTTP

HTML

Git

Database

Data structure and algorithm

CSS and CSS3

C ++

C #

AngularJS

Mobile

Wallpapers and Ringtones

Tricks application

Take and process photos

Storage - Sync

Security and Virus Removal

Personalized

Online Social Network

Map

Manage and edit Video

Data

Chat - Call - Text

Browser and Add-on

Basic setup