Upcoming SlideShare
×

Programming For As Comp

592 views

Published on

This covers details on Writing Pascal using Lazarus.
A teaching resource for students without any previous experience. Can be used for teaching or direct notes for students (Continued with notes for A2).
Originally written for AQA A level Computing (UK exam).

Published in: Technology
1 Like
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

Views
Total views
592
On SlideShare
0
From Embeds
0
Number of Embeds
5
Actions
Shares
0
8
0
Likes
1
Embeds 0
No embeds

No notes for slide
• Details of exercises for marking and checking: Exercise A 1. Have a go at altering the output text adding extra text and outputting variables. 2. Ask the user for two numbers and display them as follows: Exercise B Write code to take two numbers input. Add them together displaying the result. Multiply them displaying the result. Divide them displaying the result. Subtract them displaying the result. Exercise C Write an application (using if) to take a number input and output if the number is: “ smaller than 100” “ Between 100 and 200” “ Bigger than 200” Exercise D Rewrite the following if statements as a Case statement. Exercise E Using for loops write an application to produce the output(s): Exercise F Write a while loop to display the following output: Write a while loop to produce the below output: Write a while loop to ask a user to enter a number till they enter a number over 20 Exercise G Write an application to create 3 records storing the following aspects of people: Name Age Gender Nationality Ask a user to enter this information Output the name of the oldest person Exercise H Use an array to take in 10 numbers from a user. Output the largest number Output the smallest number Output the average number Exercise I Write an application that takes in two numbers Using a procedure produce a welcome message Using a function add the two numbers together outputting the result Using a function multiply the two numbers outputting the result
• Programming For As Comp

