Control Instructions, Algorithms, and Computer Programs

Control in computer programs is concerned with deciding which instruction to perform next. There are four basic types of control: sequence, branch, loop, and subprogram.

  1. Sequence -- do the next thing in order; physical order = logical order. Example: input a number, add one, and output the new value. This example contains three instructions, executed in order.
  1. Input number
  2. Add 1 to the number
  3. Output the updated value
  1. Branch (choice/selection/if/if-then/if-then-else) -- the instruction to be performed next is chosen based on some test. Example: input a number, if the number is less than zero, output its opposite (put a minus sign in front of it), otherwise output the original number (This set of instructions takes a number and outputs its absolute value, which is its distance from 0 on the number line.)
  1. Input number
  2. If number is negative (the test)
  3. 2a. Output the opposite of that number

    Else

    2b. Else output the number

  4. Loop (iteration/repetition) -- a set of instructions are performed repeatedly until some condition is satisfied. Example, input a number and output that number plus one, keep doing this until a zero is input. The Repeat-Until structure is one way of representing a loop.
  1. Repeat
  2. 1a. Input number

    1b. If number is not zero (test)

    1b-a. Add 1 to number

    1b-b. Ouput the number

  3. Until number is zero (test)
  1. Subprogram (subroutine/procedure) -- a set of instructions are grouped together and (usually) given a name. These instructions perform some specific task, for example, a print routine. They will probably be needed in several different places in a program, and may be used by many different programs. Putting them together so that they can be called repeatedly and shared is efficient. Example, special print routine:
  1. Input a number
  2. Call the subprogram to print a banner with that number on it

A macro is a subprogram. The idea of a macro is to encapsulate a set of instructions that you want to repeat in different places, perhaps in different documents or spreadsheets..

An algorithm is a step-by-step procedure for accomplishing some task. It must be clear and unambiguous, and it must eventually stop (i.e., it must be finite) with a correct solution. The word comes from Abu Ja'far Muhammad ibn-Musa Al-Khowarizmi, a Persian mathematician from the 8th-9th centuries who was a teacher at the Mathematical Institute in Bhagdad.

A computer program can be written to implement an algorithm. Computer programs are usually written using a "high level" language (see class notes). Programming languages have instructions to implement the different types of control. Differences among programming languages have to do with special features they incorporate, and not in their fundamental abilities to encode algorithms. The process of programming involves several steps: idea à algorithm à computer program, where the algorithm is a formal specification of a problem solution.

Many tasks we carry out on a daily basis can be described algorithmically, using the basic control instruction types. For example, recipes for cooking are algorithms. They involve sequencing (follow instructions in order), branching (if you don't have buttermilk, then substitute lemon juice and milk), looping (cook until cake springs back to the touch), and subprograms (see the instructions for making meringue on page 403). Think about your algorithm for coming to UB. If you drive, you have a plan for finding a parking place. If you take the bus, you have a plan for where and when to catch it, and you probably have a preference for where you like to sit.

A computer that is not part of a robot may not be able to park a car, but it can run a program that simulates parking a car. Simulation is an important application area for computers, for example, weather simulations. Some things to think about are, what is algorithmic? Could you write an algorithm (thus, a computer program) for falling in love? What is it about the way brains work that may (or may not) be able to be encoded as algorithms?

Take a look at this macro for an Excel spreadsheet written using Visual Basic. The instructions will be carried out in the order in which they are written (sequence) unless otherwise indicated. The program has branching (the If … Then … ElseIf statements), looping (Do Until … Loop), and it is a subprogram called grades, as indicated by the words Sub grades. ActiveCell refers to the cell that is currently selected. The initial value for ActiveCell is the cell that is selected when the macro is started. Offset(0,1) means the cell that is in the same row (row offset = 0) and one column to the right (column offset = 1). You can use negative values for offsets to specify any cell on the spreadsheet relative to any other cell. Value refers to the value currently in the cell, Select allows you to select a new cell to be the ActiveCell.

Sub grades()

'

' grades Macro

' Macro recorded 11/4/98 by burhans

'

' This is a simple macro that translates

' a number into a letter grade, the grade

' will be written in the cell next to the

' cell containing the number

' If the grade received is an A, the word

' CONGRATS!! is written in the cell to the

' right of the letter grade

Do Until ActiveCell = ""

With ActiveCell

If .Value >= 93 Then

.Offset(0, 1).Value = "A"

.Offset(0, 2).Value = "CONGRATS!!"

ElseIf .Value >= 90 Then

.Offset(0, 1).Value = "A-"

ElseIf .Value >= 85 Then

.Offset(0, 1).Value = "B+"

ElseIf .Value >= 83 Then

.Offset(0, 1).Value = "B"

ElseIf .Value >= 80 Then

.Offset(0, 1).Value = "B-"

ElseIf .Value >= 75 Then

.Offset(0, 1).Value = "C+"

ElseIf .Value >= 73 Then

.Offset(0, 1).Value = "C"

ElseIf .Value >= 70 Then

.Offset(0, 1).Value = "C-"

ElseIf .Value >= 65 Then

.Offset(0, 1).Value = "D+"

ElseIf .Value >= 60 Then

.Offset(0, 1).Value = "D"

Else: .Offset(0, 1).Value = "F"

End If

.Offset(1, 0).Select

End With

Loop

End Sub