About structural programming
Many in the comments to the post about the goto operator expressed the same opinion, which sounds something like this: “For n years of writing programs, I never needed goto, and I'm not going to use it in the future either.” And they are absolutely right, the structuring theorem has long been proved, which states that any simple program is functionally equivalent to a structured program composed using the 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:
So, if one day you write something like this (the program from the goto post ):
Better rewrite this code. It is not in vain that one smart person said: “Write the code as if it would be accompanied by a violent psychopath who knows where you live.” Given 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 structural programming. First, we compose a block diagram of this algorithm and number the blocks and arcs in accordance with the theorem.
Now build a while do loop with the replacement of predicate and function blocks.
The result was a structured program diagram, but there was too much in it. We transform it in accordance with the following algorithm:
For each j> 0, we try to replace all the assignments L = j with the corresponding subprogram Gj (including for L = 1). If L will never again be assigned the value j, you can remove the check L = j (together with its corresponding execution branch) from the program structure.
Using this scheme, you can easily write code that complies with the principles of structural programming.
Or completely get rid of the flag as eigrad did here .
It was also possible to write a recursive code by first converting the block diagram of the program into the so-called E-diagram.
The code:
Given that in real programming, functions are named according to the actions they perform, this code also becomes more readable than code using goto.
When writing the topic, the methodology of the NSTU “Informatics. Theory and practice of structural programming. ”T.A.Shaposhnikova.
PS Thank funca for his questions and corrections.
- number all nodes of the circuit, while the bypass order is arbitrary;
- number all arcs of the circuit as follows: assign the number 0 to the output arc of the circuit, assign to all other arcs the number of the vertex into which this arc enters;
- 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
- for each predicate node with number i, create a new simple program
- build a program like while do 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 (the program from the goto post ):
if (p1)
{
f1;
goto L3;
}
L1:
if (p2)
{
L2:
f2;
L3:
f3;
goto L1;
}
else if (p3)
{
f4;
goto L2;
}
f5;
Better rewrite this code. It is not in vain that one smart person said: “Write the code as if it would be accompanied by a violent psychopath who knows where you live.” Given 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 structural programming. First, we compose a block diagram of this algorithm and number the blocks and arcs in accordance with the theorem.
Now build a while do loop with the replacement of predicate and function blocks.
The result was a structured program diagram, but there was too much in it. We transform it in accordance with the following algorithm:
For each j> 0, we try to replace all the assignments L = j with the corresponding subprogram Gj (including for L = 1). If L will never again be assigned the value j, you can remove the check L = j (together with its corresponding execution branch) from the program structure.
Using this scheme, you can easily write code that complies with the principles of structural programming.
if p1:
f1
f3
l = 3
while l > 0:
if p2:
f2
f3
elif p3:
f4
f2
f3
else:
f5
l = 0;
It was also possible to write a recursive code by first converting the block diagram of the program into the so-called E-diagram.
The 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()
Given that in real programming, functions are named according to the actions they perform, this code also becomes more readable than code using goto.
When writing the topic, the methodology of the NSTU “Informatics. Theory and practice of structural programming. ”T.A.Shaposhnikova.
PS Thank funca for his questions and corrections.