High Level Programming Language Constructs Higher Computing Unit 2 – Software Development.

48
High Level Programming High Level Programming Language Constructs Language Constructs Higher Computing Higher Computing Unit 2 – Software Unit 2 – Software Development Development

Transcript of High Level Programming Language Constructs Higher Computing Unit 2 – Software Development.

High Level Programming High Level Programming Language ConstructsLanguage Constructs

Higher ComputingHigher Computing

Unit 2 – Software DevelopmentUnit 2 – Software Development

What I need to knowWhat I need to know Description and exemplification of the following Description and exemplification of the following

constructs in pseudocode and an appropriate constructs in pseudocode and an appropriate high level language: high level language:

• string operations (concatenation and substrings), string operations (concatenation and substrings), • formatting of I/O, formatting of I/O, • CASE (or equivalent multiple outcome selection) CASE (or equivalent multiple outcome selection)

Description and exemplification of real, integer Description and exemplification of real, integer and boolean variables; and 1-D arrays and boolean variables; and 1-D arrays

Description and exemplification of Description and exemplification of procedures/subroutines/subprograms, user-procedures/subroutines/subprograms, user-defined functions, modularity, parameter defined functions, modularity, parameter passing (in, out, in/out), call by reference/value, passing (in, out, in/out), call by reference/value, local and global variables, scope local and global variables, scope

Syntax and SemanticsSyntax and Semantics

The The syntax syntax of a programming language is of a programming language is the rules of the language.the rules of the language.

Each programming language has its own Each programming language has its own syntax.syntax.

The syntax of each programming language The syntax of each programming language must be precise, or the computer will not must be precise, or the computer will not understand the instruction and will give understand the instruction and will give you a you a syntax errorsyntax error..

Syntax and SemanticsSyntax and Semantics

The The semanticssemantics of a particular instruction of a particular instruction is what it does.is what it does.

SyntaxSyntax SemanticsSemantics

PRINT “Hello World!”PRINT “Hello World!” These examples all display These examples all display

printf (“Hello World!”);printf (“Hello World!”); the text “Hello the text “Hello World”World”

put “Hello World!” into field “displayMessage”put “Hello World!” into field “displayMessage”

displayMessage.Text = “Hello World”displayMessage.Text = “Hello World”

Syntax and SemanticsSyntax and Semantics

Examples of the same syntax, but different semantics:Examples of the same syntax, but different semantics:SyntaxSyntax SemanticsSemanticsnumber = 10number = 10 In Visual Basic, gives In Visual Basic, gives

the value 10 to the the value 10 to the variable called numbervariable called number

number = 10number = 10 In COMAL, this is part of a In COMAL, this is part of a condition e.g.condition e.g.IF number = 10 THEN…IF number = 10 THEN…

N.B. N.B. Syntax is the way in which you give instructions to the Syntax is the way in which you give instructions to the computer using a software development environment. computer using a software development environment. Semantics is the meaning or effect of these Semantics is the meaning or effect of these

instructions.instructions.

ModularityModularity Modularity means that when a program is Modularity means that when a program is

designed and written, it is divided up into smaller designed and written, it is divided up into smaller sections called sections called subprograms subprograms or or sub-routinessub-routines. .

Subprograms may be called in any order in a Subprograms may be called in any order in a program and they may be reused many times program and they may be reused many times over.over.

Each sub program performs a particular task Each sub program performs a particular task within the program.within the program.

Subprograms may be written at the same time as Subprograms may be written at the same time as the rest of the program or they may by pre-the rest of the program or they may by pre-written as a library module.written as a library module.

ModularityModularity• There are two types of modules or There are two types of modules or

subprograms. subprograms.

• ProceduresProcedures• Before a procedure may be used in program, Before a procedure may be used in program,

it must be it must be defineddefined..

• Defining a procedure gives it a name and also Defining a procedure gives it a name and also allows the programmer to state which data allows the programmer to state which data the procedure requires to have sent to it from the procedure requires to have sent to it from the program.the program.

• Data is sent to a procedure using Data is sent to a procedure using parameters. parameters.

