music

# sequence

## summary

The most basic of the three control structures is the sequence.

## free computer programming text book project

If you like the idea of this project,

# sequence

The most basic of the three control structures is the sequence.

A sequnce is a series of steps. Classic examples include, calculations, manipulations of data, and running subroutines or functions.

One very important note is that in structured programming, a sequence must have only one entry point and only one exit point. While structured programming does allow the use of three different control structures, a group or block of program commands must always have exactly one entry and exactly one exit.

A common example of a sequence is finding an average of several numbers. The following example assumes exactly five total numbers being added.

1.a. set the running subtotal to zero (an example of initialization)
1.b. set the count of numbers to zero (an example of initialization)
2.a. add the first number to the running subtotal
2.b. add one to the count of numbers (an example of incrementing)
3.a. add the second number to the running subtotal
3.b. again increment (add one to) the count of numbers
4.a. add the third number to the running subtotal
4.b. increment (add one to) the count of numbers
5.a. add the fourth number to the running subtotal
5.b. increment the count of numbers
6.a. add the fifth number to the running subtotal
6.b. increment the count of numbers
7. divide the running subtotal by the count of numbers

The following shows the example with actual numbers:

step next
number
running
subtotal
count of
numbers
1a N/A 0 -
1b N/A 0 0
2a 6 0+6=6 0
2b - 6 0+1=1
3a 2 6+2=8 1
3b - 8 1+1=2
4a 5 8+5=13 2
4b - 13 2+1=3
5a 9 13+9=22 3
5b - 22 3+1=4
6a 3 22+3=25 4
6b - 25 4+1=5
25 / 5 = 5 the average is 5

The most simple or elementary steps of a sequence or things like the simple computations in the example above or other basic computer operations. other examples would be making a copy of a piece of data (if this was a formal discussion, I’d call it a piece of datum), a single logical operation (AND, OR, NOT, etc, if you recognize those), a rotation or shift, a single text substittion, concatenating text, or the deletion, insertion, or modification of a piece of data into a data base, list, tree, or other data structure.

If the programming language allows combining several operations into a single instructure, the instruction is often viewed as a signle sequential step. For example, the following computation includes two multiplications, an exponentiation, an addition, and a subtraction, but is viewed collectively as a single sequential step (note that because this is an informal discussion, none of these examples are intended to be from any actual programming language):

x = 3a2 + 2b - c
or
x := 3*a**2 + 2*b - c;

In computer programming, sequential steps can be many things other than arithmetic cmputations.

The grouping of a sequence of steps (as well as a grouping with any combination of the three basic control structures) is often referred to as a block of code. The block of code may be either a concept or may be an actual programming structure in the programming language. A block of code as a single group can be viewed as a sequential step, even if the block internally contains other control structures.

A much more powerful version of a sequential step is a subroutine or function. Note that in some programming languages there is no disticntion between a subroutine and a function, while in other programming languages there is a strong difference between the two. We won’t get into those details here.

The basic idea of a function is similar to the basic idea of a function in algebra. A function takes a value (or sometimes more than one value) as an input and produces a single answer or value as a result. Common examples include trignometric functions, such as sine, cosine, and tangent, which might appear as follows:

x = cos(a) + sin(b) + tan(c)

While almost all modern computers have a single hardware instruction for trignometric functions, most functions have to be written by a programmer, whether they are built-in (supplied by the compiler) or user defined (written by the programmer).

An example of a function that would have several internal (hidden) steps, as well as more than one input value, would be finding the greatest common denominator (see example below). Even though there are several steps hidden inside the function, the function is treated as a single sequential step.

x := gcd(a,b);

A subroutine is also a set of instructions that are hidden away and treated as one group and therefore a single sequential step. For languages that distinguish between subroutines and functions, a subroutine normally doesn’t return a result, while a function normally does return a result.

A subroutine can be used twhen the same piece of coding will appear in many different places in a prgram. Rather than writing it out each time individually, the subroutine would be written once and a call to the subroutine would be inserted everywhere you would have otherwise had to write it out repeatedly. This can make the program much easier to maintain, because it is much easier to fix, change, or update one subroutine than to search a giant program listing for every place the same little piece of coding appears.

A subroutine can also be used to assist in creating a top-down structured program, increasing readability.

The use of subroutines as single sequential steps allow for decomposing a complex program from top-down, a basic method in modern structured programming. Consider the following example:

Initialize;
OpenDataFiles;
CloseFiles;
PerformCalculations;
SortData;
PrepareReport;
PrintReport;
Finish;

Even a non-programmer could probably figure out what is going on and maybe even give useful feedback on important steps that might be missing. By hiding all the details of each major step, it is easier for the programmer to visualize the structure of the program, making it more likely that the programmer will create a bug-free program that other programmers can easily modify for changing circumstances for years or decades into the future.

Using the idea of functional decomposition, the programmer might also use this same approach of a sequence of subroutines at lower levels. For example, consider the subroutine PrepareReport, which might be:

Initialize;
PrepareMainSection;
PrepareExceptionSection;
PrepareReportFooter;
Finish;

Obviously each of these sequential steps is its own subroutine with its own hidden steps.

Another important concept is the utility. There are some functions or subroutines that are so commonly used that they are considered to be utility routines or utlity function. The built-in functions of a programming language are a good example. A group of programmers will often share a library of commonly used utility functions and subroutines.

“21 The body of a program unit generally contains two parts: a declarative part, which defines the logical entities to be used in the program unit, and a sequence of statements, which defines the execution of the program unit.” —:Ada-Europe’s Ada Reference Manual: Introduction: Language Summary See legal information

“23 The sequence of statements describes a sequence of actions that are to be performed. The statements are executed in succession (unless a transfer of control causes execution to continue from another place).” —:Ada-Europe’s Ada Reference Manual: Introduction: Language Summary See legal information

# free music player coding example

Coding example: I am making heavily documented and explained open source code for a method to play music for free — almost any song, no subscription fees, no download costs, no advertisements, all completely legal. This is done by building a front-end to YouTube (which checks the copyright permissions for you).

View music player in action: www.musicinpublic.com/.

Create your own copy from the original source code/ (presented for learning programming).

## view text bookHTML file

Because I no longer have the computer and software to make PDFs, the book is available as an HTML file, which you can convert into a PDF.

 previous page next page
 Tweets by @osdata

## free computer programming text book project

Building a free downloadable text book on computer programming for university, college, community college, and high school classes in computer programming.

If you like the idea of this project,

send donations to:
Milo
PO Box 1361
Tustin, California 92781

Supporting the entire project:

If you have a business or organization that can support the entire cost of this project, please contact Pr Ntr Kmt (my church)

Some or all of the material on this web page appears in the

This web site handcrafted on Macintosh computers using Tom Bender’s Tex-Edit Plus and served using FreeBSD .

†UNIX used as a generic term unless specifically used as a trademark (such as in the phrase “UNIX certified”). UNIX is a registered trademark in the United States and other countries, licensed exclusively through X/Open Company Ltd.

Names and logos of various OSs are trademarks of their respective owners.

Created: November 14, 2010

Last Updated: November 14, 2010