Introduction to a Programming Environment. What is a Computer Program? A program is a set of...

34
Introduction to a Programming Environment
  • date post

    21-Dec-2015
  • Category

    Documents

  • view

    220
  • download

    1

Transcript of Introduction to a Programming Environment. What is a Computer Program? A program is a set of...

Page 1: 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.

Introduction to a Programming Environment

Page 2: 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.

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.

Page 3: 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.

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)

Page 4: 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.

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

Page 5: 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.

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

Page 6: 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.

Levels of Programming Languages

• Machine language

• Assembly Language

• High Level Languages

Page 7: 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.

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

Page 8: 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.

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

Page 9: 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.

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.

Page 10: 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.

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

Page 11: 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.

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/

Page 12: 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.

The Integrated Development Environment (IDE) screen immediately

after startup

Page 13: 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.

Creating a New Program

• Press ALT then select “File” then “New”

• You should get the screen below

Page 14: 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.

Entering Code

Type in the code below – save it by selecting File>Save or by pressing F2

Page 15: 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.

Points to Note

Lines end with a semicolon

Program ends with a Full Stop

This line indented using TAB key

Page 16: 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.

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

Page 17: 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.

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

Page 18: 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.

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

Page 19: 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.

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

Page 20: 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.

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

Page 21: 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.

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.

Page 22: 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.

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

Page 23: 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.

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

Page 24: 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.

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.

Page 25: 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.

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.

Page 26: 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.

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

Page 27: 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.

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!

Page 28: 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.

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

Page 29: 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.

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

Page 30: 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.

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;

Page 31: 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.

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

Page 32: 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.

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?

Page 33: 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.

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?

Page 34: 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.

Research Questions

• Who was Blaise Pascal?

• Who developed the Pascal language? What was its original purpose?