0% found this document useful (0 votes)
9 views95 pages

Intro To Computer Prog - Lecture 1 - 2

The document outlines an introductory lecture on computer programming by Frederick Edem Broni, covering key topics such as algorithms, control flow, data structures, and programming languages. It discusses the evolution of programming languages from machine and assembly languages to high-level languages, emphasizing their advantages and the importance of problem-solving skills in programming. Evaluation methods for the course include assignments, quizzes, labs, projects, and a final exam.

Uploaded by

michaelasiedu141
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views95 pages

Intro To Computer Prog - Lecture 1 - 2

The document outlines an introductory lecture on computer programming by Frederick Edem Broni, covering key topics such as algorithms, control flow, data structures, and programming languages. It discusses the evolution of programming languages from machine and assembly languages to high-level languages, emphasizing their advantages and the importance of problem-solving skills in programming. Evaluation methods for the course include assignments, quizzes, labs, projects, and a final exam.

Uploaded by

michaelasiedu141
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 95

Lecture 1

Introduction to Computer
Programming
BY

Frederick Edem
Broni
Dpt. Of Computer Science and Engineering

Contact Information: [email protected]

UNIVERSITY OF MINES AND


TECHNOLOGY

Course Code: CE 163


Course Outline

•Introduction
•Algorithms and Flowcharts

•Basic Syntax

•Control Flow

•Functions and Modules

Intro to Computer Prog – Frederick Edem Broni 04/29/2025 Slide 2


Course Outline

•Data Structures

•File Input and Output

•Advanced Topics

• Python Libraries

Intro to Computer Prog – Frederick Edem Broni 04/29/2025 Slide 3


Evaluation Methods

• Assignments
• Quizzes
• Lab
• Projects
• Final Exam

Intro to Computer Prog – Frederick Edem Broni 04/29/2025 Slide 4


What Is a Program?

• Program
– A list of instructions written in a special code, or
language.
– The program tells the computer which operations to
perform,
– and in what sequence to perform them.
– Garbage In, Garbage Out (G.I.G.O.)
– Get what you asked for, not necessarily what you
want.

Intro to Computer Prog – Frederick Edem Broni 04/29/2025 Slide 5


Computer Programs

• Software refers to programs that make the


computer perform some task.
• A program is a set of instructions that tells the
computer what to do.
• When you have written a program, the computer
will behave exactly as you have instructed it. It will
do no more or no less than what is contained in
your specific instructions.

Intro to Computer Prog – Frederick Edem Broni 04/29/2025 Slide 6


Writing Programs

• Learning to write programs requires two


skills.

– You need to use specific terminology and


punctuation that can be understood by the
machine; that is, you need to learn a
programming language.
– You need to develop a plan for solving a
particular problem. This planor algorithmis a
sequence of steps that, when followed, will
lead to a solution of the problem.

Intro to Computer Prog – Frederick Edem Broni 04/29/2025 Slide 7


Solving Problems

• Initially, you may think that learning a


language is the more difficult task because
your problems will have relatively easy
solutions. Nothing could be further from the
truth!
• The single most important thing you can
do as a student of computer science is to
develop the skill to solve problems.
• Once you have this skill, you can learn to write
programs in several different languages.

Intro to Computer Prog – Frederick Edem Broni 04/29/2025 Slide 8


What Is a Computer Language?

• A microprocessor is designed to “understand” a


set of commands called an “instruction set”
• All instructions must be provided to the CPU in
its native language, called machine language.
• All data transmission, manipulation, storage,
and retrieval is done by the machine using
electrical pulses representing sequences of
binary digits.
• If eight-digit binary codes are used, there are
256 numbered instructions from 00000000 to
11111111.
Intro to Computer Prog – Frederick Edem Broni 04/29/2025 Slide 9
Machine Language

• Instructions for adding two numbers would


consist of a sequence of these eight-digit
codes from 00000000 to 11111111.
• Instructions written in this form are referred to
as machine language.
• It is the native language that the CPU
“speaks” and “understands”.
• It is possible to write an entire program in
machine language. However, this is very time
consuming and difficult to read and
understand.
Intro to Computer Prog – Frederick Edem Broni 04/29/2025 Slide 10
Why Programming?

• To Develop Problem Solving Skills


– It is very important to develop problem
solving skills. Programming is all about solving
problems.
– Requires creativity and careful thought.
– Analyze the problem and break it down into
manageable parts (modules, procedures, functions)
• It’s also rewarding!

