Control Structures
Microcomputer/DOS Tutorial


Computer Studying

Navigation
{NavForms}

Virus Tutorial Map
Tutorial Map

As we have seen, computers operate on a sequence of instructions, known as programs. To make program generation easy and programs readable by others, a logical approach is a reasonable requirement. Code should be structured in a manner that makes it easily understood.

Mathematical analysis tells us that any problem may be solved by a series of steps that include one or more of three so called control structures...

  • Sequence: A series of steps, one following another.
  • Selection: A decision is made based on a test.
    and
  • Iteration: A repeating process.

We'll look at each of these next. The choice of which to use in what order depends on how you have set up your problem solution.

Sequence

A sequence control structure is a series of statements (operations) that do not involve choices or repeating. As a simple example, consider the problem of adding two values (X and Y):

  • Get X value: We'll assume that X contains the value 5. That value is loaded into the CPU.
  • Get Y value: Further assume that Y contains the value 9. That value is moved to a CPU register.
  • Add X and Y: Cause the CPU to add the numbers.
  • Show result: Display the result (14) on the screen.

Selection

Decisions are made with this control structure. The usual program statement associated with selection is IF...THEN...ELSE, where you do one thing or another based on the outcome of a test.

Let's add overtime to a base pay if overtime was expended.

In selection, a test variable is first checked. One of two paths is then taken depending on the test result.

  • Was overtime worked?
    • No - to end
    • Yes - compute OT pay at Rate * 1.5 then add to Salary

In this case, if the test yields false then control passes to the next step in the program.

If true, overtime pay is computed and added, then control is passed to the next step.

Looping

Often, program logic will require a given task to be performed a number of times. There are three types of loop, depending upon the logical need:

  • For.....
    When you know the exact number of loops.
  • While......Do
       or
    Repeat..Until
    When your test must be based on the change of some variable. Implementation depends on the language.

The For... loop requires you to specify the number of times the program will repeat the given task. Where you are not certain, one of the other two looping types would be preferred. Here is an example,

Sum := 0;
For I := 1 to 100 do
Sum := Sum + I;

Line 1 sets a memory location equal to zero. The next line defines a loop that will repeat exactly 100 times and execute the command found in the third line.

This simple program fragment adds the positive integers between 1 and 100. Each time the For... statement is executed, the variable I is incremented by one, until its value reaches 100.

The While..Do loop will continue to execute so long as a stated condition continues to be True. The test for the condition is made at the top of the loop.

While not odd (number) do
number := number div 2;

This program fragment loops while number is even. Note that the test is performed first so the loop may never be executed if number is odd.

The Repeat..Until loop, like the While..Do loop, continues until a condition changes. The major difference is that the Repeat loop is always executed at least once.

N := 1;
repeat
writeln (N);
N := succ (N)
until N = 11;

(Prints numbers 1 - 10 on the screen)

Keep Reading Program Design


{NavBar}
Home | Products | Definitions | Newsletter | Virus Tutorial | Contact


FILExt is proudly hosted by DewaHost.com - a fast and reliable host for everyone!
DewaHost offers premium Web hosting starting from $8.95/month and a high speed file hosting service FileBurst!

Please use the contact form for questions or comments about this web site.
Copyright © 2001 Computer Knowledge, All Rights Reserved
Pray the Rosary for peace.