Programming Concepts: Variables, Types and Control Structures | 编程概念:变量、类型与控制结构

📚 Programming Concepts: Variables, Types and Control Structures | 编程概念:变量、类型与控制结构

At the heart of every computer program lies the same simple idea: data must be stored, understood and processed in a controlled order. Variables give data a name, data types tell the computer what kind of data is being stored, and control structures decide the order in which statements are executed. Together they form the foundation of all programming.

每一个计算机程序的核心都指向一个简单的思想:数据需要被存储、被理解,并以受控的顺序被处理。变量为数据提供名称,数据类型告诉计算机存储的是什么数据,而控制结构决定语句执行的顺序。它们共同构成了所有编程的基础。


1. What is a Variable? | 什么是变量?

A variable is a named storage location in memory. Its value can change while a program is running. For example, a variable called score might start at 0, then increase as the player earns points. The computer does not work directly with a word like “score”; instead, it uses the memory address that the name represents.

变量是内存中的一个命名存储位置。它的值可以在程序运行期间改变。例如,名为 score 的变量可能从 0 开始,然后随着玩家得分而增加。计算机并不是直接操作 “score” 这个词,而是通过这个名称所对应的内存地址来工作。

A useful way to imagine a variable is as a labelled box. The label is the variable name, and the box contains the current value. You can replace the contents of the box, but the label stays the same.

一个理解变量的好方法,是把它想象成一个带有标签的盒子。标签就是变量名,盒子里装着当前的值。你可以更换盒子里的内容,但标签保持不变。

score ← 0
score ← score + 10

In the assignment above, the right-hand side is calculated first. So score + 10 is evaluated as 0 + 10, and then 10 is stored back into score. Assignment is not the same as equality in mathematics; it is an instruction to place a value into a variable.

在上面的赋值语句中,先计算右侧的表达式。因此 score + 10 会先被计算为 0 + 10,然后将 10 存回 score。赋值与数学中的相等关系不同;赋值是一条“把值放入变量”的指令。


2. Data Types | 数据类型

A data type defines what kind of values a variable can hold. This is important because different types support different operations. For example, you can divide two numbers, but you cannot meaningfully divide two first names.

数据类型定义了变量可以保存什么类型的值。这很重要,因为不同类型的值支持不同的操作。例如,你可以对两个数字进行除法,但不能对两个名字进行除法。

In CIE pseudocode, the common data types are INTEGER, REAL, CHAR, STRING and BOOLEAN.

在 CIE 伪代码中,常见数据类型包括 INTEGER、REAL、CHAR、STRING 和 BOOLEAN。

Data Type / 数据类型 Meaning / 含义 Example / 示例
INTEGER / 整数 Whole numbers, positive or negative / 正整数、负整数和零 age ← 16
REAL / 实数 Numbers containing a fractional part / 带小数的数字 pi ← 3.142
CHAR / 字符 A single character / 单个字符 grade ← ‘A’
STRING / 字符串 A sequence of characters / 字符序列 name ← “Ada”
BOOLEAN / 布尔 Only TRUE or FALSE / 只有真或假 flag ← TRUE

A CHAR value is stored in single quotes and occupies a fixed, small amount of space. A STRING value is stored in double quotes and can contain any number of characters. BOOLEAN variables are often used to represent conditions such as found ← FALSE or finished ← TRUE.

CHAR 值用单引号表示,占用固定且较小的空间。STRING 值用双引号表示,可以包含任意数量的字符。BOOLEAN 变量通常用于表示条件,例如 found ← FALSE 或 finished ← TRUE。


3. Declaring and Initialising Variables | 声明与初始化变量

Before a variable can be used in CIE pseudocode, it should be declared. Declaration creates the variable and states its data type. It tells the computer to reserve space in memory and gives that space a name.

在 CIE 伪代码中,变量在使用前应当先声明。声明会创建变量并说明其数据类型。它告诉计算机在内存中预留空间,并为这个空间命名。

DECLARE age : INTEGER
age ← 16
OUTPUT age

In the example above, the first line declares age as an INTEGER. The second line assigns 16 to age. This is known as initialisation because it gives the variable its first value. The third line outputs the current value of age.

在上面的例子中,第一行将 age 声明为 INTEGER。第二行将 16 赋给 age。这称为初始化,因为它给了变量第一个值。第三行输出 age 的当前值。

Initialising a variable is good practice. If a variable is used before it has been assigned a value, its contents may be unpredictable. In examinations, always make sure important variables are given a sensible starting value.

初始化变量是一种良好的编程习惯。如果变量在赋值之前就被使用,它的内容可能无法预测。在考试中,一定要确保重要的变量有一个合理的初始值。

  • Choose meaningful names: Use totalMarks rather than x. A meaningful identifier makes code easier to read.
  • 选择有意义的名称: 使用 totalMarks 而不是 x。有意义的标识符能让代码更容易阅读。
  • Identifier rules: Names may contain letters, digits and underscores, but they must not begin with a digit and must not contain spaces.
  • 标识符规则: 名称可以包含字母、数字和下划线,但不能以数字开头,也不能包含空格。

4. Constants and Variable Scope | 常量与变量作用域

A constant is a name that stores a value which cannot be changed during the execution of a program. In CIE pseudocode, constants are declared with CONSTANT rather than DECLARE.

常量是一种名称,它存储的值在程序执行期间不能被改变。在 CIE 伪代码中,常量使用 CONSTANT 而不是 DECLARE 来声明。

CONSTANT MaxMarks = 100

After this line, MaxMarks always represents 100. If the program tries to change MaxMarks, an error occurs. Constants are useful because they make code more readable and prevent accidental modification of important values.

执行这行之后,MaxMarks 始终表示 100。如果程序试图修改 MaxMarks,就会发生错误。常量非常有用,因为它们使代码更易读,并防止重要值被意外修改。

Variables also have a scope, which is the part of the program where they are visible. A global variable can be used anywhere in the program, while a local variable can only be used inside the procedure,

Published by TutorHao | Computer Science Revision Series | aleveler.com

更多咨询请联系16621398022(同微信)

Comments

屏轩国际教育cambridge primary/secondary checkpoint, cat4, ukiset,ukcat,igcse,alevel,PAT,STEP,MAT, ibdp,ap,ssat,sat,sat2课程辅导,国外大学本科硕士研究生博士课程论文辅导

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Discover more from aleveler.com

Subscribe now to keep reading and get access to the full archive.

Continue reading