Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "High Level Programming Language Constructs Higher Computing Unit 2 – Software Development."— Presentation transcript:

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

2 What I need to know Description and exemplification of the following constructs in pseudocode and an appropriate high level language: Description and exemplification of the following constructs in pseudocode and an appropriate 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 and boolean variables; and 1-D arrays 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 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

3 Syntax and Semantics The syntax of a programming language is the rules of the language. The syntax of a programming language is the rules of the language. Each programming language has its own syntax. 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. The syntax of each programming language must be precise, or the computer will not understand the instruction and will give you a syntax error.

4 Syntax and Semantics The semantics of a particular instruction is what it does. The semantics of a particular instruction is what it does. SyntaxSemantics PRINT “Hello World!”These examples all display printf (“Hello World!”);the text “Hello World” put “Hello World!” into field “displayMessage” displayMessage.Text = “Hello World”

5 Syntax and Semantics Examples of the same syntax, but different semantics: SyntaxSemantics number = 10In Visual Basic, gives the value 10 to the variable called number number = 10In COMAL, this is part of a condition e.g. IF number = 10 THEN… N.B. 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.

6 Modularity Modularity means that when a program is designed and written, it is divided up into smaller sections called subprograms or sub-routines. 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. 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. 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 pre- written as a library module. Subprograms may be written at the same time as the rest of the program or they may by pre- written as a library module.

7 Modularity There are two types of modules or subprograms.There are two types of modules or subprograms. ProceduresProcedures Before a procedure may be used in program, it must be defined.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.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.Data is sent to a procedure using parameters.

8 Modularity When a procedure receives data, it carries out an operation using the data and makes results available to the program.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.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.A procedure is said to produce an effect.

9 Modularity Functions Functions A function is similar to a procedure but returns a single value to a program.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.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.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.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.

10 Objects and operations An operation is a process which is carried out on an item of data. 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. An object is the item of data which is involved in the process.

11 Objects and operations Arithmetic Operations Arithmetic Operations Arithmetic operations are calculated involving numeric data (objects).Arithmetic operations are calculated involving numeric data (objects). The set of arithmetic operations includes add, subtract, multiply and divide.The set of arithmetic operations includes add, subtract, multiply and divide. These operators are represented in many programming languages by using the symbols +, -, * and /. These operators are represented in many programming languages by using the symbols +, -, * and /.

12 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.

13 Objects and Operations String operations String operations Can process string data.Can process string data. String operations include joining strings, known as concatenation, and selecting parts of strings, known as substrings.String operations include joining strings, known as concatenation, and selecting parts of strings, known as substrings.

14 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.

15 Objects and Operations Relational operations use relational operators to compare data and produce a Boolean answer of true of false. Relational operations use relational operators to compare data and produce a Boolean answer of true of false. The set of relational operators include: The set of relational operators include: =equals >greater than <less than >=greater than or equal to <=less than or equal to <>is not equal to Relational operators can be used in program control structures such as selection and repetition Relational operators can be used in program control structures such as selection and repetition

16 Objects and Operations Examples of relational operations: Repeat Until length(v_firstname) < 20

17 Objects and Operations Logical operations Logical operations are also called Boolean operations because they use logical operators to produce a Boolean result such as true or false.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.The set of logical operators includes AND, OR and NOT. Logical operators are usually combined with relational operations in program control structures.Logical operators are usually combined with relational operations in program control structures.

18 Objects and Operations Example of logical operations: Repeat Until length(v_firstname) < 20 AND v_firstname is NOT a number

19 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. 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. Each high level language has its own syntax for the formatting of program input/output. In RunRev TAB is used to format output. In RunRev TAB is used to format output. local tablist = “1, 100, 200,300”

20 Basic features The features of a software development environment include: The features of a software development environment include: Control; Control; Data Storage; & Data Storage; & Data Flow. Data Flow.

21 Control There are three basic control structures used in a procedural language. There are three basic control structures used in a procedural language. These are: These are: Sequence,Sequence, Selection; &Selection; & Repetition.Repetition.

22 Sequence Sequence simply means the order in which things are done. Sequence simply means the order in which things are done. In programming the sequence in which you give instructions to the computer is important. In programming the sequence in which you give instructions to the computer is important.

23 Selection Selection means making a choice or deciding something. 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. 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. Conditions have values, they may either be true or false.

