Introduction To Programming Program Is Synonymous With Software A Sequence or Set of Instructions

You might also like

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 19

INTRODUCTION TO PROGRAMMING

 Program is synonymous with software; a sequence or set of instructions


that can be executed by a computer. The term can refer to the original source
code or to the executable (machine language) version. The term program
implies a degree of completeness; that is, a source code program comprises
all statements and files necessary for complete interpretation or compilation,
and an executable program can be loaded into a given environment and
executed independently of other programs.
• Model of complex system
– model: simplified representation of salient features of something,
either tangible or abstract
– system: collection of components that work closely together
• Sequences of instructions expressed in specific programming language :
– syntax: grammatical rules for forming instruction
– semantics: meaning/interpretation of instructions
• Instructions written (programmed, coded) by programmer
– in a specific programming language
– programming languages allow you to express yourself more
precisely than natural language
– indeed, programs cannot contain any ambiguity
– all instructions together are called source code
• Executed by computer, by carrying out individual instructions
• Real world example:
– library catalog, word processor,
video game, ATM

Many Aspects of Programming


• Programming is controlling
– computer does exactly what you tell
it to
• Programming is teaching
– computer can only “learn” to do new things if you tell it how
• Programming is problem solving
– always trying to make computer do something useful — i.e., finding
an optimal travel route
• Programming is creative
1
– must find good solution out of many possibilities
• Programming is modelling
– describe salient (relevant) properties and behaviours of a system of
components (objects)
• Programming is abstraction
– identify important features without getting lost in detail
• Programming is concrete
– must provide detailed instructions to complete task

 Language is a communication among human beings that is characterized


by the use of arbitrary spoken (using the natural language) or written symbols
(using text, pictures, drawings, symbolic icons, etc.,) with agreed-upon
meanings. More broadly, language may be defined as communication in
general; it is regarded by some linguists as a form of knowledge, that is, of
thought or cognition.
 Programming Language is any artificial language that can be used to
define a sequence of instructions that can ultimately be processed and
executed by the computer. Defining what is or is not a programming language
can be tricky, but general usage implies that the translation of instructions
written in the language into code that the computer understands should be
completely systematic. Indeed such a translation is usually carried out by
computer.

 Programming Language is a programmer's principal interface with the


computer. More than just knowing how to program in a single language,
programmers need to understand the different styles of programming
promoted by different languages. In their professional life, they will be working
with many different languages and styles at once, and will encounter many
different languages over the course of their careers. Understanding the
variety of programming languages and the design tradeoffs between the
different programming paradigms makes it much easier to master new
languages quickly. Understanding the pragmatic aspects of programming
languages also requires a basic knowledge of programming language
translation and runtime features such as storage allocation.

Examples of PLs: ForTran, Algol, ADA, BASIC, LisP, COBOL, SmalTalk,


Simula 67, Pascal, Delphi, A, B, C, C++, C#, Assembly, FoxBase, Clipper,
SQL, Visual Basic, Q Basic, Perl, Visual FoxPro, Java, JavaScript,
Python, Oracle, and many others.

2
A short history of Programming Languages
The development of programming languages, unsurprisingly, follows
closely the development of the physical and electronic processes used in today's
computers.
Programming languages have been under development for years and will
remain so for many years to come. They got their start with a list of steps to wire
a computer to perform a task. These steps eventually found their way into
software and began to acquire newer and better features. The first major
languages were characterized by the simple fact that they were intended for one
purpose and one purpose only, while the languages of today are differentiated by
the way they are programmed in, as they can be used for almost any purpose.
And perhaps the languages of tomorrow will be more natural with the invention of
quantum and biological computers.
Charles Babbage is often credited with designing the first computer-like
machines, which had several programs written for them (in the equivalent of
assembly language) by Ada Lovelace.
In the 1940s the first recognizably modern, electrically powered computers
were created. Some military calculation needs were a driving force in early
computer development, such as encryption, decryption, trajectory calculation and
massive number crunching needed in the development of atomic bombs. At that
time, computers were extremely large, slow and expensive: advances in
electronic technology in the post-war years led to the construction of more
practical electronic computers. At that time only Konrad Zuse imagined the use of
a programming language (developed eventually as Plankalkül) like those of today
for solving problems.
Subsequent breakthroughs in electronic technology (transistors, integrated
circuits, and chips) drove the development of increasingly reliable and more
usable computers. The first widely used high level programming language was
Fortran, developed during 1954–57 by an IBM team led by John W. Backus. It is
still widely used for numerical work, with the latest international standard
released in 2004. A Computer Languages History graphic shows a timeline from
Fortran in 1954.

