如何在 COBOL 中使用 GO TO

发布于 2024-11-05 12:54:04 字数 755 浏览 5 评论 0原文

我的 COBOL 程序之一中有以下代码片段。

IF  FIRST < SECOND
   MOVE FIRST TO WS
END-IF.
MOVE SECOND TO WS.
MOVE WS TO RESULT.

我需要在 IF 块内使用 GO TO 跳转到最后一条语句 (MOVE WS TO RESULT)。

IF  FIRST < SECOND
   MOVE FIRST TO WS
   GO TO <last line.(MOVE WS to RESULT)>
END-IF.
MOVE SECOND TO WS.
MOVE WS TO RESULT.

换句话说,我需要跳过“第二个移动到 WS。”。
在 cobol 中跳转到特定行的最简单方法是什么? 我在某处读到,通过定义段落可以实现这一点,但不知道如何定义它。

它可能看起来很简单,但我是 COBOL 编程的新手。

谢谢。

----------------* 更新 *----------

基于@lawerence 解决方案,这是正确的吗?

IF  FIRST < SECOND
     MOVE FIRST TO WS
     GO TO C10-END.
  END-IF.

  MOVE SECOND TO WS.

C10-END.
MOVE WS TO RESULT.

我刚刚将最后一个语句移回第一级。

I have the following code snippet in one of my COBOL program.

IF  FIRST < SECOND
   MOVE FIRST TO WS
END-IF.
MOVE SECOND TO WS.
MOVE WS TO RESULT.

I need to use GO TO inside the IF block to jump to the last statement (MOVE WS TO RESULT).

IF  FIRST < SECOND
   MOVE FIRST TO WS
   GO TO <last line.(MOVE WS to RESULT)>
END-IF.
MOVE SECOND TO WS.
MOVE WS TO RESULT.

in other word, i need to skip "MOVE SECOND TO WS.".
what is the simplest way to jump to a specific line in cobol?
I read somewhere that this is possible by defining a PARAGRAPH, but don't know how to define it.

It might seems very simple but I'm newbie to COBOL programming.

Thanks.

----------------* UPDATE *----------

based on @lawerence solution, is this correct?

IF  FIRST < SECOND
     MOVE FIRST TO WS
     GO TO C10-END.
  END-IF.

  MOVE SECOND TO WS.

C10-END.
MOVE WS TO RESULT.

i just moved back the last statement to be in first level.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

別甾虛僞 2024-11-12 12:54:04

GOTO 可以做你正在寻找的事情,但 IF/ELSE 会更直接。您希望 MOVE SECOND TO WS 运行当且仅当 IF 块不运行,对吗?

IF  FIRST < SECOND
    MOVE FIRST TO WS
ELSE
    MOVE SECOND TO WS
END-IF.
MOVE WS TO RESULT. 

我希望我的语法正确,我从未使用过 COBOL,只是尝试处理您的代码片段和此示例 http://www.fluffycat.com/COBOL/If-and-End-If/。将来可能会出现需要 GOTO 的情况,但是 A)当另一个控制结构可以工作时应该避免使用它,B)我一点也不知道它是如何做到的

,老实说,COBOL 看起来相当悲惨,哈哈。我从未见过如此冗长的语言。祝一切顺利


编辑


主要是为了乔...

这一切不能用 min 函数更好地完成吗?我确信语法是错误的,但是:

Move Function Min(FIRST, SECOND) to RESULT

GOTO can do what you're looking for, but IF/ELSE would be more direct. You want MOVE SECOND TO WS to run iff the IF block does not, correct?

IF  FIRST < SECOND
    MOVE FIRST TO WS
ELSE
    MOVE SECOND TO WS
END-IF.
MOVE WS TO RESULT. 

I hope I got the syntax right, I have never used COBOL and just tried to work off your snippet and this example http://www.fluffycat.com/COBOL/If-and-End-If/. There probably will be situations in the future where you need GOTO, but it A) should be avoided when another control structure will work and B) I haven't the slightest idea how its done

to be honest, COBOL looks pretty miserable lol. ive never seen a language so verbose. good luck with everytihng


EDIT


Mostly for joe...

Cant this all be better done with a min function? I'm sure the syntax is wrong, but:

Move Function Min(FIRST, SECOND) to RESULT
清风挽心 2024-11-12 12:54:04

欧姆福斯!现在不是 1974 年,你为什么要这样写 Cobol?这:

IF  FIRST < SECOND
   MOVE FIRST TO WS