Intro to Computer Prog – Frederick Edem Broni 04/29/2025 Slide 11


What is a Programming Language?

• A programming language is a notational


system for describing computation in
machine-readable and human-readable form.
• Most of these forms are high-level
languages, which is the subject of the course.
• Assembly languages and other languages that
are designed to more closely resemble the
computer’s instruction set than anything that
is human-readable are low-level languages.

Intro to Computer Prog – Frederick Edem Broni 04/29/2025 Slide 12


Programming Languages

• Fortunately, special languages have


been developed that are more easily
understood (than machine language).
• These special languages are called
programming languages.
• These languages provide a way to write
computer programs that are understood
by both computers and people.
• Programming languages have their own
vocabulary and rules of usage.
• Some languages are very technical,
while others are similar to English.
Intro to Computer Prog – Frederick Edem Broni 04/29/2025 Slide 13
Assembly Language

• The programming language that is most like


machine language is assembly language.
• Assembly language uses letters and numbers
to represent machine language instructions.
• An assembler is a program that reads the
codes the programmer writes in assembly
language and “assembles” a machine
language program based on those codes.
• However, assembly language is still difficult
to read.

Intro to Computer Prog – Frederick Edem Broni 04/29/2025 Slide 14


Comparing Machine Language & Assembly Language

• For example, the machine code for adding two


integers might be:
01000011001110100011110101000001001010110
1000010
• While the assembly language code might be:
LOAD A
ADD B
STORE C
– This causes the number in A to be added to the number
in B, and the result is stored for later use in C.

Intro to Computer Prog – Frederick Edem Broni 04/29/2025 Slide 15


Low Level Languages

• Machine Language and Assembly


Language are both called low-level
languages.
• In a low-level language, it is necessary for
the programmer to know the instruction
set of the CPU in order to program the
computer.
• Each instruction in a low-level language
corresponds to one or only a few
microprocessor instructions.
Intro to Computer Prog – Frederick Edem Broni 04/29/2025 Slide 16
High Level Languages

• A high-level language is any programming


language that uses words and symbols to
make it relatively easy to read and write a
computer program.
• In a high-level language, instructions do
not necessarily correspond one-to-one
with the instruction set of the CPU.
• One command in a high-level language
may correspond to many microprocessor
instructions.
Intro to Computer Prog – Frederick Edem Broni 04/29/2025 Slide 17
High Level Languages 2

• Many high-level languages have been developed.


These include:
• FORTRAN, COBOL, BASIC, Logo, Pascal, C, C++,
Java, Python, and others.
• These languages simplify even further the
terminology and symbolism necessary for
directing the machine to perform various
manipulations of data.

Intro to Computer Prog – Frederick Edem Broni 04/29/2025 Slide 18


Advantages Of
High Level Languages

• High Level Languages:

– Reduce the number of instructions that must be


written.
– Allow programs to be written in a shorter amount
of time than a low-level language would take.
– Reduce the number of errors that are made,
because…
• The instructions are easier to read.
– Are more portable (the programs are easier to
move among computers with different
microprocessors).

Intro to Computer Prog – Frederick Edem Broni 04/29/2025 Slide 19


Advantages Of
Low Level Languages

• Low Level Languages:

– Instructions can be written to enable the computer


to do anything that the hardware will follow.
– Require less memory
– Run more quickly

Intro to Computer Prog – Frederick Edem Broni 04/29/2025 Slide 20


High Level Language Examples
• Consider the following programs that add
two numbers together:
BASIC Pascal C++ LOGO
10 I = 3 program AddIt; int main( ) to add :I :J :K
20 J = 2 var { MAKE “I :3
30 K = I + J i, j, k : integer; int i, j, k; MAKE “J :2
begin i = 3; MAKE “K :I + :J
i := 3; j = 2; end
j := 2; k = i + j;
k := i + j; return 0;
end. }

Intro to Computer Prog – Frederick Edem Broni 04/29/2025 Slide 21


High Level Language Examples 2
Java Python
public class AddTwoNumbers{ i=3
public static void main(String[] args){ j=2
int i, j, k; k=i+j
i = 3;
j = 2;
k = i + j;
}
}

Intro to Computer Prog – Frederick Edem Broni 04/29/2025 Slide 22


Interpreters and Compilers

