【发布时间】:2019-04-08 00:53:40
【问题描述】:
我必须将嵌套 if..else 条件转换为 C 代码中的 goto 标签。 我知道我必须从外部 if-s 开始,但是如何用 goto 翻译内部 if-s?
Example: if(condition)
{
if(condition)
{
if(condition)
{
statements;
}
if(condition) return;
statements;
}
}
else statements;
【问题讨论】:
-
创建与原始程序中每个块相对应的代码序列。给它们贴上标签。插入
goto语句以连接它们。只需要初步推理。 -
if (X) { statements A } else { statements B }可以改写为if (X) goto A; { statements B } goto E; A: { statements A } E: ;通过替换(就像在高中代数中一样),您可以从中得到答案。一次只替换一个 if-then-else。 -
为什么?它会让事情变得一团糟。
-
使用 go to 是不合逻辑的。但是,您需要以某种方式比较或检查这些值。您可以使用“?”,然后将行标记为“A:”,然后使用 goto A
-
如果你想在非常低的级别创建 if else 链,你必须使用程序集
标签: c if-statement goto