来自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
中,为什么它是单一退出,因为它可以终止于 S1
或S2
?
你能给出一个非单一条目的声明吗?
你能给出一个非单一退出的声明吗?
最佳答案
在许多语言中,唯一没有单个条目的语句是那些碰巧包含与位于其外部的 goto
或 switch
语句一起使用的标签的语句,并且唯一没有单一退出的语句是那些包含指向外部位置、触发异常或以其他方式强制堆栈展开的 goto
的语句。请注意,对于函数的任何特定调用,唯一的“正常”退出点将是紧随该调用之后的代码。
对于那些从未使用过不使用这种方法的代码的人来说,单次进入/单次退出的概念可能不清楚。后者的例子可以在为 Atari 2600 等平台编写代码时找到,其中 RAM 空间通常非常宝贵。如果从显示标题屏幕的代码或从游戏逻辑内部调用一段代码,并且无法承担子程序调用指令所需的两个字节的堆栈空间,那么这种情况并不少见跳转到代码(而不是使用“JSR”[跳转到子例程]指令),并通过检查游戏是否正在进行并跳回“显示标题屏幕”或“执行游戏逻辑”代码。如果需要从代码中的更多位置调用这种设计,维护起来可能会很困难,但如果 RAM 非常紧张(例如,RAM 总共只有 128 字节,如 Atari 2600),则可能需要这种技术。
关于loops - "single-entry"和 "single-exit"对于语句意味着什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56842583/