• Programmers writing in a high-level language


enter the program’s instructions into a text
editor.
• The files saved in this format are called text
files.
• A program written in a high-level language is
called source code.
• The programs are translated into machine
language by interpreters or compilers.
• The resulting machine language code is
known as object code (or byte code - Java).
Intro to Computer Prog – Frederick Edem Broni 04/29/2025 Slide 23
Interpreters

• An interpreter is a program that translates the


source code of a high-level language into
machine language.
• Each instruction is interpreted from the
programming language as needed (line by line
of code).
• Every time the program is run, the interpreter
must translate each instruction again.
• In order to “run” the program, the
interpreter must be loaded into the
computer’s memory.
Intro to Computer Prog – Frederick Edem Broni 04/29/2025 Slide 24
Compilers
• A compiler is another program that
translates a high-level language into
machine language.
• A compiler makes the translation once so
that the source code don’t have to be
translated each time the program is run.
– The source code is translated into a file called
an object file.
– A program called a linker is used to create an
executable program.
– Most modern compilers let you compile and link
in a single operation, and have an “IDE”
(Integrated Development Environment) to enter
text, debug, compile, link, and run programs.

Intro to Computer Prog – Frederick Edem Broni 04/29/2025 Slide 25


Debug

• Bug: An error in coding or logic that


causes a program to malfunction or to
produce incorrect results.
• Debug: To detect, locate, and correct
logical or syntactical errors in a
program.
• Folklore attributes the first use of the
term “bug” to a problem in one of the
first electronic computers that was
traced to a moth caught between the
contacts of a relay in the machine.

Intro to Computer Prog – Frederick Edem Broni 04/29/2025 Slide 26


Programming Languages:
First Generation

• Generation 1 – Late 1940s to Early 1950s:


Machine Languages
– Programmers entered programs and data directly
into RAM using 1s and 0s
– Several disadvantages existed:
• Coding was error prone, tedious, and slow
• Modifying programs was extremely difficult
• It was nearly impossible for a person to decipher
someone else’s program
• Programs were not portable

Intro to Computer Prog – Frederick Edem Broni 04/29/2025 Slide 27


Programming Languages:
Second Generation

• Generation 2 – Early 1950s to Present: Assembly


Languages
– Uses mnemonic symbols to represent instructions
and data
– Assembly language is:
• More programmer friendly than machine language
• Tedious to use and difficult to modify
• Since each type of computer has its own unique
assembly language, it is not portable

Intro to Computer Prog – Frederick Edem Broni 04/29/2025 Slide 28


Programming Languages:
Third Generation

• Generation 3 – Mid-1950s to Present:


High-Level Languages
– Designed to be human friendly – easy to read,
write, and understand
– Each instruction corresponds to many
instructions in machine language
– Translation to machine language occurs
through a program called an interpreter or
compiler.
– Examples: FORTRAN, COBOL, BASIC, LOGO, C,
Pascal, C++, Java, and Python.

Intro to Computer Prog – Frederick Edem Broni 04/29/2025 Slide 29


Basic Approaches of Programming

• High-level programming languages utilize two


different approaches
– Procedural approach
• Examples: COBOL, FORTRAN, BASIC, Pascal, C, C++,
LOGO, and Python.

– Object-Oriented Programming approach


• Examples: Smalltalk, C++, Java, and Python

Intro to Computer Prog – Frederick Edem Broni 04/29/2025 Slide 30


Why Study Programming
Languages?

• In 1969, Sammet listed 120 programming


languages in common use – now there are
many more!
• Most programmers never use more than a
few.
– Some limit their career’s to just one or two.
• The gain is in learning about their
underlying design concepts and how this
affects their implementation.

Intro to Computer Prog – Frederick Edem Broni 04/29/2025 Slide 31


The Six Primary Reasons

• Increased ability to express ideas


• Improved background for choosing
appropriate languages
• Increased ability to learn new languages
• Better understanding of significance of
implementation
• Better use of languages that are already
known
• Overall advancement of computing

Intro to Computer Prog – Frederick Edem Broni 04/29/2025 Slide 32


Reason #1 - Increased ability to express ideas

• The depth at which people can think is heavily


influenced by the expressive power of their
language.
• It is difficult for people to conceptualize structures
that they cannot describe, verbally or in writing.

Intro to Computer Prog – Frederick Edem Broni 04/29/2025 Slide 33