END-IF.
MOVE SECOND TO WS.
MOVE WS TO RESULT.

有很多问题:

  • 它使用句点来界定范围,这已被弃用近三十年了。
  • 它没有使用 ELSE
  • 它正在尝试使用 GO TO。

自 1985 年以来,我可以建议采用以下方法:

If FIRST < SECOND
  Move FIRST to WS
Else
  Move SECOND to WS
End-IF
Move WS to RESULT

但实际上,代码应该简单地写为:

If FIRST < SECOND
  Move FIRST to RESULT
Else
  Move SECOND to RESULT
End-If

除非 WS 中需要中间结果。 Cobol 66 和 74 使用 GOTO 和句点,因为它们缺乏现代控制结构。我知道您是一个“新手”,但您应该向教您的人建议他们确实需要提升技能。

OMFSM! It is not 1974, why are you writing Cobol like that? This:

IF  FIRST < SECOND
   MOVE FIRST TO WS
END-IF.
MOVE SECOND TO WS.
MOVE WS TO RESULT.

Has a number of problems:

  • It uses periods to delimit scope, that is nearly three decades deprecated.
  • It isn't using ELSE
  • It is trying to use GO TO.

May I suggest the following as the way to approach it since 1985:

If FIRST < SECOND
  Move FIRST to WS
Else
  Move SECOND to WS
End-IF
Move WS to RESULT

But really, the code should simply read:

If FIRST < SECOND
  Move FIRST to RESULT
Else
  Move SECOND to RESULT
End-If

Unless the intermediate result is needed in WS. Cobol 66 and 74 used GOTO and periods because they lacked modern control structures. I realize you are a 'newbie', but you should suggest to whoever is teaching you that they really need to upgrade their skills.

笑叹一世浮沉 2024-11-12 12:54:04

jon_darkstar 在改进逻辑方面是正确的,但是如果您想了解 GO TO 是如何工作的,请输入:

  IF  FIRST < SECOND
     MOVE FIRST TO WS
     GO TO C10-RESULT.
  END-IF.

  MOVE SECOND TO WS.

C10-RESULT.
  MOVE WS TO RESULT.

C10-RESULT. 开始一个段落并且必须是一个您的代码部分中的唯一名称。按照惯例,它也应该以与封闭部分相同的前缀开头。因此,此示例假设您的代码 SECTION 类似于 C00-MAIN-PROCESS SECTION。

jon_darkstar is right when it comes to improving the logic, however if you want to see how GO TO works here goes:

  IF  FIRST < SECOND
     MOVE FIRST TO WS
     GO TO C10-RESULT.
  END-IF.

  MOVE SECOND TO WS.

C10-RESULT.
  MOVE WS TO RESULT.

C10-RESULT. starts a paragraph and has to be a unique name in your code SECTION. By convention it should also start with the same prefix as the enclosing section. Therefore this example assumes that your code SECTION is something like C00-MAIN-PROCESS SECTION.

羁〃客ぐ 2024-11-12 12:54:04

一个新的无答案让这个愚蠢的问题暴露出来。

IF 中的 ELSE 是 100% 显而易见的答案。非常奇怪的是,必须使用 GO TO 而不能使用 ELSE。

另一种方式,令人惊讶的是它没有出现:

MOVE SECOND TO WS
IF  FIRST LESS THAN SECOND
   MOVE FIRST TO WS
END-IF
MOVE WS TO RESULT

没有 ELSE,没有 GO TO。不过,当 FIRST 小于秒时会执行额外的 MOVE。

包含 GO TO 很简单,但很愚蠢。添加“转到”。 GO TO 必须去某个地方(除非使用 ALTER ... TO PROCEED TO ...,每个人都希望你不是),所以在你希望它到达的地方创建一个标签,并添加该标签的名称转到“转到”。

标签是用户定义的单词。如果被引用(如本例所示),它在 SECTION 中必须是唯一的,如果您使用 SECTION,则不需要,否则在程序中是唯一的,并且无论是否引用,它都可能与其他名称不同(例如数据定义或文件的内部名称)。

标签是过程名称。过程名称应以句点/句号终止,过程本身也应以句点/句号终止。

MOVE FUNCTION MIN ( ... ) ... 作为解决方案怎么样?

嗯,它有效。如果您站点的其他员工不习惯它,则不会感谢您使用它(无论如何,无需事先讨论)。

