Control Structures
Control Structures
IF-THEN Statement
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 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
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 condition
is true, then an action occurs. If the condition is false, take an alternate action.
To illustrate:
IF variable is true
THEN take this course of action
ELSE call another routine
In this case, if the variable is true, it 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 a
condition 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.
DO-WHILE Loops
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
A FOR loop is an extension of a while loop. 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 foreach loop which will be useful when working with
arrays (and collections). A foreach loop is an even more automated while loop that cycles through
array's and such. Example:
FOREACH student IN students
give a good mark to student