Expressing Ideas as Algorithms

• This includes a programmer’s to develop effective


algorithms
• Many languages provide features that can waste
computer time or lead programmers to logic
errors if used improperly
– E. g., recursion in Pascal, C, etc.
– E. g., GoTos in FORTRAN, etc.

Intro to Computer Prog – Frederick Edem Broni 04/29/2025 Slide 34


Reason #2 - Improved background for
choosing appropriate languages

• Many professional programmers have a limited


formal education in computer science, limited to
a small number of programming languages.
• They are more likely to use languages with which
they are most comfortable than the most suitable
one for a particular job.

Intro to Computer Prog – Frederick Edem Broni 04/29/2025 Slide 35


Reason #3 - Increased ability to learn new
languages

• Computer science is a relatively young


discipline and most software technologies
(design methodology, software
development, and programming
languages) are not yet mature. Therefore,
they are still evolving.
• A thorough understanding of programming
language design and implementation
makes it easier to learn new languages.

Intro to Computer Prog – Frederick Edem Broni 04/29/2025 Slide 36


Learning a New Language

• It is easier to learn a new language if you


understand the underlying structures of
language.
Examples:
– It is easier for a BASIC program to FORTRAN
than C.
– It is easier for a C++ programmer to learn
Java.
– It is easier for a Scheme programmer to learn
LISP.

Intro to Computer Prog – Frederick Edem Broni 04/29/2025 Slide 37


Reason #4 - Better understanding
of significance of implementation

• It is often necessary to learn about language


implementation; it can lead to a better
understanding of why the language was designed
the way that it was.
• Fixing some bugs requires an understanding of
implementation issues.

Intro to Computer Prog – Frederick Edem Broni 04/29/2025 Slide 38


Reason #5 - Better use of languages
that are already known

• To allow a better choice of programming language


• Some languages are better for some jobs than
others.
– Example – FORTRAN and APL for calculations,
COBOL and RPG for report generation, LISP and
PROLOG for AI, etc.

Intro to Computer Prog – Frederick Edem Broni 04/29/2025 Slide 39


Better Use of a Language

• To improve your use of existing programming


language
• By understanding how features are implemented,
you can make more efficient use of them.
• Examples:
• Creating arrays, strings, lists, records.
• Using recursions, object classes, etc.

Intro to Computer Prog – Frederick Edem Broni 04/29/2025 Slide 40


Reason #6 - Overall advancement of computing

• Frequently, the most popular language may not


be the best language available.
• E.g., ALGOL 60 did NOT displace Fortran.
– They had difficulty understanding its description
and they didn’t see the significance of its block
structure and well-structured control statements
until many years later.

Intro to Computer Prog – Frederick Edem Broni 04/29/2025 Slide 41


Programming Domains

• Scientific Applications
• Business Applications
• Artificial Intelligence
• Web Software

Intro to Computer Prog – Frederick Edem Broni 04/29/2025 Slide 42


Numerically-Based Languages

• Many of the earliest computers were used almost


exclusively for scientific calculations and
consequently many of the earliest attempts at
languages were for scientific purposes. Eg. Julia,
MATLAB, Python or R

Intro to Computer Prog – Frederick Edem Broni 04/29/2025 Slide 43


Business Languages

• Commercial data processing was one of


the earliest commercial applications of
computers.
• Grace Murray Hopper et. al. at Univac
developed FLOWMATIC, an English-like
language for business applications.
• The U.S. Defense Dept. sponsored the
effort to develop COBOL (Common
Business-Oriented Language), which was
standardized in 1960, revised in 1961 &
1962, re-standarized in 1968, 1974, and
1984.
Intro to Computer Prog – Frederick Edem Broni 04/29/2025 Slide 44
Artificial Intelligence

• Artificial Intelligence deals with emulating


human-style reasoning on a computer.
• These applications usually involve symbolic
computation, where most of the symbols
are names and not numbers.
• The most common data structure is the list,
not the matrix or array as in scientific
computing and not the record as in
business computing
• Artificial intelligence requires more
flexibility than other programming domains.
Intro to Computer Prog – Frederick Edem Broni 04/29/2025 Slide 45
Artificial Intelligence Languages