ModularityModularity• When a procedure receives data, it carries When a procedure receives data, it carries

out an operation using the data and makes out an operation using the data and makes results available to the program.results available to the program.

• These results may simply be displayed on These results may simply be displayed on screen from within the procedure or they screen from within the procedure or they may be passed back out of the procedure to may be passed back out of the procedure to another procedure again using parameters.another procedure again using parameters.

• A procedure is said to A procedure is said to produce an effectproduce an effect..

ModularityModularity FunctionsFunctions

• A function is similar to a procedure but A function is similar to a procedure but returns a returns a single valuesingle value to a program. to a program.

• Like a procedure, a function must be defined and Like a procedure, a function must be defined and given a name before it can be used in a program. given a name before it can be used in a program.

• The name of the function is used to represent a The name of the function is used to represent a variable containing the value to be returned.variable containing the value to be returned.

• A A user-defined user-defined function is a function which is function is a function which is created within a program rather than being already created within a program rather than being already present or present or pre-definedpre-defined as part of the normal syntax as part of the normal syntax of a programming language. of a programming language.

Objects and operationsObjects and operations

An An operationoperation is a process which is is a process which is carried out on an item of data.carried out on an item of data.

An An objectobject is the item of data which is the item of data which is involved in the process.is involved in the process.

Objects and operationsObjects and operations

Arithmetic OperationsArithmetic Operations• Arithmetic operations are calculated Arithmetic operations are calculated

involving numeric data (objects).involving numeric data (objects).

• The set of The set of arithmetic operationsarithmetic operations includes add, subtract, multiply and includes add, subtract, multiply and divide.divide.

These operators are represented in many These operators are represented in many programming languages by using the programming languages by using the symbols +, -, * and /.symbols +, -, * and /.

Objects and OperationsObjects and Operations

Example of arithmetical operations:Example of arithmetical operations:

Put v_num1 + v_num2 into v_total Put v_num1 + v_num2 into v_total

the objects are v_Num1 and v_Num2, the objects are v_Num1 and v_Num2,

the operation is add.the operation is add.

Objects and OperationsObjects and Operations

String operations String operations • Can process string data. Can process string data.

• String operations include joining strings, String operations include joining strings, known as known as concatenationconcatenation, and selecting , and selecting parts of strings, known as parts of strings, known as substringssubstrings. .

Objects and OperationsObjects and Operations

Example of string operations - Example of string operations - concatenation:concatenation:

Put v_firstinitial & v_surInitial into v_initialsPut v_firstinitial & v_surInitial into v_initialsFrom the initials program. From the initials program.

This is This is concatenationconcatenation. .

Example of string operations - substring:Example of string operations - substring:

Put char 1 of v_firstname into v_firstInitialPut char 1 of v_firstname into v_firstInitialFrom the initials program. From the initials program.

This is This is concatenationconcatenation..

Objects and OperationsObjects and Operations Relational operations use Relational operations use relational relational

operators operators to compare data and produce a to compare data and produce a Boolean answer of true of false. Boolean answer of true of false.

The set of relational operators include:The set of relational operators include:== equalsequals>> greater thangreater than<< less thanless than>=>= greater than or equal togreater than or equal to<=<= less thanless than or equal toor equal to<><> is notis not equal toequal to

Relational operators can be used in program Relational operators can be used in program control structures such as selection and control structures such as selection and repetitionrepetition

Objects and OperationsObjects and Operations

Examples of relational operations:Examples of relational operations:

Repeat Until length(v_firstname) < 20Repeat Until length(v_firstname) < 20

Objects and OperationsObjects and Operations

Logical operations Logical operations • are also called are also called Boolean operationsBoolean operations because because

they use logical operators to produce a they use logical operators to produce a Boolean result such as Boolean result such as true true oror false false..

• The set of The set of logical operators logical operators includes includes AND, AND, OROR and and NOTNOT. .

• Logical operators are usually combined with Logical operators are usually combined with relational operations in program control relational operations in program control structures.structures.

Objects and OperationsObjects and Operations

Example of logical operations:Example of logical operations:

Repeat Until length(v_firstname) < 20 AND Repeat Until length(v_firstname) < 20 AND v_firstname is NOT a numberv_firstname is NOT a number

Formatting of input/outputFormatting of input/output

Formatting of input/output is arranging Formatting of input/output is arranging the appearance of the data on the screen the appearance of the data on the screen when input or output is taking place.when input or output is taking place.

Each high level language has its own Each high level language has its own

syntax for the formatting of program syntax for the formatting of program input/output.input/output.

In RunRev In RunRev TAB TAB is used to format output. is used to format output.

local tablist = “1, 100, 200,300”local tablist = “1, 100, 200,300”

Basic featuresBasic features The features of a software development The features of a software development

environment include:environment include:

Control;Control;

Data Storage; &Data Storage; &

Data Flow.Data Flow.

ControlControl

There are three basic control structures There are three basic control structures used in a procedural language. used in a procedural language.

These are:These are:

• SequenceSequence, ,

• Selection; &Selection; &

• RepetitionRepetition..

SequenceSequence

Sequence simply means the order in Sequence simply means the order in which things are done.which things are done.

In programming the sequence in which In programming the sequence in which you give instructions to the computer is you give instructions to the computer is important.important.

SelectionSelection

Selection means making a choice or Selection means making a choice or deciding something.deciding something.

Selection is based on one or more Selection is based on one or more conditionsconditions, used together with a control , used together with a control structure such as structure such as IFIF or or CASECASE..

Conditions have values, they may either Conditions have values, they may either be be truetrue or or falsefalse..

SelectionSelection

(age = 18)(age = 18) • is a is a simple conditionsimple condition

(Month >= 1) AND (Month <= 12)(Month >= 1) AND (Month <= 12) • is a is a complex conditioncomplex condition

Two or more simple conditions linked Two or more simple conditions linked by AND, OR, NOT are said to be by AND, OR, NOT are said to be complex.complex.

SelectionSelectionIF age = 18 THENIF age = 18 THEN

I can voteI can voteELSEELSE

I can’t voteI can’t voteEND IFEND IF

In each case the condition is tested and if true the In each case the condition is tested and if true the appropriate selection is made.appropriate selection is made.

Selection allows the sequence of execution of program Selection allows the sequence of execution of program statements to be changed.statements to be changed.

This has the effect of increasing the number of possible This has the effect of increasing the number of possible pathways that may be followed through a program.pathways that may be followed through a program.

IF Month >= 1 AND Month <= 12 THENIF Month >= 1 AND Month <= 12 THENprocess dateprocess date

ELSEELSEdisplay errordisplay error

END IFEND IF

SelectionSelection Two control structures are commonly used Two control structures are commonly used

to allow selection. These are;to allow selection. These are;• IF…THEN…ELSE…END IF IF…THEN…ELSE…END IF • CASE…OF…WHEN…END CASECASE…OF…WHEN…END CASE

The IF structure is suitable for use when a The IF structure is suitable for use when a single selection (or a SMALL number of single selection (or a SMALL number of selections) is to be made.selections) is to be made.

The The CASECASE structure allows structure allows multiple multiple outcomes selectionsoutcomes selections to be made in a to be made in a program.program.• Only one of several statements is executed in a Only one of several statements is executed in a

CASE structure, depending on the data being CASE structure, depending on the data being processed at the time. processed at the time.

• The other statements are not executed.The other statements are not executed.

SelectionSelection

The SYNTAX of the IF structure is:The SYNTAX of the IF structure is:

IF <condition is true> THENIF <condition is true> THEN

<action 1><action 1>

ELSEELSE

<action 2><action 2>

END IFEND IF

