120 likes | 188 Views
Session Objectives #U2 S2 . Keywords. Algorithm. Sequence. Selection. Iteration. Variables & Constants. Data Types. Planning a programme.
E N D
Keywords Algorithm Sequence Selection Iteration Variables & Constants Data Types
Planning a programme Before attempting to write a programme, the solution needs to be planned. This helps the coder to see what they want to achieve and how they can best achieve it by breaking the problem down into logical steps. Methods for planning a programme solution include the use of the flow charts and pseudocode. FLOW CHARTS – the following symbols are used START Input Width STOP START Start or Stop a programme Input Height What will this programme do? Represent a process i.e adding Area = Width x Height Represents input or output values Output Area A decision symbol i.e Yes or No, or a choice of paths STOP
Flow Charts Simple flow chart with decision symbols. START Check the weather outside Else No Don’t wear waterproofs Is it raining? Wear waterproof trousers STOP STOP IF Yes TASK 1: Create a flow chart for making a cup of coffee.
3 basic program constructs RECAP: Can you remember what the 3 basic program constructs are? Sequence Selection Iteration TASK 2: Prepare to explain the program constructs needed in your coffee program
Data Types (Variable Type) In databases we already know that the software needs to know what type of data is being held in each data field. This is known as the field type. Similarly when writing programs it is necessary to understand the type of data that will be held by the variables used in the program. The reasons are the same as databases: It provides limited validation of data Certain operations can only be performed on certain data types (NB: the prompt() command in Javascript is set to string therefore it cannot automatically perform mathematical operations ) Memory can be efficiently allocated to store data.
Data Types (Variable Type) For this course, the data types you need to know about are: Integer Floating Point Real Boolean Character String TASK 3: In your course folders, make notes on each type giving examples. Data Types Resource - Teach-ICT TASK 4: Prepare to discuss the variable types which could be used for your coffee program.
Creating a coding solution TASK 4: Now create a flow chart for a program that will add any two numbers and give the result. ANSWER: Pseudocode START Declare variables for number1, 2 and answer Prompt input number1 and store as variable Input Number 1 Input Number 2 Prompt input number2 and store as variable Answer = N1 + N2 Answer of number1 + number2 stored as variable Output Answer Alert user of answer as message. STOP
Coded Solution Now let’s code this solution. Remember javascript goes inbetween <script>…</script> tags TIPS: Declare variables at the start of the program var x, var y, parseInt() This will convert the data type to an Integer. (NB prompts are treated as string) Var1 = parseInt(prompt(‘Enter something here?’)) Notice the 2 brackets needed to close the 2 open brackets Alert() To display output
Multiple Function Calculator TASK 5: Create a calculator which can perform all 4 maths operations. You must design the solution first by creating a flow chart. EXT: How could the efficiency of this program be improved??
Flow Chart for 4 Operations Calculator START Which Calculator If = / If = + Input Number 1 Input Number 1 Input Number 1 If = - Input Number 1 If = * Input Number 2 Input Number 2 Input Number 2 Input Number 2 Answer = N1 / N2 Answer = N1 + N2 Answer = N1 - N2 Answer = N1 * N2 Output Answer Output Answer Output Answer Output Answer STOP STOP STOP STOP
Keywords Algorithm Sequence Selection Iteration Variables & Constants Data Types