• The first AI language was IPL (International Processing


Language, developed by the Rand Corporation. Its
low-level design led to its limited use.
• John McCarthy of MIT developed LIST for the IBM 704
(which eventually led to Scheme and Common LISP).
LISP is a recursion-oriented, list-processing language
that facilitated game-playing programs.
• Yngve of MIT developed COMIT, a string-processing
language, which was followed by AT&T’s SNOBOL.
• Prolog was developed by Colmerauer, Roussel and
Kowalski based on predicate calculus and
mathematical logic.

Intro to Computer Prog – Frederick Edem Broni 04/29/2025 Slide 46


Systems Languages

• Assembly languages were used for a very long


time operating systems programming because of
its power and efficiency.
• CPL, BCPL, C and C++ were later developed for
this purpose.
• Other languages for systems programming
included PL/I, BLISS, and extended ALGOL.

Intro to Computer Prog – Frederick Edem Broni 04/29/2025 Slide 47


Web Software

• Eclectic collection of languages:


– Markup (e.g., HTML) – used for annotating a
document in a manner that can be distinguished
from the text.
– Scripting (e.g., PHP) - the language that enable
the script to run these commands and typically
include control structures such as if-then-else and
while-do.
– General-purpose (e.g., Java) – can be used for a
wide range of programming jobs.

Intro to Computer Prog – Frederick Edem Broni 04/29/2025 Slide 48


Language Evaluation Criteria

• Readability – the ease with which programs can


be read and understood.
• Writability – the ease with which programs can
be developed for a given program domain.
• Reliability – the extent to which a program will
perform according to its specifications.

Intro to Computer Prog – Frederick Edem Broni 04/29/2025 Slide 49


What Do We Mean By Machine
Readability?

• A language is considered machine-


readable if it can be translated efficiently
into a form that the computer can execute.
• This requires that:
– A translation algorithm exists.
– The algorithm is not too complex.
• We can ensure machine readability by
requiring that programming languages be
context-free languages.

Intro to Computer Prog – Frederick Edem Broni 04/29/2025 Slide 50


What Do We Mean By Human
Readability?

• It is harder to define human readability in


precise terms.
• Generally this requires a programming
language to provide enough abstractions to
to make the algorithms clear to someone
who is not familiar with the program’s
details.
• As programs gets larger, making a language
readable requires that the amount of detail
is reduced, so that changes in one part of a
program have a limited effect on other
parts of the program.
Intro to Computer Prog – Frederick Edem Broni 04/29/2025 Slide 51
Simplicity
• Programming languages with a large number of
basic components are harder to learn; most
programmers using these languages tend to learn
and use subsets of the whole language.
• Complex languages have multiplicity (more than
one way to accomplish an operation).
• Overloading operators can reduce the clarity of
the program’s meaning

Intro to Computer Prog – Frederick Edem Broni 04/29/2025 Slide 52


An Example of Multiplicity

• All of the following add one to the variable count :


count = count + 1;
count += 1;
count++;
++count;
Do they mean the same thing?

Intro to Computer Prog – Frederick Edem Broni 04/29/2025 Slide 53


Example – IBM vs. VAX Assembler

• IBM Assembler
A Reg1, memory_cell ; Reg1 = Reg1 + memocell
AR Reg1, Reg2 ; Reg1 = Reg1 + Reg2

• VAX Assembler
ADDL operand1, operand2

Intro to Computer Prog – Frederick Edem Broni 04/29/2025 Slide 54


Lecture 2

Introduction to Computer
Programming Pt 2
BY

Frederick Edem
Broni
Dpt. Of Computer Science and Engineering

Contact Information: [email protected]

UNIVERSITY OF MINES AND


TECHNOLOGY

Course Code: CE 163


Control Statements

• In the 1950s and 1960s, the goto was the most


common control mechanism in a program;
however, it could make programs less readable.
• The introduction of while, for and if-then-
else eliminate the need for gotos and led to
more readable programs.

Intro to Computer Prog – Frederick Edem Broni 04/29/2025 Slide 56


Data Types and Structures

• A more diverse set of data types and the


ability of programmers to create their own
increased program readability:
– Booleans make programs more readable:
TimeOut = 1 vs. TimeOut = True
– The use of records to store complex data
objects makes programs more readable:
CHARACTER*30 NAME(100)
INTEGER AGE(100), EMPLOYEE_NUM(100)
REAL SALARY(100)
Wouldn’t it better if these were an array of
records instead of 4 parallel arrays?

Intro to Computer Prog – Frederick Edem Broni 04/29/2025 Slide 57


Syntax
• Most syntactic features in a programming
language can enhance readability:
– Identifier forms – older languages (like FORTRAN)
restrict the length of identifiers, which become less
meaningful
– Special words – in addition to while, do and for,
some languages use special words to close
structures such as endif and endwhile.
– Form and meaning – In C a static variable
within a function and outside a function mean two
different things – this is undesirable.

Intro to Computer Prog – Frederick Edem Broni 04/29/2025 Slide 58


Writability

• Historically, writability was less important than


efficiency than efficiency. As computers have
gotten faster, the reverse has become true to a
certain extent.
• Writability must be considered within the context of
the language’s target problem domain.
– E.g., COBOL handles report generating very well
but matrices poorly. The reverse is true for APL.
• A large and diverse set of construct is easier to
misuse than a smaller set of constructs that can be
combined under a consistent et of rules. (This is
simple and orthogonal)
Intro to Computer Prog – Frederick Edem Broni 04/29/2025 Slide 59
Writability and Abstraction

• A programming language should be able to


support data abstractions that a programmer is
likely to use in a given problem domain.
• Example – implementing binary trees in
FORTRAN, C++ and Java.

Intro to Computer Prog – Frederick Edem Broni 04/29/2025 Slide 60


Reliability

• Reliability is the assurance that a program


will not behave in unexpected or
disastrous ways during execution.
• This sometimes requires the use of rules
that are extremely difficult to check at
translation or execution time.
– ALGOL68’s rule prohibiting dangling reference
assignments (referring to objects that have
been de-allocated).
• Reliability and efficiency of translation are
frequently diametrically opposed.

Intro to Computer Prog – Frederick Edem Broni 04/29/2025 Slide 61


Contributing Factors To Reliability

• Type Checking – a large factor in


program reliability. Compile-time type
checking is more desireable. C’s lack of
parameter type checking leads to many
reliability problems.
• Exception Handling – the ability to catch
run-time errors and make corrections can
prevent reliability problems.
• Aliasing – having two or more ways of
referencing the same data object can
cause unnecessary errors.
Intro to Computer Prog – Frederick Edem Broni 04/29/2025 Slide 62
Cost of Use

• Cost of program execution


– A slower program is more expensive to run on a slower
computer.
– In an era of faster, cheaper computer, this is less of a
concern.
• Cost of program translation
– Optimizing compilers are slower than some other compilers
designed for student programs, which will not run as many
times..
• Cost of program creation, testing and use
– How quickly can you get the program executing correctly.
• Cost of program maintenance
– How expensive will it be to modify the program when
changes are needed in subsequent years?
Intro to Computer Prog – Frederick Edem Broni 04/29/2025 Slide 63
Influences on Language Design

Other factors have had a strong influence on


programming language design:
• Computer Architecture
• Programming Methodologies

Intro to Computer Prog – Frederick Edem Broni 04/29/2025 Slide 64


Computer Architecture

• Most computers are still based on the von


Neumann architecture, which view memory
as holding both instructions and data
interchangably.
• This has influenced the development of
imperative languages and has stifled the
adaption of functional languages.
• As parallel processing computers are
developed, there have been several attempts
made to develop languages that exploit their
features.
Intro to Computer Prog – Frederick Edem Broni 04/29/2025 Slide 65
Programming Methodologies

• New methods of program development have led


to advances in language design:
• These have included:
– structured programming languages
– data abstraction in object-oriented languages

Intro to Computer Prog – Frederick Edem Broni 04/29/2025 Slide 66


Language Categories

• There are four different programming language


paradigms:
– Imperative
– Functional
– Declarative
– Object-Oriented

Intro to Computer Prog – Frederick Edem Broni 04/29/2025 Slide 67


Imperative Languages

• Imperative languages are command-driven or


statement-oriented languages.
• The basic concept is the machine state (the set of
all values for all memory locations).
• A program consists if a sequence of statements
and the execution of each statement changes the
machine state.
• Programs take the form:
statement1;
statement2;
… …
• FORTRAN, COBOL, C, Pascal, PL/I are all
imperative languages.
Intro to Computer Prog – Frederick Edem Broni 04/29/2025 Slide 68
Functional Languages

• An functional programming language looks


at the function that the program represents
rather than the state changes as each
statement is executed.
• The key question is: What function must be
applied to our initial machine and our data
to produce the final result?
• Statements take the form:
functionn(function1, function2, … (data)) … )
• ML, Scheme and LISP are examples of functional
languages.
Intro to Computer Prog – Frederick Edem Broni 04/29/2025 Slide 69
Rule-Based Languages