SelectionSelection*grade algorithm using IF**grade algorithm using IF*1.1. IF pupil’s test mark is greater than or equal to 80 THEN IF pupil’s test mark is greater than or equal to 80 THEN 2.2. grade is grade 1grade is grade 13.3. ELSEELSE4.4. IF pupil’s test mark is greater than or equal to 60 THEN IF pupil’s test mark is greater than or equal to 60 THEN 5.5. grade is grade 2grade is grade 26.6. ELSEELSE7.7. IF pupil’s test mark is greater than or equal to 40 THEN IF pupil’s test mark is greater than or equal to 40 THEN 8.8. grade is grade 3grade is grade 39.9. ELSEELSE10.10. IF pupil’s test mark is greater than or equal to 20 THEN IF pupil’s test mark is greater than or equal to 20 THEN 11.11. grade is grade 4grade is grade 412.12. ELSEELSE13.13. IF pupil’s test mark is greater than or equal to IF pupil’s test mark is greater than or equal to

10THEN 10THEN 14.14. grade is grade 5grade is grade 515.15. ELSEELSE16.16. grade is grade 6grade is grade 617.17. END IFEND IF18.18. END IFEND IF19.19. END IFEND IF20.20. END IFEND IF21.21. ENDIFENDIF

SelectionSelection

From the example on the previous From the example on the previous slide you can see how complicated a slide you can see how complicated a nested IF structure may become, nested IF structure may become, when when multiple selections multiple selections have to have to be made.be made.

It is much more readable to use a It is much more readable to use a CASE structure.CASE structure.

SelectionSelection The SYNTAX of the The SYNTAX of the CASECASE structure is: structure is:

CASE <variable> OFCASE <variable> OFWHEN <comparison of variable is true>WHEN <comparison of variable is true>

<action1><action1>WHEN <comparison of variable is true>WHEN <comparison of variable is true>

<action2><action2>WHEN <comparison of variable is true>WHEN <comparison of variable is true>

<action3><action3>WHEN <comparison of variable is true>WHEN <comparison of variable is true>

<action4><action4>WHEN <comparison of variable is true>WHEN <comparison of variable is true>

<action5><action5>OTHERWISEOTHERWISE

<action6><action6>END CASEEND CASE

SelectionSelection When programming using a CASE When programming using a CASE

structure, it should be noted that, even if structure, it should be noted that, even if more than one condition is true for a more than one condition is true for a given variable, only the statement(s) given variable, only the statement(s) following the first of the true conditions following the first of the true conditions will be carried out.will be carried out.

Careful thought needs to be given to the Careful thought needs to be given to the order in which the conditions appear to order in which the conditions appear to ensure that your program works in the ensure that your program works in the way you intend.way you intend.

SelectionSelection*grade algorithm using CASE**grade algorithm using CASE*

1.1. CASE pupil’s test mark ofCASE pupil’s test mark of2.2. WHEN greater than or equal to 80WHEN greater than or equal to 803.3. grade is grade 1grade is grade 14.4. WHEN greater than or equal to 60WHEN greater than or equal to 605.5. grade is grade 2grade is grade 26.6. WHEN greater than or equal to 40WHEN greater than or equal to 407.7. grade is grade 3grade is grade 38.8. WHEN greater than or equal to 20WHEN greater than or equal to 209.9. grade is grade 4grade is grade 410.10. WHEN greater than or equal to 10WHEN greater than or equal to 1011.11. grade is grade 5grade is grade 512.12. OTHERWISEOTHERWISE13.13. grade is grade 6grade is grade 614.14. END CASEEND CASE

RepetitionRepetition Repetition means doing Repetition means doing

something over and over again.something over and over again.

Repetition involves using a Repetition involves using a looploop..

Loops may be either Loops may be either conditionalconditional or or fixedfixed ( (unconditionalunconditional).).

RepetitionRepetition Fixed LoopFixed Loop

• The purpose of a fixed loop is to The purpose of a fixed loop is to repeat a set of program statements repeat a set of program statements for a pre-determined number of for a pre-determined number of times.times.

repeat 5 timesrepeat 5 times…………

end repeatend repeat

RepetitionRepetition Conditional LoopConditional Loop

• The purpose of a conditional loop is to manage The purpose of a conditional loop is to manage the situation where the number of times repetition the situation where the number of times repetition must take place is not known in advance.must take place is not known in advance.

SyntaxSyntaxWHILE <condition = true> WHILE <condition = true>

<action 1><action 1>END WHILEEND WHILE

OrOrREPEATREPEAT

<action 1><action 1>UNTIL <condition = true>UNTIL <condition = true>

