loops - "single-entry"和 "single-exit"对于语句意味着什么?

标签 loops conditional-statements programming-languages control-structure

来自An Integrated Approach to Software Engineering By Pankaj Jalote

Clearly, no meaningful program can be written as a sequence of simple statements without any branching or repetition (which also involves branching). So, how is the objective of linearizing the control flow to be achieved? By making use of structured constructs. In structured programming, a statement is not a simple assignment statement, it is a structured statement. The key property of a structured statement is that it has a single-entry and a single-exit. That is, during execution, the execution of the (structured) statement starts from one defined point and the execution terminates at one defined point. With single-entry and single-exit statements, we can view a program as a sequence of (structured) statements. And if all statements are structured statements, then during execution, the sequence of execution of these statements will be the same as the sequence in the program text. Hence, by using single-entry and single-exit statements, the correspondence between the static and dynamic structures can be obtained.

The most commonly used single-entry and single-exit statements are

Selection: if B then S1 else S2
if B then S1

Iteration: While B do S
repeat S until B

Sequencing: S1; S2; S3;...

结构化语句中的“单次进入”和“单次退出”是什么意思?

为什么最后列出的语句是单进单出? 例如,在if B then S1 else S2中,为什么它是单一退出,因为它可以终止于 S1S2

你能给出一个非单一条目的声明吗?

你能给出一个非单一退出的声明吗?

最佳答案

在许多语言中,唯一没有单个条目的语句是那些碰巧包含与位于其外部的 gotoswitch 语句一起使用的标签的语句,并且唯一没有单一退出的语句是那些包含指向外部位置、触发异常或以其他方式强制堆栈展开的 goto 的语句。请注意,对于函数的任何特定调用,唯一的“正常”退出点将是紧随该调用之后的代码。

对于那些从未使用过不使用这种方法的代码的人来说,单次进入/单次退出的概念可能不清楚。后者的例子可以在为 Atari 2600 等平台编写代码时找到,其中 RAM 空间通常非常宝贵。如果从显示标题屏幕的代码或从游戏逻辑内部调用一段代码,并且无法承担子程序调用指令所需的两个字节的堆栈空间,那么这种情况并不少见跳转到代码(而不是使用“JSR”[跳转到子例程]指令),并通过检查游戏是否正在进行并跳回“显示标题屏幕”或“执行游戏逻辑”代码。如果需要从代码中的更多位置调用这种设计,维护起来可能会很困难,但如果 R​​AM 非常紧张(例如,RAM 总共只有 128 字节,如 Atari 2600),则可能需要这种技术。

关于loops - "single-entry"和 "single-exit"对于语句意味着什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56842583/

相关文章:

loops - Cypress.io 迭代元素列表以查看文本是否不存在,然后执行操作以添加元素

javascript - 提交按钮按条件在函数中向上 float

php - 我在哪里可以学习网络编程从入门到精通?

r - 当一个值大于循环内的另一个值时停止循环函数

python - 对于列表 : del n 中的 n

SQL 存储查询 - 根据记录的存在使用查询结果作为 bool 值

multithreading - 如何在一个类中创建线程?

objective-c - 混合过程和面向对象的编程

python - 找到该幂函数的循环不变量

mysql - 基于子查询的SQL查询。检索数据>阈值的交易