• Rule-based or declarative languages


execute checking to see if a particular
condition is true and if so, perform the
appropriate actions.
• The enabling conditions are usually written in
terms of predicate calculus and take the
form:
condition1  action1
condition2  action2
… … …
• Prolog is the best know example of a
declarative language.
Intro to Computer Prog – Frederick Edem Broni 04/29/2025 Slide 70
Object-Oriented Languages

• In object-oriented languages, data structures and


algorithms support the abstraction of data and
endeavor to allow the programmer to use data in
a fashion that closely represents its real world use.
• Data abstraction is implemented by use of
– Encapsulation – data and procedures
belonging to a class can only be accessed by
that classes (with noteworthy exceptions).
– Polymorphism – the same functions and
operators can mean different things depending
on the parameters or operands,
– Inheritance – New classes may be defined in
terms of other, simpler classes.
Intro to Computer Prog – Frederick Edem Broni 04/29/2025 Slide 71
Language Design Trade-offs

• Frequently, design criteria will be contrdictory:


– Reliability and cost of execution
– In APL, expressivity and writability conflict with
readability
– Flexbilty and safety (e.g., variant records as a
safety loophole in Pascal).

Intro to Computer Prog – Frederick Edem Broni 04/29/2025 Slide 72


The Compiling Process

Source Object Linker Executable


