Decision making

It is rare to write a program that does not change the normal flow of the program. Programs use control structures such as condition checking, looping, and branch logic to change the normal flow of the program. A control structure simply refers to the flow of the execution of the program.

A program can execute in one of two ways: in a linear or non linear fashion. Linear or sequential execution refers to the execution of the statements in the order they are listed. In comparison, the non-linear execution refers to the execution of statements regardless of the order they are listed.

As shown in figure 1, statement 1 is executed before statement 2 in a linear execution. In other words, statements are executed in the order they are listed. However, in a non-linear execution, as the figure shows, statement 2 is executed after statement 3 even though statement 2 is written before statement 3. See figure 1.

Figure 1 linear vs non-linear execution
Figure 1 linear vs non-linear execution

Figure 2 simple IF statement illustration
Figure 2 simple IF statement illustration

By controlling the flow of the program, you can have your program precisely decide if and when what code will execute. (Visit corresponding pages for looping and branching control structures. This page covers only condition checking.) Your program makes decisions on what code to execute based on the result of conditional logic. The conditional logic structure refers to the process of checking conditions. If a condition is met, then, some code is executed. If, however, a condition is not met, then, some other part of the program is executed. For example, you may decide you need a raincoat if it is raining. In this case, our condition is "if it is raining." If this condition is satisfied, then, you decide you need a raincoat. See figure 2.

Depending on the condition(s) you want to check, you may employ any one or more of these conditional logic options: