infs 214: introduction to computing2014/2015 – 2016/2017 infs 214: introduction to computing ......

44
College of Education School of Continuing and Distance Education 2014/2015 2016/2017 INFS 214: Introduction to Computing Session 11Principles of Programming Lecturer: Dr. Ebenezer Ankrah, Dept. of Information Studies Contact Information: [email protected]

Upload: others

Post on 14-Aug-2020

14 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: INFS 214: Introduction to Computing2014/2015 – 2016/2017 INFS 214: Introduction to Computing ... •Some programs are translated using an interpreter. Such programs are translated

College of Education

School of Continuing and Distance Education 2014/2015 – 2016/2017

INFS 214:

Introduction to Computing

Session 11– Principles of Programming

Lecturer: Dr. Ebenezer Ankrah, Dept. of Information Studies Contact Information: [email protected]

Page 2: INFS 214: Introduction to Computing2014/2015 – 2016/2017 INFS 214: Introduction to Computing ... •Some programs are translated using an interpreter. Such programs are translated

Session Overview

• Every piece of software, from a simple word processor, such as Microsoft Notepad, to the most advanced image editing programs, such as Adobe Photoshop, is made by one or more computer programmers who use a programming language that tells a computer how to act.

DR. EBENEZER ANKRAH Slide 2

Page 3: INFS 214: Introduction to Computing2014/2015 – 2016/2017 INFS 214: Introduction to Computing ... •Some programs are translated using an interpreter. Such programs are translated

Session Overview

• Programming is a highly complex subject that covers many different types of languages and can be used to solve a huge range of problems. This session seeks to explain the concept of programming, the types of programming languages and the types of errors that could occur in the course of programming.

DR. EBENEZER ANKRAH Slide 3

Page 4: INFS 214: Introduction to Computing2014/2015 – 2016/2017 INFS 214: Introduction to Computing ... •Some programs are translated using an interpreter. Such programs are translated

Session Overview

• At the end of the session, the student will – Understand and differentiate between the various types of

programming languages

– Be able to give examples of programming languages

– Understand and identify the various types of programming errors

– Be able to identify the phases in the Program Development Cycle

– Be able to write a simple computer program for execution

– Understand and differentiate between the methods of testing a computer program

DR. EBENEZER ANKRAH Slide 4

Page 5: INFS 214: Introduction to Computing2014/2015 – 2016/2017 INFS 214: Introduction to Computing ... •Some programs are translated using an interpreter. Such programs are translated

Session Outline

The key topics to be covered in the session are as follows:

• Concept of Programming

• Types of Programming Languages

• Program Development Cycle

• Types of Errors

DR. EBENEZER ANKRAH Slide 5

Page 6: INFS 214: Introduction to Computing2014/2015 – 2016/2017 INFS 214: Introduction to Computing ... •Some programs are translated using an interpreter. Such programs are translated

Reading List

• O’Leary, T. J., & O’Leary, L. I. (2014). Computer Today. Boston: McGraw Hill. (Chapter 15)

DR. EBENEZER ANKRAH Slide 6

Page 7: INFS 214: Introduction to Computing2014/2015 – 2016/2017 INFS 214: Introduction to Computing ... •Some programs are translated using an interpreter. Such programs are translated

CONCEPT OF PROGRAMMING Topic One

DR. EBENEZER ANKRAH Slide 7

Page 8: INFS 214: Introduction to Computing2014/2015 – 2016/2017 INFS 214: Introduction to Computing ... •Some programs are translated using an interpreter. Such programs are translated

Concept of Programming

• The concept of general-purpose programming in fact predates the development of computers. In the field of mathematical logic in the early 20th century, logicians created their own programming languages.

• Their motivation originally sprang from the concept of a proof system, a set of rules in which logical truths could be derived, mechanically.

DR. EBENEZER ANKRAH Slide 8

Page 9: INFS 214: Introduction to Computing2014/2015 – 2016/2017 INFS 214: Introduction to Computing ... •Some programs are translated using an interpreter. Such programs are translated

Concept of Programming

• The original computer programming languages, as mentioned above, were so-called machine languages: the human and computer programmed in same language.

• Machine language is great for computers but not so great for humans since the instructions are each very simple and so many, many instructions are required. High-level languages were introduced for ease of programmability by humans.

DR. EBENEZER ANKRAH Slide 9

Page 10: INFS 214: Introduction to Computing2014/2015 – 2016/2017 INFS 214: Introduction to Computing ... •Some programs are translated using an interpreter. Such programs are translated

Concept of Programming

• FORTRAN was the first high-level language, developed in 1957 by a team led by Backus at IBM. FORTRAN programs were translated (compiled) into machine language to be executed.

• They didn't run as fast as hand-coded machine language programs, but FORTRAN nonetheless caught on very quickly because FORTRAN programmers were much more productive. A swarm of early languages followed: ALGOL in '58, Lisp in the early 60's, PL/1 in the late 60's, and BASIC in 1966.

DR. EBENEZER ANKRAH Slide 10

Page 11: INFS 214: Introduction to Computing2014/2015 – 2016/2017 INFS 214: Introduction to Computing ... •Some programs are translated using an interpreter. Such programs are translated

Concept of Programming

• A computer program is a set of instructions written in a computer language in order to be executed to perform a specific task.

• A programmer is a person who writes the required computer programs. Programmers translate the expected tasks given in human understandable form into machine understandable form by using compilers and interpreters.

DR. EBENEZER ANKRAH Slide 11

Page 12: INFS 214: Introduction to Computing2014/2015 – 2016/2017 INFS 214: Introduction to Computing ... •Some programs are translated using an interpreter. Such programs are translated

Questions

• Individual Assignment:

– Differentiate between the types of programming errors

• Forum Question:

– Discuss the phases in the Program Development Cycle

DR. EBENEZER ANKRAH Slide 12

Page 13: INFS 214: Introduction to Computing2014/2015 – 2016/2017 INFS 214: Introduction to Computing ... •Some programs are translated using an interpreter. Such programs are translated

TYPES OF PROGRAMMING LANGUAGES

Topic Two

DR. EBENEZER ANKRAH Slide 13

Page 14: INFS 214: Introduction to Computing2014/2015 – 2016/2017 INFS 214: Introduction to Computing ... •Some programs are translated using an interpreter. Such programs are translated

Types of Programming Languages

• A programming language is a computer language with its own syntax and grammar. Both systems and application software are written in programming languages.

• A programming language is a special purpose and limited language. It is a set of rules and symbols used to construct a computer program. A language used to interact with the computer. Programming languages allow programmers to code software.

DR. EBENEZER ANKRAH Slide 14

Page 15: INFS 214: Introduction to Computing2014/2015 – 2016/2017 INFS 214: Introduction to Computing ... •Some programs are translated using an interpreter. Such programs are translated

Types of Programming Languages

• There are three major families of programming languages. These are:

– Machine languages

– Assembly languages

– High-Level languages

DR. EBENEZER ANKRAH Slide 15

Page 16: INFS 214: Introduction to Computing2014/2015 – 2016/2017 INFS 214: Introduction to Computing ... •Some programs are translated using an interpreter. Such programs are translated

Types of Programming Languages

• Machine Languages

• The machine language comprised of 1s and 0s. It is the “native” language of a computer system. It is difficult to program – one misplaced 1 or 0 will cause the program to fail.

• In early years of computer revolution, programs were written in machine language, using the binary symbols 1 and 0. All numbers, characters and special symbols were represented by eight-bit codes.

DR. EBENEZER ANKRAH Slide 16

Page 17: INFS 214: Introduction to Computing2014/2015 – 2016/2017 INFS 214: Introduction to Computing ... •Some programs are translated using an interpreter. Such programs are translated

Types of Programming Languages

• This is also considered a low-level language because there is no programming scheme less sophisticated than the binary code.

• Example: 00000010 00100101 00000011 = 2 + 3

• Example of codes are 1110100010101, 111010101110 10111010110100 and 10100011110111

DR. EBENEZER ANKRAH Slide 17

Page 18: INFS 214: Introduction to Computing2014/2015 – 2016/2017 INFS 214: Introduction to Computing ... •Some programs are translated using an interpreter. Such programs are translated

Types of Programming Languages

• Assembly Languages

• Assembly languages are a step towards easier programming. Assembly languages are comprised of a set of elemental commands which are tied to a specific processor.

• Assembly language code needs to be translated to machine language before the computer processes it. Assembly languages are English-like abbreviations representing elementary computer operations

DR. EBENEZER ANKRAH Slide 18

Page 19: INFS 214: Introduction to Computing2014/2015 – 2016/2017 INFS 214: Introduction to Computing ... •Some programs are translated using an interpreter. Such programs are translated

Types of Programming Languages

• Assembly languages are a symbolic representation of the machine language of a specific processor. It is converted to machine code by an assembler. Usually, each line of assembly code produces one machine instruction (One-to-one correspondence).

• Programming in assembly language is slow and error-prone but is more efficient in terms of hardware performance. Mnemonic representation of the instructions and data.

DR. EBENEZER ANKRAH Slide 19

Page 20: INFS 214: Introduction to Computing2014/2015 – 2016/2017 INFS 214: Introduction to Computing ... •Some programs are translated using an interpreter. Such programs are translated

Types of Programming Languages

• Assembly language replaced binary digits with symbols such as ADD, SUB for addition and subtraction, so that programming became more understandable to humans. Operating systems and utility programs were written in an assembly language.

• Example: ADD 1001010, 1011010, LOAD BASEPAY, ADD OVERPAY STORE GROSSPAY

DR. EBENEZER ANKRAH Slide 20

Page 21: INFS 214: Introduction to Computing2014/2015 – 2016/2017 INFS 214: Introduction to Computing ... •Some programs are translated using an interpreter. Such programs are translated

Types of Programming Languages

• High-Level Languages

• High-level languages represent a giant leap towards easier programming. The syntax of HL languages is similar to English. High-level languages codes are similar to everyday English.

• It uses mathematical notations. A programming language which use statements consisting of English-like keywords such as "FOR", "PRINT", “IF“ etc. Each statement corresponds to several machine language instructions (one-to-many correspondence).

DR. EBENEZER ANKRAH Slide 21

Page 22: INFS 214: Introduction to Computing2014/2015 – 2016/2017 INFS 214: Introduction to Computing ... •Some programs are translated using an interpreter. Such programs are translated

Types of Programming Languages

• Each statement corresponds to several machine language instructions (one-to-many correspondence). It is much easier to program than in assembly language.

• Data are referenced using descriptive names and operations can be described using familiar symbols.

• Example: grossPay = basePay + overTimePay

Cost = Price + Tax

DR. EBENEZER ANKRAH Slide 22

Page 23: INFS 214: Introduction to Computing2014/2015 – 2016/2017 INFS 214: Introduction to Computing ... •Some programs are translated using an interpreter. Such programs are translated

Types of Programming Languages

• These languages were easier to use than assembly and machine language because they relied on natural language (such as English) and used fewer lines of code to execute an instruction.

• Some high-level languages are BASIC, COBOL, FORTRAN, and C.

DR. EBENEZER ANKRAH Slide 23

Page 24: INFS 214: Introduction to Computing2014/2015 – 2016/2017 INFS 214: Introduction to Computing ... •Some programs are translated using an interpreter. Such programs are translated

Types of Programming Languages

• High-Level languages are divided into two groups:

–Procedural languages and

–Object-Oriented languages (OOP).

DR. EBENEZER ANKRAH Slide 24

Page 25: INFS 214: Introduction to Computing2014/2015 – 2016/2017 INFS 214: Introduction to Computing ... •Some programs are translated using an interpreter. Such programs are translated

Types of Programming Languages

• Procedural Languages

• Early high-level languages are typically called procedural languages. Procedural languages are characterized by sequential sets of linear commands. The focus of such languages is on structure. Examples are C, COBOL, Fortran, LISP, Perl, HTML and VBScript.

DR. EBENEZER ANKRAH Slide 25

Page 26: INFS 214: Introduction to Computing2014/2015 – 2016/2017 INFS 214: Introduction to Computing ... •Some programs are translated using an interpreter. Such programs are translated

Types of Programming Languages

• Object-Oriented Languages

• Most object-oriented languages are high-level languages. The focus of OOP languages are not on structure, but on modeling data. Programmers code using “blueprints” of data models called classes. Examples of OOP languages are C++, Visual Basic.NET and Java.

DR. EBENEZER ANKRAH Slide 26

Page 27: INFS 214: Introduction to Computing2014/2015 – 2016/2017 INFS 214: Introduction to Computing ... •Some programs are translated using an interpreter. Such programs are translated

Types of Programming Languages

• What does the Computer understand?

• Computer only understands machine language instructions.

• Assembling

• Instructions written in assembly language must be translated to machine language instructions. An Assembler does this. One Assemble Language instruction is mapped to one Machine Language instruction. Assemble Languages are CPU specific.

DR. EBENEZER ANKRAH Slide 27

Page 28: INFS 214: Introduction to Computing2014/2015 – 2016/2017 INFS 214: Introduction to Computing ... •Some programs are translated using an interpreter. Such programs are translated

Types of Programming Languages

• Compiling

• Regardless of the HL Language, all HL programs need to be translated to machine code so that a computer can process the program. Some programs are translated using a compiler. When programs are compiled, they are translated all at once. Compiled programs typically execute more quickly than interpreted programs, but have a slower translation speed.

DR. EBENEZER ANKRAH Slide 28

Page 29: INFS 214: Introduction to Computing2014/2015 – 2016/2017 INFS 214: Introduction to Computing ... •Some programs are translated using an interpreter. Such programs are translated

Types of Programming Languages

• A compiler is a specific software that gets the whole Source Code (Computer program written in human understandable form) and translates it into Object Code (Computer Program that is in machine understandable form) all at a time.

DR. EBENEZER ANKRAH Slide 29

Page 30: INFS 214: Introduction to Computing2014/2015 – 2016/2017 INFS 214: Introduction to Computing ... •Some programs are translated using an interpreter. Such programs are translated

Types of Programming Languages

• Interpreting

• Some programs are translated using an interpreter. Such programs are translated line-by-line instead of all at once (like compiled programs). Interpreted programs generally translate quicker than compiled programs, but have a slower execution speed.

DR. EBENEZER ANKRAH Slide 30

Page 31: INFS 214: Introduction to Computing2014/2015 – 2016/2017 INFS 214: Introduction to Computing ... •Some programs are translated using an interpreter. Such programs are translated

Types of Programming Languages

• Interpreter is translating and executing one statement (command of Computer Program) of Source Code into Object Code at a time. (It means, interpreters translate and execute computer programs line by line).

DR. EBENEZER ANKRAH Slide 31

Page 32: INFS 214: Introduction to Computing2014/2015 – 2016/2017 INFS 214: Introduction to Computing ... •Some programs are translated using an interpreter. Such programs are translated

PROGRAM DEVELOPMENT CYCLE Topic Three

DR. EBENEZER ANKRAH Slide 32

Page 33: INFS 214: Introduction to Computing2014/2015 – 2016/2017 INFS 214: Introduction to Computing ... •Some programs are translated using an interpreter. Such programs are translated

Program Development Cycle

• There are four processes in the program development cycle. The following are the processes;

• Process 1: Analyze the Problem

– What must the program do?

– What outputs are required and in what form?

– What inputs are available and in what form?

DR. EBENEZER ANKRAH Slide 33

Page 34: INFS 214: Introduction to Computing2014/2015 – 2016/2017 INFS 214: Introduction to Computing ... •Some programs are translated using an interpreter. Such programs are translated

Program Development Cycle

– Example: Find a maximum of two numbers

– Input two numbers, compare them and print the maximum value

– Inputs and outputs are decimal numbers

– Inputs are entered from the keyboard

– Result is shown on the monitor

DR. EBENEZER ANKRAH Slide 34

Page 35: INFS 214: Introduction to Computing2014/2015 – 2016/2017 INFS 214: Introduction to Computing ... •Some programs are translated using an interpreter. Such programs are translated

Program Development Cycle

• Process 2: Design the Program

– Program Design involves creating an algorithm. An algorithm is a sequence of steps, by which a computer can produce the required outputs from the available inputs.

– An algorithm can also be considered as a step by step procedure that will provide the required results from the given inputs.

– Examples of algorithm: Instructions on how to develop a website, use the bank’s ATM, etc.

DR. EBENEZER ANKRAH Slide 35

Page 36: INFS 214: Introduction to Computing2014/2015 – 2016/2017 INFS 214: Introduction to Computing ... •Some programs are translated using an interpreter. Such programs are translated

