Structure that "if a program goes from one entrance to one exit, any process can be described with only three combinations of" sequential "," selection "and" repetition "and their combinations. There is a theorem.

I wondered here, but what do you mean by "entrance" and "exit" here?

When I thought again, I couldn't imagine a code with multiple entrances and exits in my vague entrance/exit image, and I couldn't understand it even after examining it.
In fact, I should explain my interpretation here to correct the difference in interpretation, but it's so voluminous that I can't put it into the language. . .

I have mainly written Java, so there may be such a backbone.
I would appreciate it if you could answer even a small thing.

  • Answer # 1

    In this case, "entrance" and "exit" refer to the part at the beginning of execution of an execution process and the part that finishes and ends (exits). When hitting "Structured theorem" on wikipedia and looking for the origin of "entrance" and "exit",
    Structured theorem-wikipedia 日本語 版
    Structured program theorem-wikipedia English
    The English word corresponding to "entrance" is "entry path", and the English word corresponding to exit is "exit path". If we translate verbatim in a verbose way, will each be a "passage for entry, a" passage for exit "? Perhaps it is a word that is still difficult to understand for the questioner.

    I'm not a bright programming language called COBOL, so I'll remove that example. In assembly language, etc. that have been used for a long time, the structure of branching and repetition is a "label (≒ address)" Is realized by jumping to "." In addition, when a certain process is started, the caller can execute it from multiple execution start positions. You can call them "entrances", and when you exit the process, you can call them "exit" because you jump to the label you want to jump to as soon as you meet the end condition. It is also possible to have such a structure.

    In Java, the unit of processing is largely "methods", and it is not possible to do so in a way that jumps from a method of a class to a "middle" of a method of a class. Write areturnstatement in the middle of a method and write multiplereturnstatements in one method, orthrowdepending on the error. There is no point in saying that there are multiple "exits" at this point, but it is a single exit from the top calling the method. Therefore, it would be difficult to associate "multiple entrances and exits" from Java.

    If you don't understand the assembly language, it might be easier to get an image if you look briefly at the programming language BASIC. There are many programming languages ​​named BASIC, but it is not traditional Visual Basic, but traditional line number oriented BASIC. Except for branching to a subroutine calledGOSUB(a method called Java), you basically specify a line number to branch (jump). When programmed carelessly, it immediately becomes a so-called "spaghetti program" with multiple entrances and exits.

  • Answer # 2

    In the assembler, multiple entrances can be easily done, and I think it was not unusual in the past.
    (For example, if the program size is very limited, I think it will be used a lot)

    This is the case if there is no clear, enclosed function or method.

    If it is an original BASIC other than VB, this area is the same as the assembler.

    In old COBOL, functions (and methods) in other languages ​​could not be written in one compilation unit.
    Instead, the instruction "Run from the statement at label X to the statement at label Y and come back" is used.
    Of course, the ranges can overlap.
    (Assuming that the labels are aligned with A, B, C, and D, there may be two processes: "Run from label A to C" and "Run from label B to D")

    Also, in old Fortran, there was a sentence calledENTRY name (formal argument)that creates another entry in the middle of a subroutine or function.

    There are usually multiple exits from functions, etc.
    Also, in terms of structure rather than a group of functions, there are multiple loop exits.
    An example of C, but

    for (i = 0;i

    Then, there are two exits, the exit bybreakand the exit when it is no longeri.

  • Answer # 3

    I don't know the "Structured Theorem", but probably in old literature
    Wasn't it seen?

    In the past, it was like jumping through the code with a GO TO statement
    The program seems to have remained,
    against it Structured programming was advocated as an indicator.
    * Personal interpretation.

    In that sense, for chaotic programming
    Applying the concept of entrances and exits to make such theorems
    Isn't it a definition?

  • Answer # 4

    COBOL is a language that was created before the idea of ​​object orientation was born, so the concept of encapsulation and blocks was rather sparse, and the entry point (entry point) of a function (called a subroutine in COBOL) There may be multiple functions and expressions depending on the manufacturer and compiler), and there may be multiple return exits of functions.

    I don't care much.

  • Answer # 5


    I think the entry point (≒ the beginning of the function) is the entry point, and the exit is a return statement.

    And multiple return statements can be considered multiple exits.
    Multiple entrances are difficult to understand. An ordinary function has only one entrance.
    It may be easy to understand if you interpret a module that consists of multiple functions and these functions are related to calling as a "program". It can be said that multiple functions called when using the module correspond to multiple entrances.

    However, the opposite concept of structuring is the flow of processing like flowcharts and assemblers, so you can make as many entrances and exits as you want.

Related articles