Questions commonly asked about PLs?


Q: What is our language?
A: Natural Languages (e.i. English, Tagalog, Pangasinan, Ilocano,…)

Q: What language used by machines?


A: Machine language
a. machine is short for computing machine (i.e., computer)
3
b. computer’s native language
c. sequence of zeroes and ones
d. different computers understand different sequences
e. difficulties to humans to understand 01010001...

Q: How can you understand machine language?


A: Before, people learned the language of machine but until such a time people
ought to say that instead we learn its language the machine should learn our
language. Therefore people (language designers to be specific) created the
Programming Languages.

Q: Who was behind this breakthrough?


A: John Backus

Q: How 1 and 0 became multiple 1’s and 0’s?


A: Number systems

Q: What is a language? (answer it in its use)


Q: What is the role of Programming Language?
Q: Who uses PL?
Q: Why/when are some PLs better than others?
Q: What is a (good) programming style?
Q: How can we enforce good style?
Q: is a correct program always better than an incorrect one?

Natural vs. Programming Languages

Natural L Programming L
human to machine (vice
Communication Parties human to human
versa)
Mode of
verbal, non-verbal type, record, audio, scan
Communication
What is being feelings, reactions,
Instruction, data
transferred? emotions

Representation:

Natural Language Programming Language


Letters Symbols

Words Strings
Sentence Statements
Paragraph Programs

Requisite of communication is common language. Why? because two or more


different languages without anybody knowing the language of others is not good.
Solution: Interpreter or Translator

4
A person using a machine/PC can do programs by using compilers or
interpreters.
Levels of Programming Language

There is only one programming language that any computer can actually
understand and execute: its own native binary machine code. This is the lowest
possible level of language in which it is possible to write a computer program. All
other languages are said to be high level or low level according to how closely
they can be said to resemble machine code.

In this context, a low-level language corresponds closely to machine


code, so that a single low-level language instruction translates to a single
machine-language instruction. A high-level language instruction typically
translates into a series of machine-language instructions.

Low-level languages have the advantage that they can be written to take
advantage of any peculiarities in the architecture of the central processing unit
(CPU) which is the "brain" of any computer. Thus, a program written in a low-
level language can be extremely efficient, making optimum use of both computer
memory and processing time. However, to write a low-level program takes a
substantial amount of time, as well as a clear understanding of the inner
workings of the processor itself. Therefore, low-level programming is typically
used only for very small programs, or for segments of code that are highly
critical and must run as efficiently as possible.

High-level languages permit faster development of large programs. The final


program as executed by the computer is not as efficient, but the savings in
programmer time generally far outweigh the inefficiencies of the finished
product. This is because the cost of writing a program is nearly constant for each
line of code, regardless of the language. Thus, a high-level language where
each line of code translates to 10 machine instructions costs only one tenth as
much in program development as a low-level language where each line of code
represents only a single machine instruction.

In addition to the distinction between high-level and low-level languages,


there is a further distinction between compiler languages and interpreter
languages. Let's take a look at the various levels.

Absolute Machine Code

The very lowest possible level at which you can program a computer
is in its own native machine code, consisting of strings of 1's and 0's and
stored as binary numbers. The main problems with using machine code
5
directly are that it is very easy to make a mistake, and very hard to find it
once you realize the mistake has been made.

Assembly Language

Assembly language is nothing more than a symbolic representation of


machine code, which also allows symbolic designation of memory locations.
Thus, an instruction to add the contents of a memory location to an internal CPU
register called the accumulator might be add a number instead of a string of
binary digits (bits).

No matter how close assembly language is to machine code, the


computer still cannot understand it. The assembly-language program must be
translated into machine code by a separate program called an assembler. The
assembler program recognizes the character strings that make up the symbolic
names of the various machine operations, and substitutes the required machine
code for each instruction. At the same time, it also calculates the required
address in memory for each symbolic name of a memory location, and
substitutes those addresses for the names. The final result is a machine-
language program that can run on its own at any time; the assembler and the
assembly-language program are no longer needed. To help distinguish between
the "before" and "after" versions of the program, the original assembly-language
program is also known as the source code, while the final machine-language
program is designated the object code.

