The Importance of Decision Structures in Programming
- The “decision” of a decisions structure is a test, which must result in a true state in order to carry out the action associated with a test. Sections of the program, or almost the entire program can be skipped if the test result is not “true.” Other decisions decide whether a section of code should be repeated and how many times that code should be repeated. The two main structures implementing decisions in programs are the conditional branch and the loop.
- The only command that performs branching is the IF statement. The IF statement has three formats. The first tests for the condition and then only performs the code bracketed by the statement if the condition is true. The second format also provides an alternative section of code to perform if the condition is false. The third structure, which some languages implements as a CASE statement, provides a series of conditions giving different actions for each condition, should it prove to be false. This structure also provides a default action should all conditions fail.
- Looping, or iteration provides one of the main benefits of programming. Computers are very efficient at performing the same task again and again. Computers do not tire and make mistakes, like humans, when they are tasked with repeating an action again and again. The test can be positioned at the beginning of the loop, at the end, or by a statement somewhere within the lines of code inside the loop, that will break out of the loop if they test true. The difference between each strategy is that the loop with the test at the end will perform all of the code within the loop at least once. Where the test is at the beginning of the loop, all the code within the loop can be cut out completely, and the break statement will ensure that all code in the loop before that statement will be executed at least once.
- Branching and iteration both occur in almost all programs. Although branching often occurs without iteration, iteration usually includes IF statements. Conditional branching is usually required as a part of the code executed each time the loop repeats. Loops require a set of data – either a series of records fetched from a database, or a set of data stored in an array. In each case each record will be tested for a condition and branching will process each record differently according to its contents.
Decision
Branching
Looping
Combination
Source...