Program Development Cycle

– Top-down design

• The main problem is split into subtasks

• Then each subtask is divided into simpler subtasks, etc. unless it is clear how to solve all such subtasks

DR. EBENEZER ANKRAH Slide 36

Page 37: INFS 214: Introduction to Computing2014/2015 – 2016/2017 INFS 214: Introduction to Computing ... •Some programs are translated using an interpreter. Such programs are translated

Program Development Cycle

• Process 3: Code the Program

– Program Coding means expressing the algorithm developed for solving a problem, in a programming language.

– Once the design is completed, write the program code.

– Code is written in some programming language such as BASIC, Pascal, C++, Java, etc.

DR. EBENEZER ANKRAH Slide 37

Page 38: INFS 214: Introduction to Computing2014/2015 – 2016/2017 INFS 214: Introduction to Computing ... •Some programs are translated using an interpreter. Such programs are translated

Program Development Cycle

• Process 4: Testing the program

– Initially, almost all programs may contain few errors, or bugs.

– Testing is necessary to find out if the program produces a correct result. Usually it is performed with sample data.

– Ultimate test is to run the program to see if the outputs are correct for the given inputs

– Debugging is the process of locating and removing errors.

DR. EBENEZER ANKRAH Slide 38

Page 39: INFS 214: Introduction to Computing2014/2015 – 2016/2017 INFS 214: Introduction to Computing ... •Some programs are translated using an interpreter. Such programs are translated

TYPES OF ERRORS

Topic Four

DR. EBENEZER ANKRAH Slide 39

Page 40: INFS 214: Introduction to Computing2014/2015 – 2016/2017 INFS 214: Introduction to Computing ... •Some programs are translated using an interpreter. Such programs are translated

Types of Errors

• Types of Program Errors

• There are three major types of program errors. These are as follows:

– Syntax Errors: Violation of syntactic rules in a Programming Language generates syntax errors. It is also caused by wrong grammar, i.e., breaking the rules of how to write the language. Forgetting punctuation, misspelling keyword etc. Interpreter or Compiler finds it in Syntax Check Phase.

DR. EBENEZER ANKRAH Slide 40

Page 41: INFS 214: Introduction to Computing2014/2015 – 2016/2017 INFS 214: Introduction to Computing ... •Some programs are translated using an interpreter. Such programs are translated

– Semantic Errors: Doing logical mistakes causes semantic errors in source code. The program completes execution, but delivers incorrect results. Interpreters and Compilers cannot notice them, but on execution, they cause unexpected results.

DR. EBENEZER ANKRAH Slide 41

Page 42: INFS 214: Introduction to Computing2014/2015 – 2016/2017 INFS 214: Introduction to Computing ... •Some programs are translated using an interpreter. Such programs are translated

Types of Errors

– Run-time Errors: Occur on program execution. Mostly caused by invalid data entry or tries to use not existing resources. It occurs on run time and may crash the program execution

DR. EBENEZER ANKRAH Slide 42

Page 43: INFS 214: Introduction to Computing2014/2015 – 2016/2017 INFS 214: Introduction to Computing ... •Some programs are translated using an interpreter. Such programs are translated

Types of Errors

• Types of Data

• The types of date are:

– Numeric Data

• Integer data, i.e., whole numbers 10, 25, -45, etc

• Floating point data – have a decimal point 23.0, -5.0

– Character data (alphanumeric)

• All the characters you can type at the keyboard

• Letters and numbers not used in calculations

– Boolean data

• TRUE/FALSE

DR. EBENEZER ANKRAH Slide 43

Page 44: INFS 214: Introduction to Computing2014/2015 – 2016/2017 INFS 214: Introduction to Computing ... •Some programs are translated using an interpreter. Such programs are translated

References

• Hutchinson, S. E., & Sawyer, S. C. (2013). Computers: The user perspective. Boston: Irwin McGraw-Hill.

• O’Leary, T. J., & O’Leary, L. I. (2014). Computer Today. Boston: McGraw Hill.

• Williams, B. K., & Sawyer, S. C. (2014). Using Information Technology: A practical introduction to computers and communications (11th ed.). McGraw-Hill Education.

DR. EBENEZER ANKRAH Slide 44