Control structures

Course Navigation

<< Previous - Operators and expressions Next - Procedures and functions >>


Flow Control Overview

edit

A control structure is like a block of programming that analyses variables and chooses a direction in which to go based on given parameters. The term flow control details the direction the program takes (which way program control "flows"). Hence it is the basic decision-making process in computing; It is a prediction.

Basic Terminologies

edit

Those initial conditions and parameters are called preconditions. Preconditions are the state of variables before entering a control structure. Based on those preconditions, the computer runs an algorithm (the control structure) to determine what to do. The result is called a post condition. Post conditions are the state of variables after the algorithm is run.

An Example

edit

Let us analyse flow control by using traffic flow as a model. A vehicle is arriving at an intersection. Thus, the precondition is the vehicle is in motion. Suppose the traffic light at the intersection is red. The control structure must determine the proper course of action to assign to the vehicle.

Precondition: The vehicle is in motion.
Treatments of Information through Control Structures
Is the traffic light green? If so, then the vehicle may stay in motion.
Is the traffic light red? If so, then the vehicle must stop.
End of Treatment
Post condition: The vehicle comes to a stop.

Thus, upon exiting the control structure, the vehicle is stopped.

can also be form as a structure

IF-THEN Statement

edit

The IF-THEN statement is a simple control that tests whether a condition is true or false. The condition can include a variable, or be a variable. If the variable is an integer 2, it will be true, because any number that is not zero will be true. If the condition is true, then an action specified within the "THEN" statement occurs. If the condition is false, nothing is done. To illustrate:

IF variable is true
THEN take this course of action.

If the variable indeed holds a value consistent with being true, then the course of action is taken. If the variable is not true, then there is no course of action taken.

IF-THEN-ELSE Statement

edit

IF-THEN statements test for only one action. With an IF-THEN-ELSE statement, the control can "look both ways" so to speak, and take a secondary course of action if the conditional statement is not true. If the condition is true, then an action occurs. If the condition is false, an alternate action specified in the "ELSE" statement occurs. To illustrate:

IF variable is true
THEN take this course of action
ELSE call another routine

In this case, if the variable is true, the program takes a certain course of action and completely skips the ELSE clause. If the variable is false, the control structure calls a routine and completely skips the THEN clause.

Note that you can combine ELSE's with other IF's, allowing several tests to be made. In an IF-THEN-ELSEIF-THEN-ELSEIF-THEN-ELSEIF-THEN structure, tests will stop as soon as the program finds a condition that is true. That's why you'd probably want to put the most "likely" test first, for efficiency (Remembering that ELSE's are skipped if the first condition is true, meaning that the remaining portions of the IF-THEN-ELSEIF... would not be processed). eg:

In case your computer doesn't start
IF a floppy disk is in the drive
THEN remove it and restart
ELSE IF you don't have any OS installed
THEN install an OS
ELSE call the hotline

You can have as many ELSE IF's as you like.

By contrast, you can also place multiple IF-THEN statements in succession to check for every conditional. By this method, the program will not stop evaluating subsequent conditionals when it finds one that is true.

WHILE and DO-WHILE Loops

edit

A WHILE loop is a process in which a loop is initiated until a condition has been met. This structure is useful when performing iterative instructions to satisfy a certain parameter. To illustrate:

Precondition: variable X is equal to 1
WHILE X is not equal to 9
Add 1 to X

This routine will add 1 to X until X is equal to 9, at which point the control structure will quit and move on to the next instruction. Note that when the structure quits, it will not execute the Add function: when X is equal to 9, it will skip over the clause that is attached to the WHILE. This instruction is useful if a parameter needs to be tested repeatedly before acceptance. In the above example, if X is already equal to or greater than 9 when the program reaches the WHILE loop, the program will entirely skip the loop.

DO-WHILE Loops

edit

A DO-WHILE loop is nearly the exact opposite to a WHILE loop. A WHILE loop initially checks to see if the parameters have been satisfied before executing an instruction. A DO-WHILE loop executes the instruction before checking the parameters. To illustrate:

DO Add 1 to X
WHILE X is not equal 9

As you can see, the example differs from the first illustration, where the DO action is taken before the WHILE. The WHILE is inclusive in the DO. As such, if the WHILE results in a false (X is equal to 9), the control structure will break and will not perform another DO. Note that if X is equal to or greater than 9 prior to entering the DO-WHILE loop, then the loop will never terminate.

FOR Loops

edit

A FOR loop is an extension of a while loop which iterates for a predetermined length. A for loop usually has three commands. The first is used to set a starting point (like x = 0). The second is the end condition (same as in a while loop) and is run every round. The third is also run every round and is usually used to modify a value used in the condition block.

FOR X is 0. X is less than 10. add 1 to X.

This loop would be run 10 times (x being 0 to 9) so you won't have to think about the X variable in the loop, you can just put code there. Here is a while loop that does the same:

X is 0
WHILE X is less than 10
add 1 to X

Some programming languages also have a for each loop which will be useful when working with arrays (and collections). A for each loop is an even more automated while loop that cycles through array's and such. Example :

FOR EACH student IN students
give a good mark to student