0% found this document useful (0 votes)
2 views

Course Note 5

The document explains sequential programming structures where operations are executed one after another, contrasting sequential computers with parallel computers. It covers key programming concepts including storage, data declaration, input/output operations, and control structures such as sequence, decision, and repetition. Additionally, it discusses the importance of syntax and semantics in programming languages, as well as the role of variables and data types.

Uploaded by

jalalademola
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Course Note 5

The document explains sequential programming structures where operations are executed one after another, contrasting sequential computers with parallel computers. It covers key programming concepts including storage, data declaration, input/output operations, and control structures such as sequence, decision, and repetition. Additionally, it discusses the importance of syntax and semantics in programming languages, as well as the role of variables and data types.

Uploaded by

jalalademola
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

4.

SEQUENTIAL PROGRAM STRUCTURES


Sequential programming is when the algorithm to be solved consists of operations one after the
other. A sequential program explicitly waits in-line, for the expected events in various places in
the execution path. E.G : First cooking dinner, then eating, and then washing the dishes is one
sequence. If otherwise it is a much less sensible sequence.
A sequence of instructions on how you go to work:
to go to work:
get dressed
eat breakfast
catch the bus

Based on the number of microprocessors, computers can be classified into Sequential


computers and Parallel computers. Most of the computers we see today are sequential
computers where in any task is completed sequentially instruction after instruction from the
beginning to the end. The parallel computer is relatively fast. New types of computers that use
a large number of processors. The processors perform different tasks independently and
simultaneously thus improving the speed of execution of complex programs dramatically.
Sequential program structures are forms in which program components are constructed,
organized and interrelated. In learning a programming language, there is need to learn about two
important aspects of the language: its syntax and semantics.
The syntax of a language is the grammatical rules that govern the ways in which words,
symbols, expressions and statements may be formed and combined. The semantics of a language
are the rules that govern its meaning. In the case of computer language, meaning is defined in
terms of what happens when the program is executed. The main features of the computer in
programming language are storage, input and output, operation on data, and control.

4.1 STORAGE
Computers require a set of instructions and data to be stored in their memory to perform a
specific task. In programming languages, data are identified by name rather than by their location
addresses in main storage. The names that associate stored data values are called identifiers.
An identifier: is the name by which the data value may be identified. It is a constant if it is
always associated with the same data value and it is a variable if its associated data value is
allowed to change. Names are literals or identifiers. When names or letters are used literally
they are called literals, and they are distinguished from identifiers by placing them within
quotation marks. So the instruction PRINT “N” means print the letter N, meaning print the
value associated with N.

4.2 DATA DECLARATION


A variable is a symbolic name assigned to a data item by the programmer that can change from
time to time. A variable is usually given a name by the programmer. The variable must be
declared that is to state its data type and its Value.
A variable name does not have an associated value until it has been assigned one.
A data type is a classification of data, which can store a specific type of information. Data types
are primarily used in computer programming in which variables are created to store data. The
act of defining a variable is called data declaration.
Declarations provide information about the name and type of data objects needed during
program execution. Type of data types: integer, real, Boolean, and character or string.
There are two types of declaration, implicit declaration and explicit declaration.
Implicit declaration or default declaration are those declaration which is done by compiler
when no explicit declaration or user defined declaration is mentioned. For example in 'Perl'
compiler implicitly understand that:
$abc ='astring' is a string variable and
$abc=7; is an integer variable.
In explicit declaration, user explicitly defined the variable type. For example:
Float A, B;
In this example it specifies that it is of float type variable which has name A & B.

Purpose of Declarations:
i. Choice of storage representation: Translator determine the best storage representation of
data types that is why it needs to know primarily the information of data type and
attribute of a data object.
ii. Storage Management: It helps the computer to make best use of memory for data object
by providing its information so that computer can allocate the optimum size of memory
for the data.

4.3 INPUT AND OUTPUT


Programming languages have special functions for dealing with input and output. Common names for
these functions are input, read, get, accept, output, write, print, put and display.

4.4 OPERATIONS ON DATA


Operations are expressed in the form of statements. The simplest statement is the
assignment statement. It consists of a variable name, followed by the assignment operator
(=), followed by some sort of expression. The assignment operation has the form:
variable = expression
The assignment operation is used to assign a name to a value. Thus it is used whenever
there is need to keep track of a value that is needed later. Some typical uses include:
• initialize a variable ( count = 0 )

• increment/decrement a counter ( count = count + 1 )

• accumulate values ( sum = sum + item )

• capture the result of a computation ( y = 3*x + 4 )

• swap two values ( t = x; x = y; y = t )

The assignment operator is not commute i.e. x = e is not the same as e = x.

Declaration statement int a; variables


int b;
Assignment Statement a = 1234; literal
b = 99;
combined declaration c = a + b;
and assignment statement int
4.5 CONTROL
In the problem-solving phase of computer programming, you will be designing
algorithms. These algorithms can be designed though the use of flowcharts or
pseudocode. To implement an algorithm, it should be described in an understandable
form. The descriptions are called constructs. These control structures a grouped into
three constructs namely the sequence structure, the decision structure or election
structure and the repetition or iteration structure
4.5.1 The sequence structure
The first type of control structures is called the sequence structure. It is the most
elementary structure. The sequence structure is a case where the steps in an algorithm are
constructed in such a way that, no condition step is required.
Example 1: Design an algorithm for finding the average of six numbers, and the sum of
the numbers is given. The pseudocode will be as follows:
Start
Get the sum
Average = sum / 6
Output the average
Stop
The corresponding flowchart will appear as follows:
4.5.2 Decision Structure or Selection Structure
The decision structure also known as a selection structure, is case where in the algorithm,
one has to make a choice of two alternatives by making decision depending on a given
condition. i.e there are two or more alternatives to choose from. This structure can be
illustrated in a flowchart as follows:

In pseudocode form:
If condition is true
Then do task A
else
Do Task-B
In this example, the condition is evaluated, if the condition is true Task-A is evaluated
and if it is false, then Task-B is executed
A variation of the construct of the above figure is shown below

The above structure implies the following:


If condition is true then Do Task-A

The if statement together with logical operators will be used to test for true or false as
shown below. If a = b
print “a = b” The action is only taken when the test is true.

The logical operators used in pseudo-codes are


= is equal to > is greater than < is less than >= is greater than or equal
<= is less than or equal <> is not eaqual to

Example 5: The program is to input an examination mark and test it for the award of a grade.
The mark is a whole number between 1 and 100. Grades are awarded according to the following
criteria:
>= 80 Distinction
>= 60 Merit
>= 40 Pass
< 39 fail
The pseudo-code is
Use variables: mark of type integer
If mark >= 80 display “distinction”
If mark >= 60 and mark < 80 display “merit”
If mark >= 40 and mark < 60 display “pass”
If mark < 39 display “fail”

4.5.3 Repetition or Iteration Structure


A third structure causes the certain steps to be repeated.
The Repetition structure can be implemented using
• Repeat Until Loop
• The While Loop
• The For Loop
Any program instruction that repeats some statement or sequence of statements a number
of times is called an iteration or a loop. There three constructs for iterations or loops in
our pseudo- code.
The Repeat Until loop.
The syntax is
REPEAT
A statement or block of statements
UNTIL a true condition

Example 8: A program segment repeatedly asks for entry of a number in the range 1 to
100 until a valid number is entered.
REPEAT
DISPLAY “Enter a number between 1 and 100” ACCEPT number
UNTIL number < 1 OR number > 100

You might also like