High Level Programming Language Constructs
Download
Report
Transcript High Level Programming Language Constructs
High Level Programming
Language Constructs
Higher Computing
Unit 2 – Software Development
What I need to know
•
•
•
Description and exemplification of the following
constructs in pseudocode and an appropriate high
level language:
string operations (concatenation and substrings),
formatting of I/O,
CASE (or equivalent multiple outcome selection)
Description and exemplification of real, integer and
boolean variables; and 1-D arrays
Description and exemplification of
procedures/subroutines/subprograms, user-defined
functions, modularity, parameter passing (in, out,
in/out), call by reference/value, local and global
variables, scope
Syntax and Semantics
The syntax of a programming language is the
rules of the language.
Each programming language has its own
syntax.
The syntax of each programming language
must be precise, or the computer will not
understand the instruction and will give you a
syntax error.
Syntax and Semantics
The semantics of a particular instruction is
what it does.
Syntax
Semantics
PRINT “Hello World!”
These examples all display
printf (“Hello World!”);
the text “Hello World”
put “Hello World!” into field “displayMessage”
displayMessage.Text = “Hello World”
Syntax and Semantics
Examples of the same syntax, but different semantics:
Syntax
Semantics
number = 10
In Visual Basic, gives
the value 10 to the
variable called number
number = 10
N.B.
In COMAL, this is part of a
condition e.g.
IF number = 10 THEN…
Syntax is the way in which you give instructions to the
computer using a software development environment.
Semantics is the meaning or effect of these instructions.
Modularity
Modularity means that when a program is
designed and written, it is divided up into smaller
sections called subprograms or sub-routines.
Subprograms may be called in any order in a
program and they may be reused many times over.
Each sub program performs a particular task
within the program.
Subprograms may be written at the same time as
the rest of the program or they may by prewritten as a library module.
Modularity
• There are two types of modules or
subprograms.
• Procedures
• Before a procedure may be used in program, it
must be defined.
• Defining a procedure gives it a name and also
allows the programmer to state which data the
procedure requires to have sent to it from the
program.
• Data is sent to a procedure using parameters.
Modularity
• When a procedure receives data, it carries
out an operation using the data and makes
results available to the program.
• These results may simply be displayed on
screen from within the procedure or they
may be passed back out of the procedure to
another procedure again using parameters.
• A procedure is said to produce an effect.
Modularity
Functions
• A function is similar to a procedure but returns a single
value to a program.
• Like a procedure, a function must be defined and given a
name before it can be used in a program.
• The name of the function is used to represent a variable
containing the value to be returned.
• A user-defined function is a function which is created
within a program rather than being already present or
pre-defined as part of the normal syntax of a
programming language.
Objects and operations
An operation is a process which is
carried out on an item of data.
An object is the item of data which is
involved in the process.
Objects and operations
Arithmetic Operations
• Arithmetic operations are calculated
involving numeric data (objects).
• The set of arithmetic operations includes
add, subtract, multiply and divide.
These operators are represented in many
programming languages by using the symbols +, -,
* and /.
Objects and Operations
Example of arithmetical operations:
Put v_num1 + v_num2 into v_total
the objects are v_Num1 and v_Num2,
the operation is add.
Objects and Operations
String operations
• Can process string data.
• String operations include joining strings,
known as concatenation, and selecting parts
of strings, known as substrings.
Objects and Operations
Example of string operations - concatenation:
Put v_firstinitial & v_surInitial into v_initials
From the initials program.
This is concatenation.
Example of string operations - substring:
Put char 1 of v_firstname into v_firstInitial
From the initials program.
This is concatenation.
Objects and Operations
Relational operations use relational operators to
compare data and produce a Boolean answer of true
of false.
The set of relational operators include:
Relational operators can be used in program control
structures such as selection and repetition
=
>
<
>=
<=
<>
equals
greater than
less than
greater than or equal to
less than or equal to
is not equal to
Objects and Operations
Examples of relational operations:
Repeat Until length(v_firstname) < 20
Objects and Operations
Logical operations
• are also called Boolean operations because they
use logical operators to produce a Boolean result
such as true or false.
• The set of logical operators includes AND, OR
and NOT.
• Logical operators are usually combined with
relational operations in program control
structures.
Objects and Operations
Example of logical operations:
Repeat Until length(v_firstname) < 20 AND
v_firstname is NOT a number
Formatting of input/output
Formatting of input/output is arranging the
appearance of the data on the screen when
input or output is taking place.
Each high level language has its own syntax
for the formatting of program input/output.
In RunRev TAB is used to format output.
local tablist = “1, 100, 200,300”
Basic features
The features of a software development
environment include:
Control;
Data Storage; &
Data Flow.
Control
There are three basic control structures
used in a procedural language.
These are:
• Sequence,
• Selection; &
• Repetition.
Sequence
Sequence simply means the order in which
things are done.
In programming the sequence in which you
give instructions to the computer is
important.
Selection
Selection means making a choice or deciding
something.
Selection is based on one or more conditions,
used together with a control structure such
as IF or CASE.
Conditions have values, they may either be
true or false.
Selection
(age = 18)
• is a simple condition
(Month >= 1) AND (Month <= 12)
• is a complex condition
Two or more simple conditions linked by
AND, OR, NOT are said to be complex.
Selection
IF age = 18 THEN
I can vote
ELSE
I can’t vote
END IF
IF Month >= 1 AND Month <= 12 THEN
process date
ELSE
display error
END IF
In each case the condition is tested and if true the appropriate
selection is made.
Selection allows the sequence of execution of program
statements to be changed.
This has the effect of increasing the number of possible
pathways that may be followed through a program.
Selection
Two control structures are commonly used to
allow selection. These are;
• IF…THEN…ELSE…END IF
• CASE…OF…WHEN…END CASE
The IF structure is suitable for use when a
single selection (or a SMALL number of
selections) is to be made.
The CASE structure allows multiple outcomes
selections to be made in a program.
• Only one of several statements is executed in a
CASE structure, depending on the data being
processed at the time.
• The other statements are not executed.
Selection
The SYNTAX of the IF structure is:
IF <condition is true> THEN
<action 1>
ELSE
<action 2>
END IF
Selection
*grade algorithm using IF*
1.
IF pupil’s test mark is greater than or equal to 80 THEN
2.
grade is grade 1
3.
ELSE
4.
IF pupil’s test mark is greater than or equal to 60 THEN
5.
grade is grade 2
6.
ELSE
7.
IF pupil’s test mark is greater than or equal to 40 THEN
8.
grade is grade 3
9.
ELSE
10.
IF pupil’s test mark is greater than or equal to 20 THEN
11.
grade is grade 4
12.
ELSE
13.
IF pupil’s test mark is greater than or equal to 10THEN
14.
grade is grade 5
15.
ELSE
16.
grade is grade 6
17.
END IF
18.
END IF
19.
END IF
20.
END IF
21.
ENDIF
Selection
From the example on the previous slide
you can see how complicated a nested
IF structure may become, when multiple
selections have to be made.
It is much more readable to use a CASE
structure.
Selection
The SYNTAX of the CASE structure is:
CASE <variable> OF
WHEN <comparison of variable
<action1>
WHEN <comparison of variable
<action2>
WHEN <comparison of variable
<action3>
WHEN <comparison of variable
<action4>
WHEN <comparison of variable
<action5>
OTHERWISE
<action6>
END CASE
is true>
is true>
is true>
is true>
is true>
Selection
When programming using a CASE structure,
it should be noted that, even if more than
one condition is true for a given variable,
only the statement(s) following the first of
the true conditions will be carried out.
Careful thought needs to be given to the
order in which the conditions appear to
ensure that your program works in the way
you intend.
Selection
*grade algorithm using CASE*
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
CASE pupil’s test mark of
WHEN greater than or equal to 80
grade is grade 1
WHEN greater than or equal to 60
grade is grade 2
WHEN greater than or equal to 40
grade is grade 3
WHEN greater than or equal to 20
grade is grade 4
WHEN greater than or equal to 10
grade is grade 5
OTHERWISE
grade is grade 6
END CASE
Repetition
Repetition means doing something
over and over again.
Repetition involves using a loop.
Loops may be either conditional or
fixed (unconditional).
Repetition
Fixed Loop
• The purpose of a fixed loop is to repeat
a set of program statements for a predetermined number of times.
repeat 5 times
…
…
end repeat
Repetition
•
Or
Conditional Loop
The purpose of a conditional loop is to manage the
situation where the number of times repetition must
take place is not known in advance.
Syntax
WHILE <condition = true>
<action 1>
END WHILE
REPEAT
<action 1>
UNTIL <condition = true>
Variables
Variables
• Data is stored in a computer’s memory in storage locations.
• Each storage location in the computer’s memory has a unique
address.
• A variable is the name that a programmer uses to identify a
storage location.
• By using a variable name a programmer can store, retrieve and
handle data without knowing what the data will be.
• Variable names, procedures and function names are sometimes also
called identifiers, because they are used to identify that particular
item.
Local and Global Variables
There are two types of variables;
• Local Variables
Local variables only come into existence when that procedure is
entered and the data that they contain is lost when the
processing of that procedure is complete.
Using local variables reduces the unplanned effects of the same
variable name being used in another part of the program and
accidentally being changed.
• Global Variables
Global variables can be used anywhere in a program but local
variables are defines only for use in one part of a program (a
subprogram – normally a function or procedure).
Global variables should only be used for data that needs to be
shared between different procedures within a program, because
they are accessible to any part of the whole program.
Scope of Variables
This is the range of statements for which a
variable is valid.
So, the scope of a local variable is the subprogram
it is used in.
This means that in a large programming project,
where a number of programmers are writing
separate subprograms, there is no need to be
concerned about using different (or similar) local
variable names, since they cannot have any effect
outside their scope.
Data Types
The data types stored by a program may be
a number, a character, a string, a date, an
array, a sound sample, a video clip or indeed,
any kind of data.
Some high level languages, such as C++, allow
programs to specify their own data typesthese are called user defined data types
Data Types
Some of the more important data types are listed
below:
• Alphanumeric/String data, may include letters, digits
and punctuation, e.g. a word in a sentence.
• Numeric data – may consist of real data and integer
data. Real data includes ALL numbers, both whole and
fractional. Integer data is a subset of real data which
includes only whole numbers, either positive or negative.
• Boolean or logical data – may only have two values, true
or false. Boolean data is used in a program’s control
structures, i.e. selection and repetition
Data Structures - Arrays
• Arrays – A set of data items of the same type
grouped together using a single variable name is
called an array. Each part of an array is
identified by the variable name and an element
number or index.
• An array of names may look like this;
name(1) - - John
name(2) - - Helen
name(3) - - Peter
name(4) - - Mary
• This array has four parts. The third element of
this array contains ‘Peter’.
Data Structures - Arrays
Arrays which have one number as their subscript
are called one-dimensional arrays.
When programming using arrays, it is necessary to
declare the name of the array and its size at the
start of the program, so that the computer may set
aside the correct amount of memory space for the
array.
• E.g. to set aside space for an array called apples with a
size 15 in BASIC, Pascal, C and runrev:
DIM apples% (15);
VAR apples : array [1..15] of integer;
int apples [15];
Local apples
Data Structure - Arrays
1.
2.
3.
4.
5.
6.
7.
8.
Set array counter to zero
Set aside space for ten pupils names in the array name [ ]
Set aside space for ten pupils marks in the array mark [ ]
REPEAT
add one to array counter
put value into name array [counter]
put value into mark array [counter]
UNTIL end of data is reached
Data Flow
Data flow or the movement of data between
subprograms is implemented by using parameters.
Data structures (such as variables) which are only
passed into subprograms to be used are known as
in parameters.
Variables which are only passed into subprogram
and are changed or updated are referred to as
in/out parameters.
Variables which are only passed out of
subprograms are known as out parameters.
Data Flow
A parameter is information about a data
item being supplied to a subprogram
(function or procedure) when it is called
into use.
When the subprogram is used, the calling
program must pass parameters to it- this
is called parameter passing.
Data Flow
Actual and Formal Parameters
The parameter that contains the actual
data that is being passed is called the
Actual Parameter.
The parameter into which the data is
being passed into is called the Formal
Parameter.
Data Flow
Value and reference parameters
• Parameters can be passed either by value or by reference.
• The method used depends upon whether the parameter is going in to
or in/out of a procedure (or function).
• Parameters are passed by value when a parameter is passed into a
procedure but does not require to be passed out again (to be used in
another procedure).
• Parameters are passed by reference when the parameter required
to be passed into a procedure, updated and then passed out of the
procedure again.
• Once exception to this rule exists for the array data structure only.
When an array is being passed as a parameter, it is always passed by
reference.
Data Flow
It is necessary to describe the data flow in a
diagram in order to work out how the
parameters should be passed between the
main program and any subprograms and
between the subprograms themselves.