📜 ⬆️ ⬇️

About structured programming

Many in the comments to the post about the goto operator expressed the same opinion, which sounds like this: “I have never needed goto for n years of writing programs, and I don’t intend to use it in the future either.” And they are absolutely right; a structuring theorem has long been proved, which states that any simple program is functionally equivalent to a structured program composed of functions and predicates of the original program, as well as using an additional counter. The proof is the algorithm for compiling the very structured program:
  1. number all the nodes of the scheme, with the random order;
  2. enumerate all the arcs of the scheme as follows: we assign the number 0 to the output arc of the scheme, to all the other arcs we assign the number of the vertex into which this arc enters;
  3. for each functional node of the source program having the number i and the output arc j, create a new simple sequential program Gi with the number of the input arc i
  4. for each predicate node i, create a new simple program
  5. build a while do program with a do-part in the form of a structure that checks the values ​​of L.


So, if one day you write something like this (program from a post about goto ):

if (p1) { f1; goto L3; } L1: if (p2) { L2: f2; L3: f3; goto L1; } else if (p3) { f4; goto L2; } f5; 


It is better to rewrite this code. After all, it’s not for nothing that one intelligent man said: “Write the code as if a psychopath who is prone to violence and knows where you live will accompany him.” If we consider that most programmers do not like goto, then this is more than relevant.
')
We use the theorem and transform this code in accordance with the principles of structured programming. First, we draw up a block diagram of this algorithm and enumerate the blocks and arcs in accordance with the theorem.

image

Now we construct a while do loop with the replacement of predicate and function blocks.

image

The result is a structured program diagram, but there is too much superfluous in it. Convert it according to the following algorithm:
For each j> 0 we try to replace all assignments L = j with the corresponding subroutine Gj (including for L = 1). If L is never assigned the value j, then you can remove the check L = j (together with the corresponding execution branch) from the program structure.

image

With this scheme, you can easily write code that corresponds to the principles of structured programming.

 if p1: f1 f3 l = 3 while l > 0: if p2: f2 f3 elif p3: f4 f2 f3 else: f5 l = 0; 


Or completely get rid of the flag as eigrad did here .

It was also possible to write a recursive code beforehand converting the program flowchart into the so-called E-scheme.

image

Code:
 def F1(): if p2: F2() elif p3: f4 F2() else: f5 def F2(): f2 F3() def F3(): f3 F1() if p1: f1 F3() else: F1() 


If we consider that in real programming, functions are named according to the actions they perform, this code also becomes more readable than the code using goto.

When writing a topic, the NSTU Informatics. Theory and practice of structured programming. ”T.A.Shaposhnikova.

PS Thank you funca for his questions and corrections.

Source: https://habr.com/ru/post/114326/


All Articles