Presentation is loading. Please wait.

Presentation is loading. Please wait.

COMPUTATIONAL CONSTRUCTS

Similar presentations


Presentation on theme: "COMPUTATIONAL CONSTRUCTS"— Presentation transcript:

1 COMPUTATIONAL CONSTRUCTS
HIGHER COMPUTING SCIENCE MR ROSS

2 LEARNING INTENTION Computational Constructs Revision: Sequence
Selection (inc. complex selection) Iteration or Repetition Concatenation Pre-defined functions

3 REVISION – SEQUENCE Each instruction follows after the previous instruction. All instructions are executed (run). E.g. a program to add two number together 1. Declare variables 2. RECEIVE number1 FROM keyboard 3. RECEIVE number2 FROM keyboard 4. SET number3 TO number1 plus number2 5. SEND number3 TO display

4 REVISION – SEQUENCE

5 REVISION – SELECTION We use conditions to write programs which can make choices. This means that the program does not execute every step from beginning to end regardless of user input. Simple selection means that we check one condition only.

6 Greater than or equal to
REVISION – SELECTION > Greater than < Less than >= Greater than or equal to <= Less than or equal to = Equal to <> Not equal to

7 REVISION – SELECTION – IF … THEN … ELSE
E.g. a program to validate a password 1. Declare variables 2. RECEIVE attempt FROM keyboard 3. IF attempt = password THEN SEND success TO display 5. ELSE SEND failure TO display 7. END IF

8 REVISION – SELECTION - IF

9 REVISION – SELECTION – IF … THEN … ELSEIF
E.g. a program to decide which school a pupil should attend 1. Declare variables 2. RECEIVE age FROM keyboard 3. IF age >= 12 THEN SEND secondary school TO display 5. ELSEIF age >= 5 THEN SEND primary school TO display 7. ELSE SEND too young for school TO display 9. END IF

10 REVISION – SELECTION – IF … THEN … ELSEIF

11 REVISION – SELECTION – SELECT … CASE

12 REVISION – COMPLEX SELECTION
Complex selection means that we check more than one condition. To join conditions we use logical operators, such as: AND when both conditions are to be met OR when only one of the conditions is to be met NOT when a condition is to be false

13 REVISION – Complex SELECTION
Example: Can you drive on your own.

14 REVISION – ITERATION AND REPETITION
Two types of loop: Fixed – repeat a section of code a set number of times Conditional – repeat a section of code an unknown number of times until a condition specified in the code is met

15 REVISION – ITERATION AND REPETITION
What will the code above do?

16 REVISION – ITERATION AND REPETITION
What will the code above do?

17 REVISION – EXERCISE Please create a simple VB program which when the user clicks on a button will ask them to enter their age and whether they are a boy or a girl. If the user is over 18 then tell them they are too old for school. If the user is 12 or over and a girl tell them can go to Notre Dame secondary school. If the user is 12 or over and a boy tell them to go to secondary school. If the user is 5 or over tell them to go to primary school. If the user is 3 or over tell them to go to nursery school. Otherwise tell the user to go home.

18 REVISION – CONCATENATION
Concatenation is the joining together of 2 or more strings In VB it is performed using the & operator

19 REVISION – PRE-DEFINED FUNCTIONS
A pre-defined function is a function that has already been created and is part of or in-built to a programming language. A function is a piece of code which gives you a single value.

20 REVISION – PRE-DEFINED FUNCTIONS
Two parts of a function: name (what it will do) parameter (what it will do it to) e.g. in VB UCase: name parameter

21 REVISION – PRE-DEFINED FUNCTIONS
VB has a number of pre-defined String functions including: Name What it does Example UCase Converts lower-case letters into upper-case letters UCase(“Hello”) returns HELLO LCase Converts upper-case letters into lower case letters LCase(“Hello”) returns hello Len Calculate the number of characters in a string Len(“Hello”) Returns 5 Mid Extracts a sub-string from a string Mid(“Hello”, 2,3) Returns ell

22 LEARNING INTENTION Computational Constructs: Sub-programs
Scope of variables Parameter Passing

23 SUB-PROGRAMS A sub-program (subroutine) is used by computer scientists to describe a clear independent block of code within a program. These can be run (called) from within another part of the program. Used to break a complex problem down into a set of smaller, simpler problems. Make code modular so it becomes more readable and therefore easier to maintain.

24 SUB-PROGRAMS (Cont) Sub-programs are often categorised:
procedure (which executes a set of commands) function (which returns a single value) method (name for a function defined inside a class in an object oriented language)

25 SUB-PROGRAMS (Cont)

26 SUB-PROGRAMS (Cont)

27 VARIABLES and SCOPE Sub-programs, functions, procedures and methods all make use of variables. Variables must be declared and can then be given a value which may or may not change (vary) during the execution of the program.

28 VARIABLES and SCOPE (Cont)
The scope of a variable is used to describe where within code the variable can be used. There are two categories of variable scope: local variable global variable

29 LOCAL VARIABLES Local variables are only accessible (able to be used) within the sub-program in which they are defined. SubProgram2 does not know about age as it was declared in SubProgram1

30 GLOBAL VARIABLES Global variables are accessible anywhere in the code, within all sub-programs.

31 LOCAL vERSUS GLOBAL VARIABLES
The same local variable name can be used in multiple different sub programs. A local variable is only created and used in memory (RAM) while that sub-program is being run. When a sub-program is finished, the memory (RAM) for the local variables is freed, so local variables are more efficient when considering system resources

32 LOCAL vERSUS GLOBAL VARIABLES
A global variable needs to be retained in memory (RAM) for the duration of the running of the program. Global variables are therefore less efficient with respect to system resources. Programs you write in school are small so local v. global is not a big consideration but in real life applications there can be 1,000s of variables so scope needs to be considered carefully.

33 LOCAL vERSUS GLOBAL VARIABLES
But what if you need to access the same variable in more than one sub program? Should you just make it a global variable?

34 Parameter passing An alternative to global variables is to use parameter passing. There are two methods of passing parameters: passing the value of a variable or passing a reference to a variable from one sub-program to another sub-program which needs to use the variable.

35 Parameter passing

36 Parameter PASSING

37 Parameter passing

38 Parameter passing (IN)
In DisplayUserAge, we did not change the users age so we only need to pass it IN. We call this pass by value. In Visual Basic we use ByVal to signify this. A copy of the original value is used by the sub-program. The copy is discarded once the sub-program is finished.

39 Parameter passing (OUT)
In GetUserAge, we were obtaining (effectively changing) the users age so we needed to pass it OUT. We call this pass by reference. In Visual Basic we use ByRef to signify this, we pass in a reference to the value. This means the calling function (MainProgram in this example) gets the new value back out.

40 ACTUAL AND FORMAL PARAMETERS
Actual parameter is the parameter that is passed (by value or by reference) to the sub-program from another part of the program. Formal parameter is the parameter in the sub-program definition.

41 ACTUAL AND FORMAL PARAMETERS
Actual parameter Formal parameter

42 PASSING PARAMETERS - EXERCISE
You are now going to take a piece of code which uses global variables and convert it to use local variables and parameter passing. Your teacher will give you the instruction sheet.


Download ppt "COMPUTATIONAL CONSTRUCTS"

Similar presentations


Ads by Google