当前位置:网站首页>[2. Basics of Delphi grammar] 3 Object Pascal constants and variables
[2. Basics of Delphi grammar] 3 Object Pascal constants and variables
2022-07-02 15:42:00 【Janeb1018】
3.Object Pascal Constants and variables
3.1 Constant
stay Object Pascal in , A constant is an amount whose value cannot be changed during the execution of a program , Have certain data types . The representation of constants :
Direct constants : Constant values
declare constant : Denote with a declared identifier , Include : Symbolic constant 、 Type constant
3.1.1 Direct constants
Direct constants are constants used directly in programs .
integer constants
Such as :10、-3、0、1、2 etc.
Real constant
Fixed point real number , Number in decimal form , Such as :0.06、3.14、-2.58、0.0 etc.
Floating point real , A number expressed in scientific notation , Such as :1.234E-4 Express 1.234 ride 10 Negative 4 Power ;, -3.546E12 Express -3.546 ride 10 Of 12 Power .
Character type constants
A single character enclosed in single quotes . Such as :'d'、'E'、'2'、'=' etc. .
String constant
A string enclosed in single quotation marks . Such as :'Hello world.'、' Hello '、'3.1415926' etc. .
Boolean constants
True and False
3.1.2 declare constant
1. Symbolic constant
Declare the constants represented by the defined identifier in advance , Also known as pure constants . Define the syntax of symbolic constants :
Const
< Constant names 1> = < Constant values 1>;
< Constant names 2> = < Constant values 2>;
......
< Constant names n> = < Constant values n>;Const The keyword is used to declare constants .
Object Pascal Judge the type of constant according to the value of constant . Once a constant is declared , Can be quoted .
Code example :
Const
PI = 3.1415926;
Msg = 'Out of memory';
ErrMsg = 'Error:' + Msg + '.';2. Type constant
Type constants cannot appear in constant expressions . Syntax for declaring type constants :
Const
< Type constant name >:< type > = < Constant values >;Code example :
Const
PI:real = 3.1415926;
EnterKey:char = #13;
Heading:String = 'Hello world';3.2 Variable
A variable is an amount that can change its value when a program is running , In order to represent and reference variables, variables must be named with identifiers , And call this identifier variable name . All variables must follow the rule of declaration before reference .
1. Declaration of variables
stay Object Pascal in , Variables in unit 、 The declaration part of a function or procedure , The location of a declared variable determines its scope . Syntax format of variable declaration :
Var
< Variable name 1>: < Type name 1>
< Variable name 2>: < Type name 2>
......
< Variable name n>: < Type name n>When multiple variables have the same type , Compact formats can also be used :
Var < Variable name 1>, < Variable name 2>, ......, < Variable name n>: < Type name >
among ,Var Keyword is used to declare variables , When declaring variables in a compact format , Variable names are separated by commas .
Code example :
Var
a,b,c: Integer;
x,y,z: Real;
ch: Char;
city: String;When variables are declared , Except to conform to Object Pascal Beyond the language , Try to use meaningful names that are easy to remember and read as variable names .
2. Use of variables
Once declared, variables can be used in programs , Generally, you can assign values to variables through assignment statements , Then use variables in expressions .
Object Pascal It's a compiled language , The program can only run after compilation . When the program is compiled , The compiler will allocate memory space to variables according to their types , But the compiler does not automatically initialize variables , The value of an uninitialized variable is an uncertain random value , therefore , After variable declaration , It should be initialized in time .
The easiest way to initialize variables is to assign values to variables .
边栏推荐
猜你喜欢

Leetcode skimming -- verifying the preorder serialization of binary tree # 331 # medium

2022 年辽宁省大学生数学建模A、B、C题(相关论文及模型程序代码网盘下载)

动态规划入门二(5.647.62)

PostgresSQL 流复制 主备切换 主库无读写宕机场景

Soul torture, what is AQS???

Pytoch saves tensor to Mat file

Equipped with Ti am62x processor, Feiling fet6254-c core board is launched!

已知兩種遍曆序列構造二叉樹

Storage read-write speed and network measurement based on rz/g2l | ok-g2ld-c development board

How to intercept the value of a key from the JSON string returned by wechat?
随机推荐
[leetcode] 1140 stone game II
[leetcode] 167 - sum of two numbers II - enter an ordered array
Leetcode question brushing - parity linked list 328 medium
解决BASE64Encoder报错的问题
Bing.com網站
Steps for Navicat to create a new database
MD5加密
[leetcode] 19 delete the penultimate node of the linked list
【LeetCode】417-太平洋大西洋水流问题
The task cannot be submitted after the installation of flick is completed
[experience cloud] how to get the metadata of experience cloud in vscode
PTA ladder game exercise set l2-001 inter city emergency rescue
6092. 替换数组中的元素
College entrance examination admission score line climbing
lseek 出错
2303. Calculate the total tax payable
[leetcode] 189 rotation array
Leetcode skimming -- sum of two integers 371 medium
【idea】推荐一个idea翻译插件:Translation「建议收藏」
高考分数线爬取