Introduction to a Programming Environment
What is a Computer Program? A program is a set of step-by-step instructions to the computer telling it to carry out the tasks that you want it to do to produce the results you want.
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)
What do I need to support algorithm design? Some time A pen Some paper We 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
What do I need to Support Coding? A programming language to express your ideas a set of tools to design, edit, and debug your code either a compiler to translate your programs to machine code a machine to run the executable code or an interpreter to translate and execute your program
Levels of Programming Languages Machine language Assembly Language High Level Languages
Machine Language Actual 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
Assembly Language A 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
High-level language Permits 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.
Computers can only run machine language programs directly. Assembly language programs are assembled, or translated into machine language. Likewise, programs written in high-level languages, like Pascal, must also be translated into machine language before they can be run. To do this translation is to compile a program. Let’s take a look at a development environment based on a Pascal Compiler
Introduction to a High-Level Development Environment Free Pascal The compiler and development environment referred to here can be downloaded from http://www.freepascal.org/
The Integrated Development Environment (IDE) screen immediately after startup
Creating a New Program Press ALT then select “File” then “New” You should get the screen below
Entering Code Type in the code below – save it by selecting File>Save or by pressing F2
Points to Note Program ends with a Full Stop This line indented using TAB key Lines end with a semicolon
Compile and Run Having created the program we now need to compile it to create an executable (machine code) file Do this by pressing ALT then selecting Compile – or press ALT F9 If there are no errors you will now have an executable program to run it press ALT then select Run
Compile and Run When the program runs you will get a brief glimpse of the output then you are returned to the IDE To 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
Points to Note The Free Pascal website http://www.freepascal.org/ contains lots of documentation to help you learn to work in the environment The compiler will help you identify any mistakes in your programs Work through the tutorial and practical exercises associated with these lecture slides to gain some familiarity with the environment
Another Useful Website To supplement the practical exercises work through the first few exercises in the online tutorial at: http://www.taoyue.com/tutorials/pascal/contents.html
Programming Experience If you have a little programming experience you should have no difficulty picking up the basics of Pascal from the online tutorial If you have no programming experience you will need to work through them more thoroughly You do not need to become an expert Pascal programmer for this module but you need to be comfortable with the basics Including how to work with data
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.
Use of variables In 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
What are variables? A memory location used to store data Where the values can change Where the variables are given a type which determines: The kind of data to store The size of the data storage facility The location of the store
Variables Variables 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.
Data Types Different 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.
Variable Types Real - 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 False Date – from Jan1 001 to Dec 31st 9999
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 words Don’t make the words too long – you may have to type them in a few times!
Declaring Variables It 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. Examples Var Temp : String Var Count : Integer
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. Example X := 3 Y := 6 X := 4 Z := 1 What is X now? X = 4
Using Constants Constants 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;
This is not a course in Pascal programming But Pascal is a very useful environment for testing out our algorithms Be prepared to spend some time at this stage becoming familiar with the development environment This will pay off later when we consider the design of more complex programs
Review Questions What 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?
Review Questions What 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?
Research Questions Who was Blaise Pascal? Who developed the Pascal language? What was its original purpose?