Sie sind auf Seite 1von 32

Where are we?

We looked at problem solving


Writing algorithms Drawing flowcharts

Trace table

Now Use of a programming language to introduce the process of writing short programs.

Ms. Douglas and Mr. L:ucas

Programming
Programming is the art of writing the solution to a problem using a language that a computer can understand.
A programmer instructs the computer how to solve a problem since it cannot come up with a solution all by itself. (The person that write programs)

Computer Program
A computer program is a set of instructions that tells a computer what to do and how to do it. These instructions are converted into a sequence of numeric codes called machine code which is stored in memory. CPU interprets the code in order to carry out the instructions of the program

Types of Programming language Low-level language

High-level language

Low-level Language
Language is machine dependent. Other words, codes written can only be understood by the particular computer or processor used to write the code.
Types of low-level language: First generation language Second generation language

First generation language


It is called machine language. It used the digits 0 and 1 to make up binary code.

Second generation language


It is called assembly language. It has the same structure and commands as machine language but instead of using binary to write codes it used equivalent assembly language code. E.g. 10110000 01100001 : add A, B Advantage: Can be easily converted to machine code by a program called an assembler Disadvantage: Still difficult to understand compared to the high-level language. It is machine dependent.

Advantage: Code runs very fast and efficiently because it is directly executed by the CPU Disadvantage: Programmer may become confused with the massive amount of 0s and 1s in the program. It is machine dependent.

High-Level Languages
High level language are not machine dependent.

Therefore programs written on one computer can be used on another similar computer. Keywords used are similar to English and easier to write Types of high-level language Third generation languages (3GLs) Fourth generation languages (4GLs) Fifth generation language (5GLs)

3GLs
These languages are converted to machine code. E.g. FORTRAN, BASIC, C

4GLs
Non-procedural: programs such as COBOL are written to provide easy ways of designing screens and reports and using databases. They contain commands to read and process the data and place the results in report-form on the page

5GLs
Very high level languages. 5GLs are non-procedural languages meaning that the programmer states the goal to be achieved but not the steps required in order to achieve the goal. E.g. Prolog

Advantage: Can use English-type words to write program code, making it easier to create.

Advantage: Useful for generating reports

Advantage: Computers will be able to communicate in natural spoken language with their users
Disadvantage: Very complex to design, programmer must be highly trained.

Disadvantage: Programs Disadvantage: also have to be converted Can become very wordy. to machine language (binary).

Third Generation Language


FORTRAN (Formula Translation) - First high-level language, used to

express mathematical formulae, Scientific problem and engineer problem.


BASIC (Beginners All-purpose Symbolic Instruction Code) - Very easy

to learn language that was creating teach programming.


COBOL (Common Business Oriented Language) A language used in

commercials and business.


PASCAL ( named after the 17th century Mathematician Blaise Pascal)

use for teaching and alternative to BASIC

Third Generation Language


C A language used mostly to write Operating

Systems, Database Management Software and Scientific Application.


C++ - An Object Oriented Programming Language

that was originally an extension of C.


PROLOG (Programming Logic) A language used to

develop artificial intelligence.

Review Questions
1) Explain difference between low-level languages and

high level languages. 2) State the generations of computer languages. 3) State the programming language that is associated with:
Artificial intelligence applications b. Business applications c. Operating systems d. Scientific Problems
a.

Answers
1.

Low-level languages are machine dependent and not easy to understand by humans whereas, high level languages must be converted to machine language and are easier to understand by humans

2. First, second, third, fourth, fifth

3. Prolog; COBOL; C or C++; Fortran

The sequence of steps associated with implementing a program


Computer program is developed through the following

steps:
1) 2)

3)
4)

5)

6)
7)

Defining the problem Analyzing the problem Developing an algorithm or method Write the computer program which implements the algorithm Testing and debugging the program Documenting the program Maintain the program

Defining the Problem


Problem must be well defined so that there is no doubt

as to what is the solution


Must be clearly written so that it can be interpreted to

have only one meaning. Eg. - calculate the sum of two numbers - compute the average of a class - grade the scores of students in a class

Analyzing the problem


The process of finding out the operation is analyzing
Analyzing the problem involves:
Interpreting and understanding the problem Determine:
input

needs processing required output required commands and programming constructs required