Compile
Code Module version
r

Assembler version

Intro to Computer Prog – Frederick Edem Broni 04/29/2025 Slide 73


The Pure Interpretation Process

Source Interpreter Output


Code

Input

Intro to Computer Prog – Frederick Edem Broni 04/29/2025 Slide 74


The Hybrid Interpretation Process

Source Intermediate Interpreter


Interpreter Output
Code Version

Inpu
t

Intro to Computer Prog – Frederick Edem Broni 04/29/2025 Slide 75


Program Development

• Planning is a critical issue


– Don’t type in code “off the top of your
head”
• Programming Takes Time
– Plan on writing several revisions
– Debugging your program
• Programming requires precision
– One misplaced semi-colon (or colon -
Python) will stop the program

Intro to Computer Prog – Frederick Edem Broni 04/29/2025 Slide 76


Exercise in Frustration

• Plan well (using paper and pencil)


• Start early
• Be patient
• Handle Frustration
• Work Hard
• Don’t let someone else do part of the
program for you.
• Understand the Concepts Yourself!
• Solve the problem yourself!

Intro to Computer Prog – Frederick Edem Broni 04/29/2025 Slide 77


Step 1
Good Programming Habits

• 1. Analysis
– Is the computer the appropriate tool for solving this
problem?
– Would the problem be better solved with human
interaction or paper and pencil?
– Sometimes human judgment is preferable.

Intro to Computer Prog – Frederick Edem Broni 04/29/2025 Slide 78


Step 2
Good Programming Habits

• 2. Specification of the Problem


– Formulate a clear and precise statement of what is
to be done (clear and unambiguous).
– Know what data are available
– Know what may be assumed
– Know what output is desired & the form it should
take
– Divide the problem into sub problems
– Doesn’t discuss “how to” solve the problem yet.

Intro to Computer Prog – Frederick Edem Broni 04/29/2025 Slide 79


Step 3
Good Programming Habits

• 3. Develop an Algorithm
– Algorithm:
• a finite sequence of effective statements that when
applied to the problem, will solve it.
– Effective Statement:
• a clear unambiguous instruction that can be carried
out.
– Algorithms should have:
• specific beginning and ending that is reached in a
reasonable amount of time (a finite amount of time).
– This is done before sitting down at the
computer.
Intro to Computer Prog – Frederick Edem Broni 04/29/2025 Slide 80
Algorithms

• Steps for subtracting two numbers:


– Step 1: Write down the numbers, with larger number
above smaller one, digits column-aligned from right
– Step 2: Start with rightmost column of digits and work
your way left through the various columns
– Step 3: Write down difference between the digits in
the current column of digits, borrowing a 1 from the top
number’s next column to the left if necessary
– Step 4: If there is no next column to the left, stop
• Otherwise, move to column to the left; go to Step 3
• The computing agent is a human being