VariablesVariables VariablesVariables

• Data is stored in a computer’s memory in storage locations. Data is stored in a computer’s memory in storage locations.

• Each storage location in the computer’s memory has a unique Each storage location in the computer’s memory has a unique address.address.

• A A variable variable is the name that a programmer uses to identify a is the name that a programmer uses to identify a storage location.storage location.

• By using a variable name a programmer can store, retrieve By using a variable name a programmer can store, retrieve and handle data without knowing what the data will be.and handle data without knowing what the data will be.

• Variable names, procedures and function names are Variable names, procedures and function names are sometimes also called sometimes also called identifiersidentifiers, because they are used to , because they are used to identify that particular item.identify that particular item.

Local and Global VariablesLocal and Global Variables There are two types of variables;There are two types of variables;

• Local VariablesLocal Variables Local variables only come into existence when that Local variables only come into existence when that

procedure is entered and the data that they contain is lost procedure is entered and the data that they contain is lost when the processing of that procedure is complete.when the processing of that procedure is complete.

Using local variables reduces the unplanned effects of the Using local variables reduces the unplanned effects of the same variable name being used in another part of the same variable name being used in another part of the program and accidentally being changed.program and accidentally being changed.

• Global VariablesGlobal Variables Global variables can be used anywhere in a program but Global variables can be used anywhere in a program but

local variables are defines only for use in one part of a local variables are defines only for use in one part of a program (a subprogram – normally a function or procedure).program (a subprogram – normally a function or procedure).

Global variables should only be used for data that needs to Global variables should only be used for data that needs to be shared between different procedures within a program, be shared between different procedures within a program, because they are accessible to any part of the whole because they are accessible to any part of the whole program.program.

Scope of VariablesScope of Variables This is the range of statements for which a This is the range of statements for which a

variable is valid.variable is valid.

So, the scope of a local variable is the So, the scope of a local variable is the subprogram it is used in.subprogram it is used in.

This means that in a large programming This means that in a large programming project, where a number of programmers are project, where a number of programmers are writing separate subprograms, there is no writing separate subprograms, there is no need to be concerned about using different (or need to be concerned about using different (or similar) local variable names, since they similar) local variable names, since they cannot have any effect outside their scope.cannot have any effect outside their scope.

Data TypesData Types

The data types stored by a program may The data types stored by a program may be a number, a character, a string, a be a number, a character, a string, a date, an array, a sound sample, a video date, an array, a sound sample, a video clip or indeed, any kind of data.clip or indeed, any kind of data.

Some high level languages, such as C+Some high level languages, such as C++, allow programs to specify their own +, allow programs to specify their own data types- these are called user defined data types- these are called user defined data typesdata types

Data TypesData Types

Some of the more important data types are Some of the more important data types are listed below:listed below:• Alphanumeric/String dataAlphanumeric/String data, may include letters, , may include letters,

digits and punctuation, e.g. a word in a sentence.digits and punctuation, e.g. a word in a sentence.

• Numeric dataNumeric data – may consist of – may consist of real datareal data and and integer datainteger data. Real data includes ALL numbers, both . Real data includes ALL numbers, both whole and fractional. Integer data is a subset of real whole and fractional. Integer data is a subset of real data which includes only whole numbers, either data which includes only whole numbers, either positive or negative.positive or negative.

• BooleanBoolean or or logical datalogical data – may only have two values, – may only have two values, true true or or falsefalse. Boolean data is used in a program’s . Boolean data is used in a program’s control structures, i.e. selection and repetitioncontrol structures, i.e. selection and repetition

Data Structures - ArraysData Structures - Arrays• ArraysArrays – A set of data items – A set of data items of the same of the same

type type grouped together using a single grouped together using a single variable name is called an variable name is called an arrayarray. Each part . Each part of an array is identified by the of an array is identified by the variable variable namename and an and an element number element number or or indexindex..

• An array of names may look like this;An array of names may look like this;name(1) - - name(1) - - JohnJohnname(2) - - Helenname(2) - - Helenname(3) - - Petername(3) - - Petername(4) - - Maryname(4) - - Mary

