COMPUTATIONAL CONSTRUCTS

Slides:



Advertisements
Similar presentations
Microsoft® Small Basic
Advertisements

Objectives Understand the software development lifecycle Perform calculations Use decision structures Perform data validation Use logical operators Use.
Software Development Sub Procedures, Functions and Parameters.
CS0004: Introduction to Programming Repetition – Do Loops.
Program Development Procedures 1.Program definition clearly define what the problem is. clearly define Input and output data (types, precision, units used)
Program Design and Development
Chapter 5: Loops and Files.
CS241 PASCAL I - Control Structures1 PASCAL I - Control Structures Philip Fees CS241.
Adding Automated Functionality to Office Applications.
Software design and development Marcus Hunt. Application and limits of procedural programming Procedural programming is a powerful language, typically.
Apply Sub Procedures/Methods and User Defined Functions
Mr C Johnston ICT Teacher BTEC IT Unit 06 - Lesson 05 Learning To Program.
1 Subroutines and Functions Chapter 6 in Deitel, Deitel and Nieto.
Flow of Control. 2 Control Structures Control structure: An instruction that determines the order in which other instructions in a program are executed.
PROGRAMMING Functions. Objectives Understand the importance of modular programming. Know the role of functions within programming. Use functions within.
Chapter 8 High-Level Programming Languages. 8-2 Chapter Goals Describe the translation process and distinguish between assembly, compilation, interpretation,
110-G1 Motivation: Within a program, may have to perform the same computation over and over Many programs share the same computation (e.g. sorting) To.
Software Development Topic 3 High Level Language Constructs.
CPS120: Introduction to Computer Science Decision Making in Programs.
What does C store? >>A = [1 2 3] >>B = [1 1] >>[C,D]=meshgrid(A,B) c) a) d) b)
CPS120: Introduction to Computer Science Lecture 14 Functions.
Visual Basic Programming
Procedural programming in Java Methods, parameters and return values.
Higher Grade Computing Studies 3. High Level Language Constructs Higher Computing Software Development S. McCrossan 1 Simple Data Types Integer: An integer.
22/11/ Selection If selection construct.
I Power Higher Computing Software Development High Level Language Constructs.
Practical Programming COMP153-08S Week 5 Lecture 1: Screen Design Subroutines and Functions.
1 Scripting Languages VBScript - Recognized mainly by Internet Explorer only - Netscape does have a plug-in JavaScript - Recognized by Internet Explorer.
Lecture 26: Reusable Methods: Enviable Sloth. Creating Function M-files User defined functions are stored as M- files To use them, they must be in the.
Computing Higher – SD Unit - Topic 8 – Procedure and Standard Algorithms P Lynch, St Andrew’s High School Unit 2 Software Development Process Topic.
Loops and Files. 5.1 The Increment and Decrement Operators.
31/01/ Selection If selection construct.
Controlling Program Flow with Decision Structures.
Visual Basic Review LBS 126. VB programming Project Form 1Form 2Form 3 Text boxButton Picture box Objects Text box Button Objects.
Selection Using IF THEN ELSE CASE Introducing Loops.
CS0004: Introduction to Programming
3.1 Fundamentals of algorithms
High and low level languages
Functions and Procedures
IF statements.
Scripts & Functions Scripts and functions are contained in .m-files
Designing and Debugging Batch and Interactive COBOL Programs
When I want to execute the subroutine I just give the command Write()
Teaching London Computing
Scratch: selection / branching/ if / If…else / compound conditionals / error trapping by Mr. Clausen.
If selection construct
Coding Concepts (Basics)
` Structured Programming & Flowchart
If selection construct
Do … Loop Until (condition is true)
Introduction to Problem Solving and Control Statements
Logical Operations In Matlab.
Computer Science Core Concepts
Learning Intention I will learn about programming using selection (making choices) with one condition.
If, Subroutines and Functions
Language Constructs Construct means to build or put together. Language constructs refers to those parts which make up a high level programming language.
Chapter 3: Selection Structures: Making Decisions
Boolean Expressions to Make Comparisons
Flow of Control.
The structure of programming
Introduction to Computer Science
Chapter 3: Selection Structures: Making Decisions
Based on slides created by Bjarne Stroustrup & Tony Gaddis
The structure of programming
Thinking procedurally
Learning Intention I will learn about the standard algorithm for input validation.
The Step Parameter & Nested For … To … Next loops
10.2 Procedures Passing Parameters 30/08/2019.
COMPUTING.
Presentation transcript:

COMPUTATIONAL CONSTRUCTS HIGHER COMPUTING SCIENCE MR ROSS

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

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

REVISION – SEQUENCE

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.

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

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 4. SEND success TO display 5. ELSE 6. SEND failure TO display 7. END IF

REVISION – SELECTION - IF

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 4. SEND secondary school TO display 5. ELSEIF age >= 5 THEN 6. SEND primary school TO display 7. ELSE 8. SEND too young for school TO display 9. END IF

REVISION – SELECTION – IF … THEN … ELSEIF

REVISION – SELECTION – SELECT … CASE

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

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

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

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

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

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.

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

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.

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

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

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

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.

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)

SUB-PROGRAMS (Cont)

SUB-PROGRAMS (Cont)

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.

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

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

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

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

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.

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?

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.

Parameter passing

Parameter PASSING

Parameter passing

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.

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.

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.

ACTUAL AND FORMAL PARAMETERS Actual parameter Formal parameter

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.