Search code examples
centry-point

what is one entry and one exit of a simple construct?


I am reading some C text at the address https://cs.senecac.on.ca/~btp100/pages/content/const.html.

In the section "STRUCTURED PROGRAMMING", the author mentioned: "Structured programs are understandable, testable and readily modifiable. They consist of simple constructs, each of which has one entry point and one exit point."

I understood what is a structured program, but I am not really understanding the idea "one entry point and one exit point". What if we do not have such stuff?

Can anyone elaborate on that, please?


Solution

  • Look at the Flags example close to the bottom and Avoiding Jumps below that: https://cs.senecac.on.ca/~btp100/pages/content/const.html#fla

    What they're basically trying to say here is that you could have some sort of loop (for/while/whatever) where you could use something like break to exit a loop prematurely, rather than waiting on the actual condition that you're checking in the loop to become false and have the loop exit normally. In this case you would have two exit points.

    They suggest the use of a flag variable added to the loop's condition to have a single exit point, makes sense.

    The use of continue is another example where you can "break structure." You could use continue to stop the current iteration of the loop and reenter it, where in this case you would have multiple entry points.

    Things like that can make code a lot harder to read and be able to follow the flow, even though sometimes it may seem necessary to do so.