Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

Wikiversit y

Sear

Control structures
Flow Control Overview …

A cont rol st ruct ure is like a block of programming t hat analyses variables and chooses a
direct ion in which t o go based on given paramet ers. The t erm flow control det ails t he direct ion
t he program t akes (which way program cont rol "flows"). Hence it is t he basic decision-making
process in comput ing; It is a predict ion.

Basic Terminologies …

Those init ial condit ions and paramet ers are called preconditions. Precondit ions are t he st at e
of variables before ent ering a cont rol st ruct ure. Based on t hose precondit ions, t he comput er
runs an algorithm (t he cont rol st ruct ure) t o det ermine what t o do. The result is called a post
condition. Post condit ions are t he st at e of variables aft er t he algorit hm is run.

An Example …

Let us analyse flow cont rol by using t raffic flow as a model. A vehicle is arriving at an
int ersect ion. Thus, t he precondit ion is t he vehicle is in mot ion. Suppose t he t raffic light at t he
int ersect ion is red. The cont rol st ruct ure must det ermine t he proper course of act ion t o
assign t o t he vehicle.

Precondition: The vehicle is in mot ion.


Treatments of Information through Control Structures
Is t he t raffic light green? If so, t hen t he vehicle may st ay in mot ion.
Is t he t raffic light red? If so, t hen t he vehicle must st op.
End of Treatment
Post condition: The vehicle comes t o a st op.

Thus, upon exit ing t he cont rol st ruct ure, t he vehicle is st opped.
can also be form as a st ruct ure

IF-THEN Statement …

The IF-THEN st at ement is a simple cont rol t hat t est s whet her a condit ion is t rue or false.
The condit ion can include a variable, or be a variable. If t he variable is an int eger 2, it will be
t rue, because any number t hat is not zero will be t rue. If t he condit ion is t rue, t hen an act ion
occurs. If t he condit ion is false, not hing is done. To illust rat e:

IF variable is t rue
THEN t ake t his course of act ion.

If t he variable indeed holds a value consist ent wit h being t rue, t hen t he course of act ion is
t aken. If t he variable is not t rue, t hen t here is no course of act ion t aken.

IF-THEN-ELSE Statement …

IF-THEN st at ement s t est for only one act ion. Wit h an IF-THEN-ELSE st at ement , t he cont rol
can "look bot h ways" so t o speak, and t ake a secondary course of act ion. If t he condit ion is
t rue, t hen an act ion occurs. If t he condit ion is false, t ake an alt ernat e act ion. To illust rat e:

IF variable is t rue
THEN t ake t his course of act ion
ELSE call anot her rout ine

In t his case, if t he variable is t rue, it t akes a cert ain course of act ion and complet ely skips t he
ELSE clause. If t he variable is false, t he cont rol st ruct ure calls a rout ine and complet ely skips
t he THEN clause.

Not e t hat you can combine ELSE's wit h ot her IF's, allowing several t est s t o be made. In an IF-
THEN-ELSEIF-THEN-ELSEIF-THEN-ELSEIF-THEN st ruct ure, t est s will st op as soon as a
condit ion is t rue. That 's why you'd probably want t o put t he most "likely" t est first , for
efficiency (Remembering t hat ELSE's are skipped if t he first condit ion is t rue, meaning t hat
t he remaining port ions of t he IF-THEN-ELSEIF... would not be processed). eg:

In case your comput er doesn't st art


IF a floppy disk is in t he drive
THEN remove it and rest art
ELSE IF you don't have any OS inst alled
THEN inst all an OS
ELSE call t he hot line

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

WHILE and DO-WHILE Loops …

A WHILE loop is a process in which a loop is init iat ed unt il a condit ion has been met . This
st ruct ure is useful when performing it erat ive inst ruct ions t o sat isfy a cert ain paramet er. To
illust rat e:

Precondition: variable X is equal t o 1


WHILE X is not equal t o 9
Add 1 t o X

This rout ine will add 1 t o X unt il X is equal t o 9, at which point t he cont rol st ruct ure will quit
and move on t o t he next inst ruct ion. Not e t hat when t he st ruct ure quit s, it will not execut e
t he Add funct ion: when X is equal t o 9, it will skip over t he clause t hat is at t ached t o t he
WHILE. This inst ruct ion is useful if a paramet er needs t o be t est ed repeat edly before
accept ance.

DO-WHILE Loops …

A DO-WHILE loop is nearly t he exact opposit e t o a WHILE loop. A WHILE loop init ially checks
t o see if t he paramet ers have been sat isfied before execut ing an inst ruct ion. A DO-WHILE
loop execut es t he inst ruct ion before checking t he paramet ers. To illust rat e:

DO Add 1 t o X
WHILE X is not equal 9

As you can see, t he example differs from t he first illust rat ion, where t he DO act ion is t aken
before t he WHILE. The WHILE is inclusive in t he DO. As such, if t he WHILE result s in a false (X
is equal to 9), t he cont rol st ruct ure will break and will not perform anot her DO. Not e t hat if X
is equal t o or great er t han 9 prior t o ent ering t he DO-WHILE loop, t hen t he loop will never
t erminat e.
FOR Loops …

A FOR loop is an ext ension of a while loop. A for loop usually has t hree commands. The first is
used t o set a st art ing point (like x = 0). The second is t he end condit ion (same as in a while
loop) and is run every round. The t hird is also run every round and is usually used t o modify a
value used in t he condit ion block.

FOR X is 0. X is less t han 10. add 1 t o X.

This loop would be run 10 t imes (x being 0 t o 9) so you won't have t o t hink about t he X
variable in t he loop you can just put code t here. Here is a while loop t hat does t he same:

X is 0
WHILE X is less t han 10
add 1 t o X

Some programming languages also have a for each loop which will be useful when working
wit h arrays (and collect ions). A for each loop is an even more aut omat ed while loop t hat
cycles t hrough array's and such. Example :

FOR EACH st udent IN st udent s


give a good mark t o st udent

Next Lesson

Previous Lesson

Course Home Page

Retrieved from
"https://en.wikiversity.org/w/index.php?
title=Control_structures&oldid=2290175"

Last edited 11 months ago by Hasley

Wikiversity

You might also like