当前位置:网站首页>[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 .
边栏推荐
猜你喜欢
5. Practice: jctree implements the annotation processor at compile time
Yolov5 code reproduction and server operation
[salesforce] how to confirm your salesforce version?
2022 年辽宁省大学生数学建模A、B、C题(相关论文及模型程序代码网盘下载)
2022 college students in Liaoning Province mathematical modeling a, B, C questions (related papers and model program code online disk download)
Finally, I understand the event loop, synchronous / asynchronous, micro task / macro task, and operation mechanism in JS (with test questions attached)
已知兩種遍曆序列構造二叉樹
自定义异常
Leetcode skimming -- incremental ternary subsequence 334 medium
Custom exception
随机推荐
/bin/ld: 找不到 -lxml2
Storage read-write speed and network measurement based on rz/g2l | ok-g2ld-c development board
beforeEach
(Video + graphic) machine learning introduction series - Chapter 5 machine learning practice
4. Jctree related knowledge learning
PTA 天梯赛习题集 L2-001 城市间紧急救援
6095. 强密码检验器 II
【LeetCode】876-链表的中间结点
[leetcode] 189 rotation array
6096. Success logarithm of spells and potions
folium地图无法显示的问题,临时性解决方案如下
[leetcode] 977 square of ordered array
【LeetCode】977-有序数组的平方
目标检测—利用labelimg制作自己的深度学习目标检测数据集
/bin/ld: 找不到 -lssl
Redux——详解
数字藏品系统开发(程序开发)丨数字藏品3D建模经济模式系统开发源码
Engineer evaluation | rk3568 development board hands-on test
[leetcode] 19 delete the penultimate node of the linked list
动态规划入门二(5.647.62)