If an assembly-language program needs to be changed or corrected, it is


necessary to make the changes to the source code and then re-assemble it to
create a new object program.

Compiler Language

Compiler languages are the high-level equivalent of assembly language.


Each instruction in the compiler language can correspond to many machine
instructions. Once the program has been written, it is translated to the equivalent
machine code by a program called a compiler. Once the program has been
compiled, the resulting machine code is saved separately, and can be run on its
own at any time.

As with assembly-language programs, updating or correcting a compiled


program requires that the original (source) program be modified appropriately
and then recompiled to form a new machine-language (object) program.

6
Typically, the compiled machine code is less efficient than the code
produced when using assembly language. This means that it runs a bit more
slowly and uses a bit more memory than the equivalent assembled program. To
offset this drawback, however, we also have the fact that it takes much less time
to develop a compiler-language program, so it can be ready to go sooner than
the assembly-language program.

Interpreter Language

An interpreter language, like a compiler language, is considered to be high


level. However, it operates in a totally different manner from a compiler language.
Rather, the interpreter program resides in memory, and directly executes the
high-level program without preliminary translation to machine code.

This use of an interpreter program to directly execute the user's program


has both advantages and disadvantages. The primary advantage is that you can
run the program to test its operation, make a few changes, and run it again
directly. There is no need to recompile because no new machine code is ever
produced. This can enormously speed up the development and testing process.

On the down side, this arrangement requires that both the interpreter and
the user's program reside in memory at the same time. In addition, because the
interpreter has to scan the user's program one line at a time and execute internal
portions of itself in response, execution of an interpreted program is much slower
than for a compiled program.

7
PROGRAMMING BASICS
Planning the Solution
In planning the solution to any problem, it is always necessary that if you
identify the problem you will arrive to a good solution. We must be “more specific”
about the three components: input, process and output.
The result of our planning is a series of procedure that later be converted
into programs.

Algorithm – is a procedure for solving a problem; it is a series of steps that


should be followed “exactly” to produce a desired result.
There are two things to consider about algorithm: 1) there are one or more
algorithm to solve a problem, and 2) all the series or steps should be followed
correctly and in sequence to come up with a complete solution.

Structured Programming
This is a technique that makes programs easier to write, check, read and
maintain. This technique emphasized the breaking of a program into logical
sections or “modules” following a universal programming standard. Breaking of
programs into smaller modules will help organize the flow of solution from one
module to next.

Control Structure
It is a series of steps that determines what instructions or set of
instructions will be done next.
3 Basic Control Structures
1. Sequential C.S.
2. Selection C.S.
3. Iteration C.S.

Selection Control Structure


It is the most straight-forward of all the control structures.
Format:

Process 1

Process 2 8
Selection Control Structure
It is used to let your algorithm or program make a decision or choice.
Format:
if (condition) then (process1) else (process2)

if false
if true
use a relational operator

True False

if

Process 1 Process 2

Iteration or Loop
It is used when an algorithm or program repeat a process on a specific
number of times. This repeats an instruction or program until a condition is met.

Process

no
Is the condition
met?

yes

Flowchart is a visual graph of an algorithm or program.


Types:

Start / End symbol Decision Box


(Conditional)

Process Box Connector

Input / Output
Direction

9
Initialization Box

Examples:
Sequential Control Structure
Problem 1: Create a flowchart the will accept the name and course of a student.

begin

name=” “
course=” “

Input name,
course

end

Problem 2: Create a flowchart that will accept two numbers and output the sum.

begin

n1,n2=0
sum=0

Input n1, n2

sum=n1+n2

Display sum

end

10
Problem 3: Create a flowchart showing the input of four numbers (any type) and
output the average in floating data type.

x
begin

Display
n1,n2,n3,n4=0.0 average
average=0.0

end
Input
n1,n2,n3,n4

average=(n1+n2+n3+n4)/4

Problem 4: Create a flowchart that will identify the circumference of a circle:


Formula: 2 pi r

begin begin

C, rad=0.0
C, rad=0.0 pi = 3.1416

Input Input
rad rad

Or
C = 2 (3.1416) (rad) C = 2 (pi) (rad)

Display Display
C C

end end

11
Problem 5: Create a flowchart to read a "float" representing a number in
millimeter, and print as a "float" the equivalent value in centimeter, decimeter and
meter.