Intro to Computer Prog – Frederick Edem Broni 04/29/2025 Slide 81


Algorithms CONT.

• Sequence of steps that describes each of these


computational processes is called an algorithm
• Features of an algorithm:
– Consists of a finite number of instructions
– Each individual instruction is well defined
– Describes a process that eventually halts after
arriving at a solution to a problem
– Solves a general class of problems

Intro to Computer Prog – Frederick Edem Broni 04/29/2025 Slide 82


Step 3.5
Good Programming Habits

• 3.5 Document the Program


– Programming Style
• Upper / Lower Case, Indenting, format
– Comments
– Descriptive Identifier Names
• Variables, Constants, Procedures, Functions
– Pre & Post Conditions
• For each Procedure and Function
– Output

Intro to Computer Prog – Frederick Edem Broni 04/29/2025 Slide 83


Step 4
Good Programming Habits

• 4. Code the Program


– After algorithms are correct
– Desk check your program
• Without the computer,
just paper and pencil
• 4.1 Type and Run the Program
– Look for errors
• Syntax Errors (semi colon missing, etc.)
• Logic Errors (divide by zero, etc.)

Intro to Computer Prog – Frederick Edem Broni 04/29/2025 Slide 84


Step 4.2
Good Programming Habits

• 4.2 Test the Results


– Does it produce the correct solution?
– Check results with paper and pencil.
– Does it work for all cases?
• Border, Edge, Extreme Cases
– Revise the program if not correct.
– The coding process is not completed until the
program has been tested thoroughly and works
properly (recheck the specifications).

Intro to Computer Prog – Frederick Edem Broni 04/29/2025 Slide 85


Step 5
Good Programming Habits

• 5. Interpretation
– The program may execute without any obvious
errors.
– It may not produce the results which solve the
problem.
• G.I.G.O Get what you ask for,
not what you want.
• Recheck your program with the original specifications

Intro to Computer Prog – Frederick Edem Broni 04/29/2025 Slide 86


Top Down Design

• Subdivide the problem into major tasks


– Subdivide each major task into smaller tasks
• Keep subdividing until each task is easily solved.
• Each subdivision is called stepwise refinement.
• Each task is called a module
• We can use a structure chart to show
relationships between modules.

Intro to Computer Prog – Frederick Edem Broni 04/29/2025 Slide 87


Top Down Design 2

S truc tu re C h a rt

M ain T a sk

S u b ta sk S u b ta sk S u b ta sk

Intro to Computer Prog – Frederick Edem Broni 04/29/2025 Slide 88


Top Down Design 3

• Pseudocode

– is written in English with programming language


like sentence structure and indentations.
– Major Tasks are numbered with whole numbers
– Subtasks use decimal points for outline.

Intro to Computer Prog – Frederick Edem Broni 04/29/2025 Slide 89


Top Down Design 4

Intro to Computer Prog – Frederick Edem Broni 04/29/2025 Slide 90


Writing Programs

• Vocabulary
– reserved words
• have a predefined meaning that can’t be changed
– library identifiers
• words defined in standard libraries
– programmer supplied identifiers
• defined by the programmer following a well defined
set of rules

Intro to Computer Prog – Frederick Edem Broni 04/29/2025 Slide 91


Writing Programs 2

• Words are CaSe SeNsItIvE


– For constants use ALL CAPS (UPPERCASE)
– For reserved words and identifiers use lowercase
• Syntax
– rules for construction of valid statements, including
• order of words
• punctuation

Intro to Computer Prog – Frederick Edem Broni 04/29/2025 Slide 92


Writing Code

• Executable Statement
– basic unit of grammar
• library identifiers, programmer defined identifiers,
reserved words, numbers and/or characters
– A semicolon terminates a statement in many
programming languages
• Programs should be readable

Noformat.py format.py

Intro to Computer Prog – Frederick Edem Broni 04/29/2025 Slide 93


The Use of Comments

• Comments should be included to help make the


program more clear to someone reading the code
other than the author.
• Use comments after the header to explain the
function of the program, & throughout the
program

Intro to Computer Prog – Frederick Edem Broni 04/29/2025 Slide 94


Test Programs

• Test programs are short programs written to


provide an answer to a specific question.
• You can try something out
• Practice the programming language
• Ask “what if” questions
• Experiment: try and see

Intro to Computer Prog – Frederick Edem Broni 04/29/2025 Slide 95

You might also like