Developing Algorithm
Algorithm: set of steps which when followed can lead

to the solution of a problem


Can be written using pseudo code Pseudo code is a language consisting of English like

statement used to represent the steps in an algorithm.


Flow Chart: a pictorial representation of an algorithm.

Programming Terms and Concept

Source Code: original text of a program written in high level language The actual programming language statements is called the source code. Object Code: output from a compiler or assembler. The source code must be translated to machine language. Machine language is the object code

Programming Terms and Concept

Compile or interpret: mean to change a program or translate a program from high level language to machine code. (translators) Two type of translator: Interpreter- translates or converts programming codes line by line

Complier translates or converts all the programming codes at one time

Programming Terms

Dry Run: manually checking through the steps in a program. Testing the program.
Debugging: correcting errors in the source codes of a program. Logics: sequential order and comparison within the program. Logic Error: results that come from sequence instructions, faulty comparison and selection

Programming Terms

Syntax is the very precise way in which the statement in a program must be written in order to be understand.

Therefore , The language of the programming language is the syntax of the language.

Syntax Error: results from not adhering to the rules of a programming language.

Run Time Error: errors occur while the program compiles or runs.

Variable Declaration
Variable hold areas or spaces for data which a

program might use or manipulate. Variables are given names, which can assign values such as number and text. For example, age: integer. Variable store values of a given type. Some data type are:
Integer to store integer or whole number.

Real to store real or functional numbers.


Character a single character such as a letter. String collection of character such as a word.

Variable Declaration
This is how you declare variables in pseudo Code Input Age, Cost, Grade Age= 23 Cost 56.50 Grade = A This is how you declare variables in Pascal Var
Age := integer; Cost := real; Grade:= char;
Age

23 56.50 A

Assign Initial Values:


Age:= 23; Cost:=56.50; Grade:=A;

Cost
Grade

Variable Declaration
To declare a variable means to create the variable by giving it a name and give it a data type (integer, real etc. This is how you declare constants in pseudo Code Input Price = 50, ItemName = cake This is how you declare variables in Pascal and assigning initial values: Const
Price := 50; Cost := Cake;

Reading from and writing to variables, arithmetic operations


Print( Please enter a number) Read (x) reading from a variable a= x*x

writing to variable

Program square; Var


x :integer; a :integer; Writeln(Please enter a number); Read (x); a =x*x; Writeln (The Square of, x, is, a);

Begin

End

control structures
Conditional branching: if-then if-then-else, Program grade; Var grade :integer; Begin Writeln(Enter your information technology grade); Read(grade); If (grade >= 50)

Then

Writeln(Pass accepted for cxc)


Writeln( failed, try harder next time)

Else

End

control structures
Loops: While Program whileLoop; Var a:integer; Begin a:= 40; While a <> 0 DO

a=a-5; Writeln( The value of variable a is: , a);

End

control structures
Loops: Repeat Program repeatLoop; Var a:integer; Begin a:= 40; Repeat

a=a-5; Writeln( The value of variable a is: , a);

Until a=0;

control structures
Loops: For Program forLoop; Var a:integer; Begin a:= 40; For m=1 to 8

a=a-5; Writeln( The value of variable a is: , a);

End;

Array
Idimensional array Is a set of data of the same type grouped together and referred to by a single name.
23 33 56 41 79 38
AGE[6]

Position 4

Position 6

Position 2

Position 5

Position 3

Position 1

VAR AGE: ARRAY[1..6] OF INTERGER;

Writing to Array
Writing to Array Program WriteArray; Var

i: integer; Marks: array[1..6] of real;

Begin
writeln(please enter grade);

For i := 1 to 6 do Read(grade); This line write data to variable. Marks [i] := grade; writeln(please enter grade);

End;

Reading from Array


Reading from Array Program WriteArray; Var

i: integer; day: array[1..7] of real;

Begin

Day[1]:= Sunday; Day[2]:= monday; Day[3]:= tuesday; Day[4]:= wednesday; Day[5]:= thursday; Day[6]:= friday; Day[7]:= saturday;

Begin writeln(which day of the week is you favorite day); Read(i) skipLine(3); writeln(Your favorite day is, day[1]);

End;

This line reads data to variable.

Das könnte Ihnen auch gefallen