begin begin

mm, cm=0.0 mm, cm=0.0


dm, m=0.0 dm, m=0.0

Input Input
mm mm

OR

cm = mm / 10 cm = mm / 10
dm = mm / 100 dm = cm / 10
m = mm / 1000 m = dm / 10

Display Display
cm, dm, m cm, dm, m

end end

Problem 6: Create a flowchart that will accept input from a student name, course
and year. Also, inputs from his class standing (60%) and prelim grade (40%) to
computer for his prelim grade. Finally, display the student name, course and his
prelim grade.

x
begin

Display name,
name, course, yr = “ “ course
CS, PE, PG = 0.0 PG

Input name, end


course, yr,
CS, PE

CS = CS * 0.60
PG = (CS*0.60) + (PE*0.4) PE = PE * 0.40
Or PG = CS + PE

12
Selection Control Structure (Simple)
Problem 1: Create a flowchart that will accept student name and final grade then
identify by displaying also whether his grade “passed” or “failed”.

begin

name = “ “
FG = 0.0

Input name
Input FG

If
FG >=75

Display Passed Display Failed

end

Problem 2: Input two numbers. Print their sum if the first number entered was
lesser than the second number. Otherwise print their average.
begin

n1, n2, sum = 0.0


average = 0.0

Input n1
Input n2

True False
If
n1 < n2

sum = n1+n2
sum = n1 + n2
average = sum / 2

Display sum Display average

end
13
Problem 3: Input two numbers and get their difference. From the difference
identify and display whether the difference is a “positive” or a “negative” number.

begin

n1, n2, diff = 0.0

Input n1, n2

diff = n1 – n2

True False
If
diff > 0

Positive Negative

end

Problem 4: Input sexcode as M or F. If sexcode is ‘M’ display “Male” otherwise


“Female”.
begin

sexcode = ‘ ‘

Input sexcode

True False
If
Sexcode= ‘M’

Display Male Display Female

end

14
Selection Control Structure (Ladderized)
Problem 5: Create a flowchart that will accept input to represent angle (an
integer) and identify whether it is an “acute angle”, a “right angle”, an “obtuse
angle” or a “complex angle”.

begin

Angle = 0

Input Angle

True
If
Acute angle
Angle < 90

False

True
If
Right angle
Angle = 90

False

True
If
Obtuse angle
Angle >= 180

False
Complex angle

end

15
Problem 6: Create a flowchart that will input the student name and his final
grade. Also, identify whether his grade falls on the remarks below and display the
remark that corresponds to his level of standing.
Final Grade Remarks
75 below “very poor”
75 to 80 “poor”
81 to 85 “average”
86 to 90 “good”
91 to 95
begin “very good”
96 “excellent”

FG = 0.0

Input FG

True
If
Very poor
FG < 75

False

True
If
Poor
FG <= 80

False

True
If
Average
FG <= 85

False
True
If
Good
FG <= 90

False

True
If
Very Good
FG <= 95

False
Very Good

end

Iterative Control Structure


Problem 1: Create a flowchart that will represent an iteration of 10 looping
starting from 1 to 10. The looping will end after it finishes the ten tasks.

16
begin

x=1

True
If
x=x+1
x <= 10

False

end

Or

begin

x=0

x=x+1

True
If
x <= 10

False

end

Problem 2: Create a flowchart that will represent 10 iterations. On every process,


it should display numbers starting from 2 and will last until 20.

17
Note: the counter increments by 2.

begin

x=0

x=x+2

True
If
Display x
x <= 20

False

end

OR

begin

y = 0, x=0

x=x+2
y=y+1

True
If
Display x
y <= 10

False

end

Problem 3: Create a flowchart that will verify and confirm each password entered
by a user to restrict unauthorized users. Each time the user fail to enter correct

18
password, the computer will display “invalid password” otherwise if password is
correct, then it displays “password accepted”.
Password is “rolmaq”

begin

pw = “ "

Enter pw

False
If Invalid
pw = rolmaq password

True

Password
accepted

end

Problem 4: (same as the problem above only that trial in entering a password
should only be three attempts. After which the process terminates for at most 3
attempts failed to do so).

begin

pw = “ "
ctr=1

Enter pw ctr = ctr + 1

True
False
If Invalid
If ctr < 4
pw = “rolmaq" password

True False
Password
accepted end

19

You might also like