它有什么作用?那么,在 Enterprise COBOL 中,编译器会生成一个额外的小区域,将第一个参数复制到该区域,针对第二个参数进行测试,将第一个参数或第二个参数(以相关者为准)的副本复制到结果。

与 ELSE 相比,这是定义了一个额外的存储区域、一条用于其可寻址性的额外指令、一个额外的汇编器移动 (MVC) 以及缺乏就绪识别。

对于刚接触 COBOL、习惯了其他语言的多种函数的程序员来说更好吗?事实并非如此,因为如果他们不编写其他员工可以理解的程序(凌晨 2 点),他们就会被彻底击败。

IF FUNCTION MIN(VAR1 VAR2 VAR3 VAR4 VAR5) = 17

这是 FUNCTION 的另一个缺点。你看,你可以做到这一点。然后,凌晨 2 点,当程序崩溃了 32 行后,VAR1 和 VAR3 被更改后,您是否能够在核心转储中找到该 IF 的结果?也许,也许不是。取决于是否使用了任何其他功能以及类型。凌晨 2 点,你不希望这样。一点也不。

从好的方面来说,打字更少。对于那些打字而不是使用编辑器的人。

A new non-Answer has brought this silly question to light.

ELSE in the IF is the 100% obvious answer. It is deeply odd that GO TO has to be used whereas ELSE may not be used.

Another way, surprised it didn't come up:

MOVE SECOND TO WS
IF  FIRST LESS THAN SECOND
   MOVE FIRST TO WS
END-IF
MOVE WS TO RESULT

No ELSE, no GO TO. Extra MOVE executed when FIRST is less than second, though.

To include a GO TO is simple, but stupid. Add a GO TO. A GO TO has to go somewhere (unless using ALTER ... TO PROCEED TO ..., which everyone hopes you weren't), so make a label at the point you want it to arrive, and add the name of that label to the GO TO.

A label is a user-defined word. If referenced (as in this case) it must be unique within a SECTION, if you use SECTIONs, which you don't need to, otherwise unique within the program and whether referenced or not it may not be the same name as something else (like a data-definition or the internal name of a file).

A label is a procedure-name. A procedure-name should terminate with a period/full-stop and the procedure itself should also terminate with a period/full-stop.

What about the MOVE FUNCTION MIN ( ... ) ... as a solution?

Well, it works. If other staff at your site are not used to it, you will not be thanked for using it (without prior discussion, anyway).

What does it do? Well, in Enterprise COBOL, the compiler generates an extra little area, copies the first argument to that area, tests against the second argument, copies the copy of the first argument, or the second argument, whichever is relevant, to the result.

Vs the ELSE, that is an extra storage area defined, an extra instruction for addressability of that, and an extra Assembler move (MVC) plus the lack of ready recognition.

Better for programmers new to COBOL, used to a multitude of functions in other languages? Not really, as they will be soundly beaten if they don't write programs that can be understood (at 2am) by the rest of the staff.

IF FUNCTION MIN(VAR1 VAR2 VAR3 VAR4 VAR5) = 17

It's another downside of FUNCTION. You see, you can do that. Then, at 2am, when the program has crashed 32 lines later, after VAR1 and VAR3 have been changed, are you going to be able to find the result of that IF in the core dump? Maybe, maybe not. Depends if any other functions, and of what type, have been used. At 2am, you don't want that. Not at all.

On the upside, it is less typing. For those who type, rather than use the editor.

谁的年少不轻狂 2024-11-12 12:54:04

在我们的商店中,我们会使用 Bill Woodger 提供的示例。但是,我们确实使用句点作为范围终止符。 COBOL 应该像任何其他语言一样结构化并使用 KISS 原则。 注意:

MOVE SECOND TO WS.
IF FIRST LESS THAN SECOND
    MOVE FIRST TO WS.
MOVE WS TO RESULT.

只有当我们确信 SECOND 和 FIRST 具有数值,或者 WS 是 PIC X() 字符串,而不是数字 PIC 9() 时,这才有效。
这是迄今为止最容易阅读的。无需 ELSE 或 GO TO。

In our shop, we'd use the example provided by Bill Woodger. However, we do use periods as scope-terminators. COBOL should be structured and use the KISS principle, just like any other language. This:

MOVE SECOND TO WS.
IF FIRST LESS THAN SECOND
    MOVE FIRST TO WS.
MOVE WS TO RESULT.

Note that this only works if we are assured that SECOND and FIRST have numeric values, or that WS is a PIC X() string, not a numeric PIC 9().
This is by far the easiest to read. No ELSE or GO TO required.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文