2 What is a Computer Program? A program is a set of step-by-stepinstructions to the computer telling it tocarry out the tasks that you want it to doto produce the results you want.
3 coding (the construction phase) What is Programming?Programming consists of two distinct steps:algorithmic design (the problem solving stage, analogous to the work of an architect designing a building)coding (the construction phase)
4 What do I need to support algorithm design? Some timeA penSome paperWe will have a great deal to say about algorithm design in the remainder of this module. But for now we will focus on coding issues
5 What do I need to Support Coding? A programming language to express your ideasa set of tools to design, edit, and debug your codeeithera compiler to translate your programs to machine codea machine to run the executable codeoran interpreter to translate and execute your program
6 Levels of Programming Languages Machine languageAssembly LanguageHigh Level Languages
7 Machine LanguageActual binary code that gives basic instructions to the computer.These are usually simple commands like adding two numbers or moving data from one memory location to another.Different for each computer processor
8 Assembly LanguageA way for humans to program computers directly without memorizing strings of binary numbers.There is a one-to-one correspondence with machine code.For example ADD and MOV are mnemonics for addition and move operations that can be specified in single machine language instructions.Different for each computer processor
9 High-level languagePermits humans to write complex programs without going step-by step.High-level languages include Pascal, C, C++, FORTRAN, Java, Visual Basic, and many more.One command in a high-level language may translate to tens of machine language instructions.
10 Computers can only run machine language programs directly.Assembly language programs are assembled, ortranslated into machine language.Likewise, programs written in high-level languages,like Pascal, must also be translated into machinelanguage before they can be run. To do thistranslation is to compile a program.Let’s take a look at a development environmentbased on a Pascal Compiler
11 Introduction to a High-Level Development Environment Free Pascal The compiler and development environment referred to here can be downloaded from
12 The Integrated Development Environment (IDE) screen immediately after startup
13 Creating a New Program Press ALT then select “File” then “New” You should get the screen below
14 Entering CodeType in the code below – save it by selecting File>Save or by pressing F2
15 Points to Note Program ends with a Full Stop This line indented using TAB keyLines end with a semicolon
16 Compile and RunHaving created the program we now need to compile it to create an executable (machine code) fileDo this by pressing ALT then selecting Compile – or press ALT F9If there are no errors you will now have an executable program to run it press ALT then select Run
17 Compile and RunWhen the program runs you will get a brief glimpse of the output then you are returned to the IDETo see more of the output press ALT F5 or select Debug>User Screen.When you have inspected the output press ALT F5 again to get back to the IDE
18 Points to NoteThe Free Pascal website contains lots of documentation to help you learn to work in the environmentThe compiler will help you identify any mistakes in your programsWork through the tutorial and practical exercises associated with these lecture slides to gain some familiarity with the environment
19 Another Useful Website To supplement the practical exercises work through the first few exercises in the online tutorial at:
20 Programming Experience If you have a little programming experience you should have no difficulty picking up the basics of Pascal from the online tutorialIf you have no programming experience you will need to work through them more thoroughlyYou do not need to become an expert Pascal programmer for this module but you need to be comfortable with the basicsIncluding how to work with data
21 A Few Words about Variables and Data types This topic is covered in the online tutorials – if you find the ideas difficult be sure to spend some time working through them.
22 Use of variablesIn order to solve problems programs may need to hold information for a short while or for their entire duration.This may be necessary when making comparisons or processing data into another form.This information is held in VARIABLES
23 What are variables? A memory location used to store data Where the values can changeWhere the variables are given a type which determines:The kind of data to storeThe size of the data storage facilityThe location of the store
24 VariablesVariables are memory storage areas which as the name suggests are capable of changing their contents.For example a variable called number may hold a number 10, this may then be changed to 8 and then to –6.Changing the value of a variable involves destroying the record in memory and replacing it with a new value.To identify a variable you must supply it with a name so that the computer knows where to store new values and retrieve existing ones from.Variable names must always begin with a letter, e.g. Name, City, TR500 etc. but not 51Avenue.
25 Data TypesDifferent types of data can be stored in the computer’s memory and it is important the you understand and are comfortable dealing with the different types.The types you are most likely to use are: String, Integer, Real, Currency, Date and Boolean.
26 Variable TypesReal - A number with something after the decimal point (e.g. 3.5)Integer - A whole number with nothing after the decimal point. (e.g. 3)String – any ASCII symbols or characters - (e.g. “Name”)Boolean - True or FalseDate – from Jan1 001 to Dec 31st 9999
27 Declaring Variables Give every variable a name You can include numbers, letters and underscore characters but no spaces or keywords.Don’t begin with a number though!Use meaningful wordsDon’t make the words too long – you may have to type them in a few times!
28 Declaring VariablesIt is good programming practice to declare variables that are to be used with the variable names.This can be done at the beginning of an event procedure.ExamplesVar Temp : StringVar Count : Integer
29 Assigning values Assign values using the := symbol As a declaration, X:=1 does not mean that the value of X is the same as 1, it means assign X the value of 1. This value will be stored until a new value is stored in it.ExampleX := 3Y := 6X := 4Z := 1What is X now?X = 4
30 Using ConstantsConstants hold values similar to variables, but as the name suggests the value cannot change.Use it for values such as Pi, g, conversion factors etc.The values are set at compile time rather than runtime which speeds things up.Constant names should always be in upper case and are set using the Const keyword.They are declared like this:Const CL_Per_BOTTLE : integer;Const PI : real;
31 This is not a course in Pascal programming But Pascal is a very useful environment for testing out our algorithmsBe prepared to spend some time at this stage becoming familiar with the development environmentThis will pay off later when we consider the design of more complex programs
32 Review QuestionsWhat is machine code how does it differ from high-level program code?Name some commonly used high-level languages. What are the advantages of using high-level languages?What is the overall structure of a Pascal program?How is the semicolon used in Pascal? When must it be used?
33 Review QuestionsWhat punctuation mark comes at the end of every Pascal program?What is the difference between compilation and execution of a program?What syntax errors are novice Pascal programmers most likely to make?
34 Research Questions Who was Blaise Pascal? Who developed the Pascal language? What was its original purpose?