Main Content

From flowchart to program - code design for the newcomer

Archive - Originally posted on "The Horse's Mouth" - 2011-09-29 07:07:25 - Graham Ellis

We're all used to seeing little flow chart diagrams on instrtuction sheets and in books - telling us things like whether we should call the doctor for our sick child, or let him/her sweat it out. So - for newcomers to programming - flowcharts are a natural way of drawing out what the program needs to do. But how to translate such a diagram into a program?


Programs are written as a series of instructions within a file - line by line, and the two lins coming down the page as shown in my first diagram isn't possible - so the first stage is to redraw the flowchart into a single line, as I've done here on the right. The lines are all joining the same boxes, but those boxes have been moved above each other and there are some jumps around.


Then you can start to see the structure of your code. The decision boxes are going to be "if" statements, with an "else" before the second of the sets of boxes that we stacked in the second stage. And there you have your first program structure to hand.


Progressing on further ... if you have one of the vertical stacks jumping BACK to the decision box, you'll be looking at a loop (with a world like "while" replacing "if"), and if each of the boxes performs a substantial task, then you'll want to write the code for it really simply as a call to a function which you'll write - programming at a macro level, and you can then separately flowchart and "solve" each block in turn. You're headed - without even realising it - for a structured programming technique ...