• This array has four parts. The third element This array has four parts. The third element of this array contains ‘Peter’.of this array contains ‘Peter’.

Data Structures - ArraysData Structures - Arrays Arrays which have one number as their Arrays which have one number as their

subscript are called subscript are called one-dimensional arraysone-dimensional arrays.. When programming using arrays, it is When programming using arrays, it is

necessary to declare the name of the array and necessary to declare the name of the array and its size at the start of the program, so that the its size at the start of the program, so that the computer may set aside the correct amount of computer may set aside the correct amount of memory space for the array.memory space for the array.

• E.g. to set aside space for an array called apples with E.g. to set aside space for an array called apples with a size 15 in BASIC, Pascal, C and runrev:a size 15 in BASIC, Pascal, C and runrev:

DIM apples% (15); DIM apples% (15); VAR apples : array [1..15] of integer; VAR apples : array [1..15] of integer; int apples [15];int apples [15]; Local applesLocal apples

Data Structure - ArraysData Structure - Arrays

1.1. Set array counter to zeroSet array counter to zero2.2. Set aside space for ten pupils names in the array Set aside space for ten pupils names in the array

name [ ]name [ ]3.3. Set aside space for ten pupils marks in the array Set aside space for ten pupils marks in the array

mark [ ]mark [ ]4.4. REPEATREPEAT5.5. add one to array counteradd one to array counter6.6. put value into put value into name name array [counter]array [counter]7.7. put value into put value into mark mark array [counter]array [counter]8.8. UNTIL UNTIL end of dataend of data is reached is reached

Data FlowData Flow

Data flow or the movement of data between Data flow or the movement of data between subprograms is implemented by using subprograms is implemented by using parameters. parameters.

Data structures (such as variables) which are Data structures (such as variables) which are only passed into subprograms to be used are only passed into subprograms to be used are known as known as in in parameters.parameters.

Variables which are only passed into Variables which are only passed into subprogram and are changed or updated are subprogram and are changed or updated are referred to as referred to as in/out in/out parameters.parameters.

Variables which are only passed out of Variables which are only passed out of subprograms are known as subprograms are known as out out parameters.parameters.

Data FlowData Flow

A A parameter parameter is information about a is information about a data item being supplied to a data item being supplied to a subprogram (function or procedure) subprogram (function or procedure) when it is called into use.when it is called into use.

When the subprogram is used, the When the subprogram is used, the calling program must pass parameters calling program must pass parameters to it- this is called to it- this is called parameter passingparameter passing..

Data FlowData Flow

Actual and Formal ParametersActual and Formal Parameters The parameter that contains the The parameter that contains the

actual data that is being passed is actual data that is being passed is called the called the Actual ParameterActual Parameter..

The parameter into which the data is The parameter into which the data is being passed into is called the being passed into is called the Formal Parameter.Formal Parameter.

Data FlowData Flow Value and reference parametersValue and reference parameters

• Parameters can be passed either by Parameters can be passed either by value value or by or by referencereference..

• The method used depends upon whether the parameter is The method used depends upon whether the parameter is going in to or in/out of a procedure (or function).going in to or in/out of a procedure (or function).

• Parameters are passed by Parameters are passed by valuevalue when a parameter is passed when a parameter is passed intointo a procedure but does not require to be passed out again a procedure but does not require to be passed out again (to be used in another procedure).(to be used in another procedure).

• Parameters are passed Parameters are passed by referenceby reference when the parameter when the parameter required to be passed into a procedure, required to be passed into a procedure, updated updated and then and then passed out of the procedure again.passed out of the procedure again.

• Once exception to this rule exists for the array data structure Once exception to this rule exists for the array data structure only. When an only. When an array array is being passed as a parameter, it is is being passed as a parameter, it is always passed by always passed by referencereference..

Data FlowData Flow

It is necessary to describe the data flow It is necessary to describe the data flow in a diagram in order to work out how the in a diagram in order to work out how the parameters should be passed between parameters should be passed between the main program and any subprograms the main program and any subprograms and between the subprograms and between the subprograms themselves.themselves.