24 Selection (age = 18) (age = 18) is a simple conditionis a simple condition (Month >= 1) AND (Month = 1) AND (Month <= 12) is a complex conditionis a complex condition Two or more simple conditions linked by AND, OR, NOT are said to be complex. Two or more simple conditions linked by AND, OR, NOT are said to be complex.

25 Selection IF age = 18 THEN I can vote ELSE I can’t vote END IF In each case the condition is tested and if true the appropriate selection is made. 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. 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. This has the effect of increasing the number of possible pathways that may be followed through a program. IF Month >= 1 AND Month = 1 AND Month <= 12 THEN process date ELSE display error END IF

26 Selection Two control structures are commonly used to allow selection. These are; Two control structures are commonly used to allow selection. These are; IF…THEN…ELSE…END IFIF…THEN…ELSE…END IF CASE…OF…WHEN…END CASECASE…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 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. 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.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.The other statements are not executed.

27 Selection The SYNTAX of the IF structure is: The SYNTAX of the IF structure is: IF THEN ELSE END IF

28 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

29 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. 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. It is much more readable to use a CASE structure.

30 Selection The SYNTAX of the CASE structure is: The SYNTAX of the CASE structure is: CASE OF WHEN WHEN <action1> <action2> <action3> <action4> <action5>OTHERWISE<action6> END CASE

31 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. 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. 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.

32 Selection *grade algorithm using CASE* 1. CASE pupil’s test mark of 2. WHEN greater than or equal to 80 3. grade is grade 1 4. WHEN greater than or equal to 60 5. grade is grade 2 6. WHEN greater than or equal to 40 7. grade is grade 3 8. WHEN greater than or equal to 20 9. grade is grade 4 10. WHEN greater than or equal to 10 11. grade is grade 5 12. OTHERWISE 13. grade is grade 6 14. END CASE

33 Repetition Repetition means doing something over and over again. Repetition means doing something over and over again. Repetition involves using a loop. Repetition involves using a loop. Loops may be either conditional or fixed (unconditional). Loops may be either conditional or fixed (unconditional).

34 Repetition Fixed Loop Fixed Loop The purpose of a fixed loop is to repeat a set of program statements for a pre- determined number of times.The purpose of a fixed loop is to repeat a set of program statements for a pre- determined number of times. repeat 5 times …… end repeat

35 Repetition Conditional Loop 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.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 Syntax WHILE WHILE END WHILE OrREPEAT UNTIL UNTIL

36 Variables Variables Variables 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 address.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.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.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.Variable names, procedures and function names are sometimes also called identifiers, because they are used to identify that particular item.

37 Local 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 procedure is entered and the data that they contain is lost when the processing of that procedure is complete. 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. 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 VariablesGlobal 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 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. 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.

38 Scope of Variables This is the range of statements for which a variable is valid. 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. 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. 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.

39 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. 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 types- these are called user defined data types Some high level languages, such as C++, allow programs to specify their own data types- these are called user defined data types

40 Data Types Some of the more important data types are listed below: 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.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.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 repetitionBoolean 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

41 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.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;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’.This array has four parts. The third element of this array contains ‘Peter’.

42 Data Structures - Arrays Arrays which have one number as their subscript are called one-dimensional 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. 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:E.g. to set aside space for an array called apples with 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 apples Local apples

43 Data Structure - Arrays 1.Set array counter to zero 2.Set aside space for ten pupils names in the array name [ ] 3.Set aside space for ten pupils marks in the array mark [ ] 4.REPEAT 5.add one to array counter 6.put value into name array [counter] 7.put value into mark array [counter] 8.UNTIL end of data is reached

44 Data Flow Data flow or the movement of data between subprograms is implemented by using parameters. 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. 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 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. Variables which are only passed out of subprograms are known as out parameters.

45 Data Flow A parameter is information about a data item being supplied to a subprogram (function or procedure) when it is called into use. 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. When the subprogram is used, the calling program must pass parameters to it- this is called parameter passing.

46 Data Flow Actual and Formal Parameters The parameter that contains the actual data that is being passed is called the Actual Parameter. 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. The parameter into which the data is being passed into is called the Formal Parameter.

47 Data Flow Value and reference parameters Value and reference parameters Parameters can be passed either by value or by reference.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).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 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.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.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.

48 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. 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.


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

Similar presentations


Ads by Google