1. 1. Programming for AS Computing Using Pascal & Lazarus By David Halliday
2. 2. Pascal <ul><li>Pascal is a high level imperative language </li></ul><ul><li>Developed in 1970 </li></ul><ul><li>Named after mathematician and philosopher Blaise Pascal. </li></ul><ul><li>More history: http://en.wikipedia.org/wiki/Pascal_programming_language </li></ul><ul><li>Pascal documentation available here: http://www.freepascal.org/docs-html/ref/ref.html </li></ul>
3. 3. Delphi/Object Pascal <ul><li>Borland Delphi is a trade name for Object Pascal created by Borland. </li></ul><ul><li>The language is actually called “Object Pascal” but is widely referred to as “Delphi”. </li></ul><ul><li>Object Pascal adds object oriented programming and graphical (GUI) “widgets” to the language. </li></ul><ul><li>Released in 1995. </li></ul><ul><li>More history: http:// en.wikipedia.org/wiki/Delphi_programming_language </li></ul><ul><li>Object Pascal documentation available here: http://www.freepascal.org/docs-html/ref/ref.html </li></ul>
4. 4. Lazarus <ul><li>An IDE (Integrated Development Environment) for Pascal & Delphi. </li></ul><ul><li>A RAD (Rapid Application Development) tool. </li></ul><ul><li>Freely available under GNU GPL. </li></ul><ul><li>Platform independent. </li></ul>
5. 5. The Lazarus Interface
6. 6. Getting normal Pascal in Lazarus <ul><li>Save any work/project you are working on </li></ul><ul><li>Project>New Project </li></ul><ul><li>Select “Program” </li></ul><ul><li>You are now ready to edit Pascal code without the graphical aspects (can make learning easier). Many tutorials (and the early lessons here) are based on Pascal not Delphi and removing the graphical aspects can make learning easier. </li></ul>
7. 7. Pascal code - comments <ul><li>The first thing to learn in any language is comments </li></ul><ul><li>Comments are ignored by the compiler and help you remember things or provide guidance to other developers. </li></ul><ul><li>(* This is an old style comment *)   </li></ul><ul><li>{  This is a Turbo Pascal comment }   </li></ul><ul><li>// This is a Delphi comment. Ignored till end of line. </li></ul>
8. 8. Statements <ul><li>Statements are “instructions” in a programming language. </li></ul><ul><li>Some languages use line breaks (pressing return) to separate statements. </li></ul><ul><li>Many languages including C, C++ and Pascal use a special character to say “end of statement”. In Pascal the character is the semi colon “;”. </li></ul><ul><li>writeln('hello world'); //output “hello world” </li></ul><ul><li>myNo := thisNo + thatNo; //Add two numbers </li></ul>
9. 9. First application <ul><li>The first part just tells the compiler this is a program and in “Project1”. </li></ul><ul><li>The sections in curly brackets {} are comments. </li></ul><ul><li>Uses & Classes is for including extra library code. </li></ul><ul><li>The Begin and end. (the period is important) mark where the actual code starts/finishes. </li></ul><ul><li>All of this code is already written for you by Lazarus. </li></ul><ul><li>program Project1; </li></ul><ul><li>{\$mode objfpc}{\$H+} </li></ul><ul><li>uses </li></ul><ul><li>Classes </li></ul><ul><li>{ add your units here }; </li></ul><ul><li>Begin </li></ul><ul><li>end. </li></ul>
10. 10. First application part 2 <ul><li>The command used between the begin and end. Is “writeln” which will be covered in more detail shortly. </li></ul><ul><li>The statement consists of a key word “writeln” followed by “arguments” in brackets. </li></ul><ul><li>In the brackets is text in single quotes. The text will be displayed the quotes are there just to tell the compiler that this is to be treated as text. </li></ul><ul><li>program Project1; </li></ul><ul><li>{\$mode objfpc}{\$H+} </li></ul><ul><li>uses </li></ul><ul><li>Classes </li></ul><ul><li>{ add your units here }; </li></ul><ul><li>Begin </li></ul><ul><li>writeln('hello world’); </li></ul><ul><li>end. </li></ul>
11. 11. First application part 3 <ul><li>A subtlety of modern operating systems being graphical is that when you launch an application using a command line (shown in a window) the window closes when the application finishes. </li></ul><ul><li>For now just use the extra lines shown. </li></ul><ul><li>This is all one line but is wrapped around on the slide. </li></ul><ul><li>program Project1; </li></ul><ul><li>{\$mode objfpc}{\$H+} </li></ul><ul><li>uses </li></ul><ul><li>Classes </li></ul><ul><li>{ add your units here }; </li></ul><ul><li>Var </li></ul><ul><li>myint : integer; </li></ul><ul><li>Begin </li></ul><ul><li>writeln('hello world’); </li></ul><ul><li>writeln(‘type in a number and press enter’); </li></ul><ul><li>read(myint); </li></ul><ul><li>end. </li></ul>
12. 12. First application part 4 <ul><li>Press the green arrow/play button in Lazarus to compile </li></ul><ul><li>Below is an image of the code running after being compiled </li></ul><ul><li>program Project1; </li></ul><ul><li>{\$mode objfpc}{\$H+} </li></ul><ul><li>uses </li></ul><ul><li>Classes </li></ul><ul><li>{ add your units here }; </li></ul><ul><li>Var </li></ul><ul><li>myint : integer; </li></ul><ul><li>Begin </li></ul><ul><li>writeln('hello world’); </li></ul><ul><li>writeln(‘type in a number and press enter’); </li></ul><ul><li>read(myint); </li></ul><ul><li>end. </li></ul>
13. 13. Pascal input & output <ul><li>The extra code we added actually did both input (taking data into the program from the user) and output (producing something for the user). </li></ul><ul><li>We also declared a variable (more on this shortly). </li></ul><ul><li>The output was done by telling the application to write something to the screen using “writeln”. </li></ul><ul><li>The input was when we asked the user to type in a number (and the application waited for the user to type a number…and press enter to let the application know the user had finished typing). </li></ul><ul><li>program Project1; </li></ul><ul><li>{\$mode objfpc}{\$H+} </li></ul><ul><li>uses </li></ul><ul><li>Classes </li></ul><ul><li>{ add your units here }; </li></ul><ul><li>Var </li></ul><ul><li>myint : integer; </li></ul><ul><li>Begin </li></ul><ul><li>writeln('hello world’); </li></ul><ul><li>writeln(‘type in a number and press enter’); </li></ul><ul><li>read(myint); </li></ul><ul><li>end. </li></ul>
14. 14. Output <ul><li>“ Writeln” outputs the contents of the brackets and then goes onto a new line. </li></ul><ul><li>“ Write” outputs the contents of the brackets but does not go onto a new line. </li></ul><ul><li>Note that the new line occurs at the end of the output if it is a “writeln”. </li></ul><ul><li>On the right: </li></ul><ul><ul><li>Top: code extract </li></ul></ul><ul><ul><li>Bottom: Output </li></ul></ul><ul><li>writeln(‘hello world’); </li></ul><ul><li>write(‘1’); </li></ul><ul><li>write(‘2’); </li></ul><ul><li>write(‘3’); </li></ul><ul><li>writeln(‘4’); </li></ul><ul><li>write(‘5’); </li></ul><ul><li>writeln(‘’); </li></ul><ul><li>writeln(‘6’); </li></ul><ul><li>writeln(‘7’); </li></ul><ul><li>hello world </li></ul><ul><li>1234 </li></ul><ul><li>5 </li></ul><ul><li>6 </li></ul><ul><li>7 </li></ul>
15. 15. Output part 2 <ul><li>“ Writeln” can also output the contents of variables. </li></ul><ul><li>For this example “myint” is an integer variable. </li></ul><ul><li>Here we see text in single quotes. </li></ul><ul><li>Here a variable is referenced </li></ul><ul><li>Next we output both in one command. Note that you need to use the comma to separate elements. </li></ul><ul><li>writeln(‘your integer:’); </li></ul><ul><li>writeln(myint); </li></ul><ul><li>writeln(‘your number:’, myint); </li></ul>
16. 16. Input <ul><li>The first thing about taking an input from a user is “what is the computer going to do with the input”. </li></ul><ul><li>In short this depends on what the input is going to be used for. </li></ul><ul><li>If you are going to perform mathematics then you need the computer to realise that you are giving it a number or “Integer” (data types will be covered later). </li></ul><ul><li>program Project1; </li></ul><ul><li>{\$mode objfpc}{\$H+} </li></ul><ul><li>uses </li></ul><ul><li>Classes </li></ul><ul><li>{ add your units here }; </li></ul><ul><li>Begin </li></ul><ul><li>end. </li></ul>
17. 17. Input part 2 <ul><li>Variables are defined in a separate section before the main code as shown. </li></ul><ul><li>The variable “myint” is first named. </li></ul><ul><li>Then you put in a colon as a seperator. </li></ul><ul><li>Finaly the data type (here it is “intiger” for whole numbers). </li></ul><ul><li>Then the semi colon. </li></ul><ul><li>program Project1; </li></ul><ul><li>{\$mode objfpc}{\$H+} </li></ul><ul><li>uses </li></ul><ul><li>Classes </li></ul><ul><li>{ add your units here }; </li></ul><ul><li>Var </li></ul><ul><li>myint : integer; </li></ul><ul><li>Begin </li></ul><ul><li>end. </li></ul>
18. 18. Input part 3 <ul><li>Variable declaration all happens in the “var” section. </li></ul><ul><li>The layout is: </li></ul><ul><li><variable_name> : <data_type> ; </li></ul><ul><li>You can declare multiple variables of the same type using a comma to separate them. </li></ul><ul><li>program Project1; </li></ul><ul><li>{\$mode objfpc}{\$H+} </li></ul><ul><li>uses </li></ul><ul><li>Classes </li></ul><ul><li>{ add your units here }; </li></ul><ul><li>Var </li></ul><ul><li>myint : integer; </li></ul><ul><li>thisno, thatno : integer; </li></ul><ul><li>result: real; </li></ul><ul><li>Begin </li></ul><ul><li>end. </li></ul>
19. 19. Input part 4 <ul><li>Now we have somewhere to store out input we need to get it. </li></ul><ul><li>First ask the user to enter something (and what data type they are to enter). </li></ul><ul><li>Then tell the computer to read the input. </li></ul><ul><li>As this example keeps the question and input on one line you need to write a blank “writeln” to the screen to move down to a new line. </li></ul><ul><li>Now output some text and the number input. </li></ul><ul><li>program Project1; </li></ul><ul><li>{\$mode objfpc}{\$H+} </li></ul><ul><li>uses </li></ul><ul><li>Classes </li></ul><ul><li>{ add your units here }; </li></ul><ul><li>Var </li></ul><ul><li>myint : integer; </li></ul><ul><li>Begin </li></ul><ul><li>write(‘Give number: ‘); </li></ul><ul><li>read(myint); </li></ul><ul><li>writeln(‘’); </li></ul><ul><li>writeln(‘got: ‘, myint); </li></ul><ul><li>end. </li></ul>
20. 20. Input part 5 <ul><li>Finally don’t forget that you need to keep the command window open. </li></ul><ul><li>We do this by asking the user to type in a number then press enter. </li></ul><ul><li>Begin </li></ul><ul><li>write(‘Give number: ’); </li></ul><ul><li>read(myint); </li></ul><ul><li>writeln(‘’); </li></ul><ul><li>writeln(‘got: ’, myint); </li></ul><ul><li>writeln(‘’); </li></ul><ul><li>writeln(‘give num then press enter’); </li></ul><ul><li>read(myint); </li></ul><ul><li>end. </li></ul>
21. 21. Exercise A <ul><li>Have a go at altering the output text adding extra text and outputting variables. </li></ul><ul><li>Ask the user for two numbers and display them as follows: </li></ul><ul><li>Give me the first number: 4 </li></ul><ul><li>Give me the second number: 3 </li></ul><ul><li>The numbers in reverse order are: </li></ul><ul><li>3 4 </li></ul>Input by user Input numbers shown in reverse order
22. 22. Data (variable) types <ul><li>Pascal supports FOUR standard variable types, which are </li></ul><ul><ul><li>Integer (whole numbers e.g. 3 6 294) </li></ul></ul><ul><ul><li>Char (character e.g. ‘b’ ‘n’ ‘4’ ‘&’) </li></ul></ul><ul><ul><li>Boolean (true or false) </li></ul></ul><ul><ul><li>real (floating point numbers with decimal point/fractions) </li></ul></ul><ul><ul><li>Standard Pascal does not make provision for the string data type, but most modern compilers do. Strings covered later. </li></ul></ul>
23. 23. Variable Declaration <ul><li>Variable declaration before application code starts. </li></ul><ul><li>Declare an integer, character real/floating point number. </li></ul><ul><li>Start the application. </li></ul><ul><li>Set the variables. </li></ul><ul><li>Output the contents of the variables. </li></ul><ul><li>End the application </li></ul><ul><li>var </li></ul><ul><li>number1: integer; </li></ul><ul><li>letter : char; </li></ul><ul><li>money : real; </li></ul><ul><li>begin </li></ul><ul><li>number1 := 34; </li></ul><ul><li>letter := 'Z'; </li></ul><ul><li>money := 32.345; </li></ul><ul><li>writeln( ‘number: ’, number1 ); </li></ul><ul><li>writeln( ‘letter: ’, letter ); </li></ul><ul><li>writeln( ‘money: ’, money ); </li></ul><ul><li>end. </li></ul>
24. 24. Mathematics <ul><li>The main use of computers and their for programming in the early days (just before they made the first computer game) was for scientific and mathematic calculations. </li></ul><ul><li>Mathematics in Pascal uses the following symbols: </li></ul><ul><ul><li>Div is used when you want an integer result for division. All the other symbols are the same for all numerical data types </li></ul></ul><ul><li>Be careful about your data type </li></ul>/ Divide (possible decimal point answer) Div Divide (whole number result) * multiply - Subtract + Add
25. 25. Mathematics examples <ul><li>Addition </li></ul><ul><li>Subtraction </li></ul><ul><li>Multiplication </li></ul><ul><li>Division (integer) </li></ul><ul><li>Division (real) </li></ul><ul><li>Myint := thisint + thatint; </li></ul><ul><li>Myint := thisint - thatint; </li></ul><ul><li>Myint := thisint * thatint; </li></ul><ul><li>Myint := thisint div thatint; </li></ul><ul><li>MyReal := thisint / thatint; </li></ul>
26. 26. Exercise B <ul><li>Write code to take two numbers input. </li></ul><ul><li>Add them together displaying the result. </li></ul><ul><li>Multiply them displaying the result. </li></ul><ul><li>Divide them displaying the result. </li></ul><ul><li>Subtract them displaying the result. </li></ul>
27. 27. User defined Data types <ul><li>Often it is important to define your own data type to provide only a set number of possibilities </li></ul><ul><li>Useful for effective usage of memory (RAM) when working with a string that can only be one of a small number of choices. </li></ul><ul><li>Good for preventing errors (only allowed data items can be used). </li></ul><ul><li>Known as Enumerated variables which are defined by the programmer. </li></ul><ul><li>You first create the set of symbols, and assign to them a new data type variable name. </li></ul>
28. 28. Enumerated Variables <ul><li>Type </li></ul><ul><li>beverage = ( coffee, tea, cola, soda, milk, water ); </li></ul><ul><li>colour = ( green, yellow, blue, black, white ); </li></ul><ul><li>Var </li></ul><ul><li>drink : beverage; </li></ul><ul><li>chair : colour; </li></ul><ul><li>drink := coffee; </li></ul><ul><li>chair := green; </li></ul><ul><li>if chair = yellow then drink := tea; </li></ul>Define beverage as being one of the named items Define colour as being one of the named items Define drink as a beverage Define chair as colour (even though this is only one possible attribute) Assign values to the variables
29. 29. Strings <ul><li>Often a single character is insufficient for any/most text based work. </li></ul><ul><li>Characters can be put together or strung together to allow for words and other text objects to be stored. </li></ul><ul><li>Strings aren’t a standard data type but are included here as they are common. And are included with most compilers. </li></ul><ul><li>Strings historically use arrays (covered later). </li></ul>
30. 30. String example <ul><li>Starting code as normal </li></ul><ul><li>Declare string variable </li></ul><ul><li>Begin application </li></ul><ul><li>Assign string </li></ul><ul><li>Output string variable </li></ul><ul><li>Output string from code </li></ul><ul><li>Read an input (to hold application window open till enter pressed). </li></ul><ul><li>program Project1; </li></ul><ul><li>{\$mode objfpc}{\$H+} </li></ul><ul><li>uses </li></ul><ul><li>Classes </li></ul><ul><li>{ add your units here }; </li></ul><ul><li>var </li></ul><ul><li>mystr : string; </li></ul><ul><li>begin </li></ul><ul><li>mystr:= 'fred'; </li></ul><ul><li>write (mystr); </li></ul><ul><li>writeln (' is cool'); </li></ul><ul><li>readln; //takes input </li></ul><ul><li>end. </li></ul>
31. 31. Constants <ul><li>The last type of variable to look at now is one that doesn’t change (more will be seen later such as Arrays). </li></ul><ul><li>What is the point of a variable that doesn’t change? </li></ul><ul><ul><li>Things like pi and other mathematical values don’t ever change but you may need to define them for your application. </li></ul></ul><ul><ul><li>Things like VAT rate that rarely change (this isn’t the best example) </li></ul></ul><ul><li>Why not use a normal variable? </li></ul><ul><ul><li>Secures your data from errors (code mistakes will show up when they try to change the value of pi [more useful than you realise at first]). </li></ul></ul>
32. 32. Constants example <ul><li>Normal stuff </li></ul><ul><li>Constant declaration </li></ul><ul><ul><li>Name </li></ul></ul><ul><ul><li>Data type </li></ul></ul><ul><ul><li>Value </li></ul></ul><ul><li>Begin application </li></ul><ul><li>Output text & constant </li></ul><ul><li>Read a line to keep application open </li></ul><ul><li>End application </li></ul><ul><li>program Project1; </li></ul><ul><li>{\$mode objfpc}{\$H+} </li></ul><ul><li>uses </li></ul><ul><li>Classes </li></ul><ul><li>{ add your units here }; </li></ul><ul><li>const </li></ul><ul><li>pi : real = 3.141 ; </li></ul><ul><li>begin </li></ul><ul><li>writeln ('pi is:', pi); </li></ul><ul><li>readln; //takes any character </li></ul><ul><li>end. </li></ul>
33. 33. Conditions & Iteration <ul><li>Sometimes the computer has to make decisions based on what is happening. For this we use conditions e.g.: </li></ul><ul><ul><li>If </li></ul></ul><ul><ul><li>Case </li></ul></ul><ul><li>Often in code it is useful/necessary to use iteration to repeat an instruction 2 or more times (potentially infinitely). For this we use iteration e.g.: </li></ul><ul><ul><li>While </li></ul></ul><ul><ul><li>For </li></ul></ul>
34. 34. <ul><li>Often in life we have to make simple decisions like: </li></ul><ul><li>If hungry then eat </li></ul><ul><li>If thirsty then drink </li></ul><ul><li>If bored with lesson then talk to the person next to you </li></ul><ul><li>In computer programs we need to make decisions and for these we use the “IF” statement. </li></ul><ul><li>If   exp1   Then begin </li></ul><ul><li> Statment1; </li></ul><ul><li> Statment2; </li></ul><ul><li> end </li></ul><ul><li>End ; </li></ul>If this then that Statements to be executed This “begin” and “end” marks the start and end of the subsection of code. This end marks the “end” of the “if” statement. This is a logical expression/test with a true or false outcome.
35. 35. The If..then statement <ul><li>Start off as previously with a new program. </li></ul><ul><li>We will need to create an integer variable and get an input from the user (see previous slides for details on that). </li></ul><ul><li>Don’t forget that we don’t want the window to close before we have finished. </li></ul><ul><li>program Project1; </li></ul><ul><li>{\$mode objfpc}{\$H+} </li></ul><ul><li>uses </li></ul><ul><li>Classes </li></ul><ul><li>{ add your units here }; </li></ul><ul><li>Begin </li></ul><ul><li>end. </li></ul>
36. 36. The If..then statement 2 <ul><li>The first thing we want to do is get the program to make a simple decision for us. </li></ul><ul><li>To make life easy all we want to do is output a line of text if the number entered is 10. </li></ul><ul><li>Any ideas? </li></ul><ul><li>program Project1; </li></ul><ul><li>{\$mode objfpc}{\$H+} </li></ul><ul><li>uses </li></ul><ul><li>Classes </li></ul><ul><li>{ add your units here }; </li></ul><ul><li>Var </li></ul><ul><li>myint : integer; </li></ul><ul><li>Begin </li></ul><ul><li>write(‘Give number: ‘); </li></ul><ul><li>read(myint); </li></ul><ul><li>writeln(‘type in a number and press enter’); </li></ul><ul><li>read(myint); </li></ul><ul><li>end. </li></ul>
37. 37. The If..then statement 3 <ul><li>Can you see how the comparison works? </li></ul><ul><li>If the value in “myint” is equal to 10 then the statement(s) between the extra “begin” and “end” are executed. </li></ul><ul><li>Any other value to “myint” and nothing happens. </li></ul><ul><li>begin </li></ul><ul><li>write(‘Give number: ‘); </li></ul><ul><li>read(myint); </li></ul><ul><li>if myint = 10 then begin </li></ul><ul><li>writeln(‘Ten’); </li></ul><ul><li>end </li></ul><ul><li>end; </li></ul><ul><li>writeln(‘type in a number and press enter’); </li></ul><ul><li>read(myint); </li></ul><ul><li>end; </li></ul>
38. 38. The If..then..else statement <ul><li>Sometimes you want to do one of two things depending on the situation e.g.: if tired then stay in and watch DVD else go clubbing . </li></ul><ul><li>Once again this is common in programming. </li></ul><ul><li>Be careful with the semi colons (don’t use on these subroutines) </li></ul><ul><li>if myint = 10 then begin </li></ul><ul><li>writeln(‘Ten’); </li></ul><ul><li>end </li></ul><ul><li>Else begin </li></ul><ul><li>writeln(‘not ten’); </li></ul><ul><li>end </li></ul><ul><li>end; </li></ul>
39. 39. The If..then..else if statement <ul><li>Occasionally we are fussy beyond belief and we let a single thing control our life e.g.: if very tired then go to bed else if a bit tired watch DVD else go clubbing . </li></ul><ul><li>if myint = 10 then begin </li></ul><ul><li>writeln(‘Ten’); </li></ul><ul><li>end </li></ul><ul><li>Else if myint <10 then begin </li></ul><ul><li>writeln(‘Less than Ten’); </li></ul><ul><li>end </li></ul><ul><li>Else begin </li></ul><ul><li>writeln(‘more than Ten’); </li></ul><ul><li>end </li></ul><ul><li>end; </li></ul>
40. 40. Exercise C <ul><li>Write an application (using if) to take a number input and output if the number is: </li></ul><ul><ul><li>“smaller than 100” </li></ul></ul><ul><ul><li>“Between 100 and 200” </li></ul></ul><ul><ul><li>“Bigger than 200” </li></ul></ul>
41. 41. Messy ifs <ul><li>If you are going to use lots of ifs things can get messy in your code. </li></ul><ul><li>Imagine the code to convert decimal numbers to text. </li></ul><ul><li>if myint=10 then begin </li></ul><ul><li>writeln(‘Ten’); </li></ul><ul><li>end </li></ul><ul><li>Else if myint=9 then begin </li></ul><ul><li>writeln(‘Nine’); </li></ul><ul><li>end </li></ul><ul><li>… </li></ul><ul><li>Else begin </li></ul><ul><li>writeln(‘Only numbers from 1 to 10’); </li></ul><ul><li>end </li></ul><ul><li>end; </li></ul>
42. 42. Case <ul><li>Case can be used to replace a lot of ifs. </li></ul><ul><li>Start the case similar to the if. </li></ul><ul><li>Case can only be used for equality and not: </li></ul><ul><ul><li>< (less than) </li></ul></ul><ul><ul><li>> (more than) </li></ul></ul><ul><ul><li><> (not equal to) </li></ul></ul><ul><li>case Value of </li></ul><ul><li>Case-Instance </li></ul><ul><li>Case-Instance </li></ul><ul><li>Case-Instance </li></ul><ul><li>... </li></ul><ul><li>Case-Instance </li></ul><ul><li>end; </li></ul>
43. 43. Case part 2 <ul><li>Our if statement can be solved like this: </li></ul><ul><li>I have used a lot of careful spacing to keep the appearance of this easy to read. </li></ul><ul><li>Try adding this code to your application. </li></ul><ul><li>case myint of </li></ul><ul><li>10 : begin </li></ul><ul><li>writeln(‘Ten’); </li></ul><ul><li>end; </li></ul><ul><li>9 : begin </li></ul><ul><li>writeln(‘Nine’); </li></ul><ul><li>end; </li></ul><ul><li>… </li></ul><ul><li>0 : begin </li></ul><ul><li>writeln(‘Zero’); </li></ul><ul><li>end </li></ul><ul><li>end; </li></ul>
44. 44. Case part 3 <ul><li>Often you may be given something that doesn’t fit what your conditions are. In these cases we use “otherwise”. </li></ul><ul><li>case myint of </li></ul><ul><li>10 : begin </li></ul><ul><li>writeln(‘Ten’); </li></ul><ul><li>end; </li></ul><ul><li>… </li></ul><ul><li>0 : begin </li></ul><ul><li>writeln(‘Zero’); </li></ul><ul><li>end; </li></ul><ul><li>otherwise begin </li></ul><ul><li>writeln(‘0 – 10 only'); </li></ul><ul><li>end </li></ul><ul><li>end; </li></ul>
45. 45. Exercise D <ul><li>Rewrite the following if statements as a Case statement. </li></ul><ul><ul><li>You will have to get an input to store in “flag” from the user. </li></ul></ul><ul><ul><li>Don’t forget to produce an output of “number”. </li></ul></ul><ul><li>if flag = 1 then begin number := 10; end; </li></ul><ul><li>else if flag = 2 then begin number := 20 ; end; </li></ul><ul><li>else if flag = 3 then begin number := 40 ; end; </li></ul><ul><li>else begin writeln(‘ERROR’); end </li></ul><ul><li>end; </li></ul>
46. 46. For statement <ul><li>Repeats an action a specified number of times </li></ul><ul><li>i is an intiger. </li></ul><ul><li>for i := 1 to 10 do begin //count from 1 till 10 </li></ul><ul><li>write(i, ‘ ’); </li></ul><ul><li>end; </li></ul><ul><li>Produces: </li></ul><ul><li>1 2 3 4 5 6 7 8 9 10 </li></ul>Start number End number Count up to
47. 47. Exercise E <ul><li>Using for write an application to produce the output: </li></ul><ul><li>1 2 3 4 5 6 7 8 </li></ul><ul><li>Using for write an application to produce the output: </li></ul><ul><li>1 </li></ul><ul><li>22 </li></ul><ul><li>333 </li></ul><ul><li>4444 </li></ul><ul><li>55555 </li></ul>
48. 48. While statement <ul><li>Repeats instructions while a condition is met see example below. </li></ul><ul><li>One advantage of this is that the number of times the instruction is to be repeated doesn’t have to be specified when the loop starts, it continues till the condition is met. </li></ul><ul><li>while myint < 20 do begin </li></ul><ul><li>writeln(‘Still not 20.. Adding 1'); </li></ul><ul><li>myint := myint + 1; </li></ul><ul><li>end; </li></ul>Repeat this while “myint” is less than 20
49. 49. Exercise F <ul><li>Write a while loop to display the following output: </li></ul><ul><li>A B C D E F </li></ul><ul><li>Write a while loop to produce the below output: </li></ul><ul><li>1 2 3 4 5 6 </li></ul><ul><li>Write a while loop to ask a user to enter a number till they enter a number over 20 </li></ul>
50. 50. Records <ul><li>A record is a collection of data items/variables. </li></ul><ul><li>Essentially this is like a record in a database. </li></ul><ul><li>An example record could be a person with attributes which you can look at, change or manipulate e.g. name, height, age, gender. </li></ul>
51. 51. Record example part 1 <ul><li>Define a record similar to other enumerated data types. </li></ul><ul><li>Define the data types within the record. </li></ul><ul><li>End the type definition </li></ul><ul><li>Declare two variables of type “Person” </li></ul><ul><li>Type </li></ul><ul><li>Person = record </li></ul><ul><li>Name : string; </li></ul><ul><li>Age : integer; </li></ul><ul><li>City, State : String; </li></ul><ul><li>  end; </li></ul><ul><li>var </li></ul><ul><li>fred : Person; </li></ul><ul><li>garry: Person; </li></ul>
52. 52. Record example part 2 <ul><li>Begin application </li></ul><ul><li>Assign some variables </li></ul><ul><li>Output the text strings </li></ul><ul><li>Output the two ages added together </li></ul><ul><li>Read a character to keep application open </li></ul><ul><li>begin </li></ul><ul><li>fred.Name := 'fred'; </li></ul><ul><li>garry.Name:= 'gary'; </li></ul><ul><li>fred.Age := 20; </li></ul><ul><li>garry.Age := 30; </li></ul><ul><li>write(fred.name, ' and '); </li></ul><ul><li>write(garry.name); </li></ul><ul><li>write(' combined age: '); </li></ul><ul><li>write(fred.Age + garry.Age); </li></ul><ul><li>readln; //takes any character </li></ul><ul><li>end. </li></ul>fred and garry combined age: 50 Output:
53. 53. Exercise G <ul><li>Write an application to create 3 records storing the following aspects of people: </li></ul><ul><ul><li>Name </li></ul></ul><ul><ul><li>Age </li></ul></ul><ul><ul><li>Gender </li></ul></ul><ul><ul><li>Nationality </li></ul></ul><ul><li>Ask a user to enter this information </li></ul><ul><li>Output the name of the oldest person </li></ul>
54. 54. Arrays <ul><li>Suppose you wanted to read in 5000 integers and do something with them. How would you store the integers? </li></ul><ul><li>You could use 5000 variables… </li></ul><ul><li>Int1, int2, int3, int4 ……… int5000 : integer; </li></ul><ul><ul><li>Slow to type and wrist ache by about int1248 </li></ul></ul><ul><ul><li>And that’s before assigning values </li></ul></ul><ul><li>An array contains several storage spaces, all the same type. You refer to each storage space with the array name and with an id number. The type definition is: </li></ul><ul><li>type </li></ul><ul><li>noarray = array [1..50] of integer; </li></ul>
55. 55. Array example <ul><li>Type definition </li></ul><ul><li>Variable declaration </li></ul><ul><li>type </li></ul><ul><li>noarray = array [1..10] of integer; </li></ul><ul><li>var </li></ul><ul><li>myarray : noarray; </li></ul><ul><li>i : integer; </li></ul><ul><li>Continues next slide… </li></ul>
56. 56. Array example part 2 <ul><li>Start application </li></ul><ul><li>Assign variables to array locations 1 - 5 </li></ul><ul><li>begin </li></ul><ul><li>myarray[1] := 6; </li></ul><ul><li>myarray[2] := 3; </li></ul><ul><li>myarray[3] := 10; </li></ul><ul><li>myarray[4] := 14; </li></ul><ul><li>myarray[5] := 16; </li></ul><ul><li>Continues next slide… </li></ul>
57. 57. Array example part 3 <ul><li>Use for loop to assign remaining variables </li></ul><ul><ul><li>Note: not starting from 1 </li></ul></ul><ul><ul><li>Note use of “i” to address array location </li></ul></ul><ul><li>Output array contents using for loop </li></ul><ul><li>readln to prevent application from closing. </li></ul><ul><li>for i:= 6 to 10 do begin </li></ul><ul><li>myarray[i]:= 1; </li></ul><ul><li>end; </li></ul><ul><li>for i:=1 to 10 do begin </li></ul><ul><li>write('myarray pos: '); </li></ul><ul><li>write(i); </li></ul><ul><li>write(': '); </li></ul><ul><li>write(myarray[i]); </li></ul><ul><li>writeln(''); </li></ul><ul><li>end; </li></ul><ul><li>readln; //takes any character </li></ul><ul><li>end. </li></ul>
58. 58. Array example output <ul><li>myarray pos: 1: 6 </li></ul><ul><li>myarray pos: 2: 3 </li></ul><ul><li>myarray pos: 3: 10 </li></ul><ul><li>myarray pos: 4: 14 </li></ul><ul><li>myarray pos: 5: 16 </li></ul><ul><li>myarray pos: 6: 1 </li></ul><ul><li>myarray pos: 7: 1 </li></ul><ul><li>myarray pos: 8: 1 </li></ul><ul><li>myarray pos: 9: 1 </li></ul><ul><li>myarray pos: 10: 1 </li></ul>
59. 59. Exercise H <ul><li>Use an array to take in 10 numbers from a user. </li></ul><ul><li>Output the largest number </li></ul><ul><li>Output the smallest number </li></ul><ul><li>Output the average number </li></ul>
60. 60. Multi dimensional arrays <ul><li>So far we have seen arrays with one direction (1 – 10 in the example). </li></ul><ul><li>You can have more than one dimension if you wish. </li></ul><ul><li>This is good for tables, grids and games. </li></ul><ul><li>type </li></ul><ul><li>StatusType = (X, O, Blank); </li></ul><ul><li>BoardType = array[1..3,1..3] of StatusType; </li></ul><ul><li>var </li></ul><ul><li>Board : BoardType; </li></ul><ul><li>Board[1,1]:= ‘X’ </li></ul>
61. 61. More dimensions <ul><li>If you have a need or are just completely mad an Array can have more dimensions than you can draw. </li></ul><ul><li>All you have to do is add more commas in the declaration: </li></ul><ul><li>Myarray : [1..10, 1..10, 1..10…] of arraytype; </li></ul><ul><li>This idea was used/discussed in </li></ul><ul><li>the cult film “cube 2: hypercube” </li></ul><ul><li>See right for a tesseract </li></ul><ul><li>(hypercube) multi dimensional </li></ul><ul><li>square </li></ul>
62. 62. Procedures & Functions <ul><li>Procedures and Functions are stand alone code that can be “called” from any other code to perform a task. </li></ul><ul><li>Functions always return a value (of a predefined type). </li></ul><ul><li>Both can (and often do) take arguments (data items) eg: </li></ul><ul><li>Procedure DoSomething (Para : String);   begin     Writeln (’Got parameter : ’,Para);     Writeln (’Parameter in upper case : ’,Upper(Para));   end; </li></ul><ul><li>Procedures are used for buttons in Lazarus. </li></ul>Argument variable name use within this function Data type
63. 63. Procedures & Functions (comparison) <ul><li>Taken from AQA mark scheme: </li></ul><ul><ul><li>function returns a value // function has a (data) type // </li></ul></ul><ul><ul><li>function appears in an expression // </li></ul></ul><ul><ul><li>function appears on the RHS (Right Hand Side) of an assignment statement; </li></ul></ul><ul><ul><li>procedure does not have to return a value // </li></ul></ul><ul><ul><li>procedure forms a statement on its own; </li></ul></ul>
64. 64. Function example <ul><li>Take 2 numbers and add them together returning the result (It is trivial but demonstrates the idea). </li></ul><ul><li>function myadd(x, y : integer) : integer ; </li></ul><ul><li>begin </li></ul><ul><li>myadd := x + y; </li></ul><ul><li>end; </li></ul><ul><li>var </li></ul><ul><li>num1, num2, num3: integer; </li></ul>Standard variable declaration Define that this is a function Give it a name Data & data types to be passed to function (names for internal use) all within brackets. Data type of return value Begin & end of function The return of the function is decided by assigning a value to a variable, which has to be the name of the function.
65. 65. Function example part 2 <ul><li>Call the function </li></ul><ul><li>begin </li></ul><ul><li>num1 := 2; </li></ul><ul><li>num2 := 3; </li></ul><ul><li>num3 := myadd(num1, num2); </li></ul><ul><li>writeln (‘solution: ’, num3); </li></ul><ul><li>end. </li></ul>
66. 66. Exercise I <ul><li>Write an application that takes in two numbers </li></ul><ul><li>Using a procedure produce a welcome message </li></ul><ul><li>Using a function add the two numbers together outputting the result </li></ul><ul><li>Using a function multiply the two numbers outputting the result </li></ul>
67. 67. Binary converter example <ul><li>This application converts from decimal to binary and from binary to decimal. </li></ul><ul><li>Simple but uses lots of aspects of programming in Pascal. </li></ul>
68. 68. <ul><li>program binary(input,output); </li></ul><ul><li>uses </li></ul><ul><li>Classes; </li></ul><ul><li>procedure main;forward; // tell compiler that procedure </li></ul><ul><li>//exists as it is called before it is declared in code </li></ul><ul><li>const //Define constants (these don't change) </li></ul><ul><li>{these allow for the application to be easily converted to </li></ul><ul><li> perform different conversions} </li></ul><ul><li>eight : integer =128; //value of highest column </li></ul><ul><li>bit : integer =8; //how many bit binary to work with </li></ul><ul><li>lastcol : integer =1; //what the value of the last column is </li></ul><ul><li>maxno : integer =255; //what the largest number to convert is </li></ul>
69. 69. <ul><li>procedure tobinary; </li></ul><ul><li>Var //variables only local to procedure </li></ul><ul><li>number:integer; </li></ul><ul><li>column:integer; </li></ul><ul><li>row:integer; </li></ul><ul><li>Begin //begin procedure </li></ul><ul><li>column:=eight; </li></ul><ul><li>number:=maxno+1; {set number to one more than the maximum allowed to invoke the below loop} </li></ul><ul><li>while (number >maxno) or (number <0) do //gets run first time </li></ul><ul><li>begin </li></ul><ul><li>writeln ('the number must be between ',maxno,' and 0'); </li></ul><ul><li>write ('enter the number to convert to binary : '); </li></ul><ul><li>readln (number); //takes input from 1 - 255 </li></ul><ul><li>end; </li></ul><ul><li>writeln ('your number has been converted to ',bit,' bit binary'); </li></ul>
70. 70. <ul><li>write (number,'='); </li></ul><ul><li>while (column >=lastcol) do //while not on the last column </li></ul><ul><li>begin </li></ul><ul><li>if (number >= column) then </li></ul><ul><li>begin </li></ul><ul><li>write ('1'); </li></ul><ul><li>number:=number-column; </li></ul><ul><li>end </li></ul><ul><li>else </li></ul><ul><li>begin </li></ul><ul><li>write ('0'); </li></ul><ul><li>end; </li></ul><ul><li>column:=column div 2 </li></ul><ul><li>end; </li></ul><ul><li>writeln (''); //put in line break </li></ul><ul><li>end; //procedure </li></ul>
71. 71. <ul><li>procedure tonumber; </li></ul><ul><li>Var //local variables </li></ul><ul><li>column:integer; </li></ul><ul><li>number:integer; </li></ul><ul><li>binary:integer; </li></ul><ul><li>Begin //procedure </li></ul><ul><li>column:=eight; </li></ul><ul><li>number:=0; </li></ul><ul><li>writeln ('enter the numbers from left to right'); </li></ul><ul><li>while (column >=lastcol) do </li></ul><ul><li>begin </li></ul><ul><li>write ('enter the number in the ',column,' column : '); </li></ul><ul><li>readln (binary); </li></ul><ul><li>if (binary = 1) or (binary = 0) then </li></ul><ul><li>begin </li></ul><ul><li>number:=number+(column*binary); </li></ul><ul><li>column:=column div 2 </li></ul><ul><li>end </li></ul><ul><li>else </li></ul><ul><li>begin </li></ul><ul><li>writeln ('error/...you must enter a value of 1 or 0'); </li></ul><ul><li>end; </li></ul><ul><li>end; </li></ul><ul><li>writeln ('the total of the binary entered is : ',number); </li></ul><ul><li>end; //procedure </li></ul>
72. 72. <ul><li>procedure main; </li></ul><ul><li>Var //internal variable </li></ul><ul><li>answer:char; </li></ul><ul><li>begin </li></ul><ul><li>writeln ('if you want to convert a number to binary type b'); </li></ul><ul><li>writeln ('if you want to convert binary to a number type n'); </li></ul><ul><li>readln (answer); </li></ul><ul><li>if (answer = 'b') or (answer = 'B') then </li></ul><ul><li>tobinary //run tobinary procedure </li></ul><ul><li>else if (answer = 'n') or (answer = 'N') then </li></ul><ul><li>tonumber //run tonumber procedure </li></ul><ul><li>else //give error message </li></ul><ul><li>writeln ('error/... you have to type in b or n') </li></ul><ul><li>end; </li></ul>
73. 73. <ul><li>Var //application wide variable </li></ul><ul><li>reply:char; </li></ul><ul><li>Begin //application </li></ul><ul><li>main; //call main procedure </li></ul><ul><li>writeln ('please type any key then press enter to quit'); </li></ul><ul><li>readln; //prevent window from closing </li></ul><ul><li>end. //application </li></ul>
74. 74. <ul><li>Var //application wide variable </li></ul><ul><li>reply:char; </li></ul><ul><li>Begin //application </li></ul><ul><li>main; //call main procedure </li></ul><ul><li>writeln ('please type any key then press enter to quit'); </li></ul><ul><li>readln; //prevent window from closing </li></ul><ul><li>end. //application </li></ul>
75. 75. Lazarus & GUI <ul><li>After some time of looking at command line programming it feels evident that anything reflecting the type of application that you are used to using feels 100 million miles away. </li></ul><ul><li>Fortunately you don’t have to write all the code to draw the pretty pictures and boxes on the screen. </li></ul><ul><li>The GUI components have been held back to keep things simple. </li></ul><ul><li>Now on to Windows, Icons, Menus and Pointers for the WIMPs out there. </li></ul>
76. 76. First GUI application <ul><li>Since we have covered the code basics most of this is just point and click. </li></ul><ul><li>The aim is to make an application to do the following: </li></ul><ul><ul><li>Take two inputs </li></ul></ul><ul><ul><li>Provide a button to add them together displaying the result in a third box </li></ul></ul><ul><ul><li>Provide a button to subtract them displaying the result in a third box </li></ul></ul><ul><ul><li>Provide a button to divide them displaying the result in a third box </li></ul></ul><ul><ul><li>Provide a button to divide them displaying the result in a third box </li></ul></ul>
77. 77. Appearance addbtn subbtn multbtn divbtn output1 input2 input1 <ul><li>Starting code on next slide (all created by Lazarus) </li></ul>
78. 78. Starting code <ul><li>unit Unit1; </li></ul><ul><li>{\$mode objfpc}{\$H+} </li></ul><ul><li>interface </li></ul><ul><li>uses </li></ul><ul><li>Classes, SysUtils, LResources, Forms, Controls, Graphics, Dialogs, StdCtrls, Buttons; </li></ul><ul><li>Type </li></ul><ul><li>{ TForm1 } </li></ul><ul><li>TForm1 = class(TForm) </li></ul><ul><li>addbtn: TButton; </li></ul><ul><li>Label1: TLabel; </li></ul><ul><li>subbtn: TButton; </li></ul><ul><li>multbtn: TButton; </li></ul><ul><li>divbtn: TButton; </li></ul><ul><li>input1: TEdit; </li></ul><ul><li>input2: TEdit; </li></ul><ul><li>output1: TEdit; </li></ul><ul><li>private </li></ul><ul><li>{ private declarations } </li></ul><ul><li>public </li></ul><ul><li>{ public declarations } </li></ul><ul><li>end; </li></ul><ul><li>var </li></ul><ul><li>Form1: TForm1; </li></ul><ul><li>implementation </li></ul><ul><li>{ TForm1 } </li></ul><ul><li>initialization </li></ul><ul><li>{\$I unit1.lrs} </li></ul><ul><li>end. </li></ul>
79. 79. Drag & drop Click on button to select “button” Drag and drop button on form Object inspector used to change properties
80. 80. Edit properties Change “text” property to change the default contents Look at other properties for ideas Commonly used properties are displayed under “favourites” Don’t forget to find the “name” property to give each item a memorable name (see the slide with the boxes & buttons labelled for ideas Events are used for things like “on click”
81. 81. Make a button work <ul><li>Select the button you want to give a function to. </li></ul><ul><li>Go to the “Events” section of the object browser. </li></ul><ul><li>Find “OnClick” and select it </li></ul><ul><li>Click the button with three dots to the end of it </li></ul><ul><li>Lazarus will create code in the source editor. All we are interested in is between: </li></ul><ul><li>procedure TForm1.addbtnClick(Sender: TObject); </li></ul><ul><li>begin </li></ul><ul><li>And the: </li></ul><ul><li>End; </li></ul>
82. 82. Code to make things happen <ul><li>Declare some local variables (between the procedure declaration and the “begin”. </li></ul><ul><li>var </li></ul><ul><li>num1, num2, num3 : integer; </li></ul><ul><li>Copy the contents of the two input boxes to the first to integers: </li></ul><ul><li>num1 := strtoint(input1.Text); </li></ul><ul><li>num2 := strtoint(input2.Text); </li></ul><ul><li>Now we are ready to do some maths work </li></ul>Use a function to convert string to integer Just like calling the contents of a record
83. 83. Maths & Output <ul><li>num3 := num1 + num2; //normal Pascal addition </li></ul><ul><li>Output1.Text := inttostr(num3); </li></ul>Just like setting contents of record Function to convert integer to string
84. 84. Compile and run <ul><li>Your application will now add two numbers together. </li></ul><ul><li>To extend the application just repeat the steps to add functionality to the other buttons. </li></ul><ul><li>Tip… you may wish to use real numbers for the division result (or any others). In which case you need to use these functions: </li></ul><ul><ul><li>Floattostr() </li></ul></ul><ul><ul><li>Strtofloat() </li></ul></ul>
85. 85. Going further <ul><li>GUI programming is a combination of normal Pascal programming and manipulating the properties of objects (which behave just like records). </li></ul><ul><li>Manipulating objects (or rather instances of objects) is all about changing properties. Providing you supply the correct data type the world is your oyster. </li></ul>
86. 86. More information <ul><li>Further tuition (online instructions etc…) can be found here: </li></ul><ul><li>http://www.taoyue.com/tutorials/pascal/ </li></ul><ul><li>Lazarus is available from here: </li></ul><ul><li>http://www.lazarus.freepascal.org/ </li></ul><ul><li>Lazarus tutorials & more: </li></ul><ul><li>http://wiki.lazarus.freepascal.org/Lazarus_Tutorial </li></ul>