A. Sequence
- The sequence control structure is the simplest of all basic control structures. In a sequence, statements are executed in order, one after another, as the sequence states. It is simply a series of statements with no decision making, looping, or branching. Sequences are like a straight line. There can be as many statements as you would like in a sequence as long as they are executed one after the next in the order determined by the programmer that wrote the Sequence.
B. Selection (Decision, If Then/Else) – A Selection Control structure is a structure that provides a decision when a question is asked. Selection is limited to a true or false / yes or no statement. When a question proves to be true then a statement or a sequence of statements will be executed. When a question proves to be false a different statement of sequence of statements will be executed. The If Then/Else statement is similar. If something is true than a certain statement or sequence will be executed, if it is false than nothing will be executed and that statement or sequence will be skipped. The Case Control structure is presented when you need an option for more than two alternative results. It is basically an extended form of the Selection structure.
C. Loop (looping, iteration) – A loop is a control structure that executes statements repeatedly. Loops contain three basic parts: a loop termination decision (determines when the loop will terminate), the body of the loop (the statements that are repeated in the loop), and a transfer to the beginning of the loop (returns control to the top of the loop, beginning a new iteration). The two basic types of loops are the Pre-test Loop and the Post-test Loop. A Pre-test Loop executes the loop termination decision first. If conditions are favorable to execute the body of the loop, those statements are executed followed by a transfer to the beginning of the loop (the loop termination decision). A Post-test Loop executes the body of the loop first, followed by the loop termination decision. If conditions are favorable a transfer to the beginning of the loop is executed, followed by an iteration of the body of the loop.
Pre-Test Loop Post-Test Loop
D. Unconditional Branch (GOTO) – A Unconditional Branch Control Structure or GOTO Statement is a simple statement. There is no decision to be made. Basically it instructs the computer to jump to a specified label or line of code unconditionally (no matter what). Most high-level languages use the GOTO instruction followed by a destination to perform this operation. Similarly, a Conditional Branch Control Structure provides the same GOTO instruction but it is based on a Boolean (true/false) condition. If the condition is true the GOTO instruction is followed, if the condition is false it does not. The constructs of modern programming and the Control Structures listed above virtually eliminate the need for GOTO statements. Today, GOTO statements are mostly required when writing error-handling code.
Pre-Test Loop Post-Test Loop
D. Unconditional Branch (GOTO) – A Unconditional Branch Control Structure or GOTO Statement is a simple statement. There is no decision to be made. Basically it instructs the computer to jump to a specified label or line of code unconditionally (no matter what). Most high-level languages use the GOTO instruction followed by a destination to perform this operation. Similarly, a Conditional Branch Control Structure provides the same GOTO instruction but it is based on a Boolean (true/false) condition. If the condition is true the GOTO instruction is followed, if the condition is false it does not. The constructs of modern programming and the Control Structures listed above virtually eliminate the need for GOTO statements. Today, GOTO statements are mostly required when writing error-handling code.
I like the way you formatted this post. It has paragraph breaks (which is more than some people) and even includes images, a definite plus for any post that usually just has lots of text. And then, putting each control structure element into an A-D list just makes things that much easier to understand.
ReplyDeleteSequences are often very useful, but can sometimes be a pain to correctly line up to yield proper results, especially in assembly. It is fine enough to tell the computer one line at a time what’s going to happen, but if you don’t set a label, registry or bit offset correctly then everything else down the line will be completely messed up. And with registries the way they are, it can be a confusing endeavor to debug.
I personally find that selection statements and looping controls are the most used forms of control structures that I use in my programs, probably selections more than loops in fact. Both have their unique uses, and both are extremely important to the successful execution of any program. But I find that unconditional branches (or goto statements) are the least-used form of control structure that I use in programs. I don’t see this statement used much in practice actually, possibly used in conjunction with other control structures. Outside of that, when code can be just as easily moved around to create a sequence rather than perform an unconditional branching, I would go for that instead. So as you note, error handling may be the only place I would see such a statement. But I can see this being more useful if it was used as a jump return statement rather than a simple jump. Then there is some more primary control going on rather than a one-way ticket.