Introduction To Programming Languages

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 69

Introduction to Programming

Introduction to Programming Languages

M A N I L A , P H I L I P P I N E S
Computer Program
A computer program (also a software program, or just
a program) is a sequence of instructions written to perform a
specified task for a computer. A computer requires programs
to function, typically executing the program's instructions in
a central processor. The program has an executable form
that the computer can use directly to execute the instructions.

11/18/2019 2
M A N I L A , P H I L I P P I N E S
Source Code
Source code is text written in a computer programming
language. Such a language is specially designed to
facilitate the work of computer programmers, who specify
the actions to be performed by a computer mostly by
writing source code, which can then be automatically
translated to binary machine code that the computer can
directly read and execute.

11/18/2019 3
M A N I L A , P H I L I P P I N E S
Computer Programming
Computer programming (often shortened to programming
or coding) is the process of designing, writing, testing,
debugging/ troubleshooting, and maintaining the source
code of computer programs. This source code is written in a
programming language. The purpose of programming is to
create a program that exhibits a certain desired behavior.
The process of writing source code often requires expertise
in many different subjects, including knowledge of the
application domain, specialized algorithms and formal logic.
11/18/2019 4
M A N I L A , P H I L I P P I N E S
Introduction to Programming
• Program: self-contained set of instructions used to
operate a computer to produce a specific result
• Also called software
• Programming: the process of writing a program, or
software

11/18/2019
M A N I L A , P H I L I P P I N E S
5 5
Machine Language
• Machine language programs, also called executables,
consist of binary instructions
• Each instruction has two parts:
• Instruction part: the operation to be performed; also called an
opcode
• Address part: memory address of the data to be used
• Each class of computer has its own particular machine
language
• Writing in machine language is tedious!

11/18/2019
M A N I L A , P H I L I P P I N E S
6 6
Assembly Languages
• Assembly Language: programming language with
symbolic names for opcodes, and decimals or labels
for memory addresses
Example:
ADD 1, 2
MUL 2, 3
• Assembly language programs must be translated into
machine instructions, using an assembler

11/18/2019
M A N I L A , P H I L I P P I N E S
7 7
Introduction to Programming: Assembly Language (continued)

11/18/2019
M A N I L A , P H I L I P P I N E S
8 8
Introduction to Programming:
• Low-level languages: languages that use instructions
tied directly to one type of computer
Examples: machine language, assembly language

• High-level languages: instructions resemble written


languages, such as English,and can be run on a variety
of computer types
Examples: Visual Basic, C, C++, Java

11/18/2019
M A N I L A , P H I L I P P I N E S
9 9
Introduction to Programming:
(continued)
• Source code: the programs written in a high- or low-
level language
• Source code must be translated to machine
instructions in one of two ways:
• Interpreter: each statement is translated individually and
executed immediately after translation
• Compiler: all statements are translated and stored as an
executable program, or object program; execution occurs
later

11/18/2019
M A N I L A , P H I L I P P I N E S
10 10
Low- and High-Level Languages (continued)

• Large C++ programs may be stored in two or more


separate program files due to
• Use of previously written code
• Use of code provided by the compiler
• Modular design of the program (for reusability of
components)
• Linker: combines all of the compiled code required for
the program

11/18/2019
M A N I L A , P H I L I P P I N E S
11 11
Low- and High-Level Languages (continued)

• Programs can also be classified by their orientation:


• Procedural: available instructions are used to create self-
contained units called procedures
• Object-oriented: reusable objects, containing code and
data, are manipulated
• Object-oriented languages support reusing existing
code more easily

11/18/2019
M A N I L A , P H I L I P P I N E S
12 12
Procedural and Object Orientations

Figure 1.2
Creating an
executable C++
program.

11/18/2019
M A N I L A , P H I L I P P I N E S
13 13
History of C++

During the 60s


ALGOL 60, was developed as an alternative to
FORTRAN taking from it some concepts of
structured programming which would later inspire
most procedural languages.

ALGOL 68 also directly influenced the


development of data types in C. Nevertheless
ALGOL was an non-specific language and its
abstraction made it impractical to solve most
commercial tasks.
11/18/2019 M A N I L A , P H I L I P P I N E S
14
History of C++ (continued)

In 1963 the CPL (Combined Programming


language) appeared with the idea of being more
specific for concrete programming tasks of that
time than ALGOL or FORTRAN. Nevertheless this
same specificity made it a big language and,
therefore, difficult to learn and implement.

11/18/2019 M A N I L A , P H I L I P P I N E S
15
History of C++ (continued)

In 1967, Martin Richards developed the BCPL


(Basic Combined Programming Language),
that signified a simplification of CPL but kept most
important features the language offered. Although
it too was an abstract and somewhat large
language.

11/18/2019 M A N I L A , P H I L I P P I N E S
16
History of C++ (continued)

In 1970, Ken Thompson, immersed in the


development of UNIX at Bell Labs, created the B
language. It was a port of BCPL for a specific
machine and system (DEC PDP-7 and UNIX), and
was adapted to his particular taste and necessities.
The final result was an even greater simplification of
CPL, although dependent on the system. It had
great limitations, like it did not compile to
executable code but threaded-code, which
generates slower code in execution, and therefore
was inadequate for the development of an
operating system.
11/18/2019 M A N I L A , P H I L I P P I N E S
17
History of C++ (continued)

Therefore, from 1971, Dennis Ritchie, from


the Bell Labs team, began the development
of a B compiler which, among other things,
was able to generate executable code
directly. This "New B", finally called C,
introduced in addition, some other new
concepts to the language like data types
(char).

11/18/2019 M A N I L A , P H I L I P P I N E S
18
History of C++ (continued)

The C programming language was designed by


Dennies Ritchie in 1973 at Bell Laboratories. It
was first used system implementation language
for the nascent Unix operating system. The main
reason to devised C was to overcome the
limitations of B. It was Derived from the type-less
language BCPL (Basic Combined
Programming Language). C was was the
evolution of B and BCPL by incorporating type
checking. It was originally intended for use in
writing compilers for other languages.

11/18/2019 M A N I L A , P H I L I P P I N E S
19
History of C++ (continued)

Dennis Ritchie (September 9, 1941 – October 12, 2011) was an


American computer scientist and winner, with Kenneth Thompson, of the
1983 Turing Award. He created the C programming language and, with Thompson,
the Unix operating system, which have had pervasive and lasting influence on
subsequent programming languages and operating systems.

11/18/2019 M A N I L A , P H I L I P P I N E S
20
History of C++ (continued)

In 1980, Bjarne Stroustrup, from Bell labs, began the


development of the C++ language, that would receive
formally this name at the end of 1983, when its first manual
was going to be published. In October 1985, the first
commercial release of the language appeared as well as the
first edition of the book "The C++ Programming Language"
by Bjarne Stroustrup.
In the words of Bjarne Stroustrup "the name signifies the
evolutionary nature of the changes from C".
That is during when C++ was evolving initially, it was called
as new C or C with classes. The "+" is actually the
operator's sign used in programming languages normally for
coding purposes.

11/18/2019 M A N I L A , P H I L I P P I N E S
21
History of C++ (continued)

Bjarne Stroustrup (born December 30, 1950) is a computer


scientist and creator of the C++programming language.
Professor and holder of the College of Engineering Chair in
Computer Science

11/18/2019 M A N I L A , P H I L I P P I N E S
22
History of C++ (continued)
During the 80s, the C++ language was being refined until it
became a language with its own personality. All that with very
few losses of compatibility with the code with C, and without
resigning to its most important characteristics. In fact, the ANSI
standard for the C language published in 1989 took good part of
the contributions of C++ to structured programming.

From 1990 on, ANSI committee X3J16 began the


development of a specific standard for C++. In the period
elapsed until the publication of the standard in 1998, C++ lived a
great expansion in its use and today is the preferred language to
develop professional applications on all platforms.

C++ has been evolving, and a new version of the standard,


c++0x, is being developed to be published soon, with several
new features.
11/18/2019 M A N I L A , P H I L I P P I N E S
23
Difference of C and C++

C++ is an extension of C language. This means that


you can not only use the new features introduced
with C++ but can also use the power and efficiency
of C language. C and C++ are no more language for
writing compilers and other languages, these
general purpose languages are used worldwide in
every field.
However, C++ does not retain complete source-
level compatibility with C.

11/18/2019 M A N I L A , P H I L I P P I N E S
24
Advantages of C++

1. C++ allows expression of abstract ideas


2. C++ still allows a programmer to keep low-level
control
3. C++ has national standards (*ANSI)
4. C++ is reusable and object-oriented
5. C++ is widely used and taught

*ANSI: American National Standard Institute

11/18/2019 M A N I L A , P H I L I P P I N E S
25
List of differences between C and C++:

• The main difference between C and C++ is that


C++ is object oriented while C is function or
procedure oriented.
• C++ has a much larger library
• You must declare functions before use.
Although most good C code will follow this
convention, in C++ it is strictly enforced that
all functions must be declared before they are
used.

11/18/2019 M A N I L A , P H I L I P P I N E S
26
Difference of C and C++
• In C, main() doesn't provide return 0 automatically.
In C++, you are free to leave off the statement
'return 0;' at the end of main; it will be provided
automatically:
int main()
{ printf( "Hello, World" );
but }in C, you must manually add it:

int main()
{ printf( "Hello, World" );
return 0;
}

11/18/2019 M A N I L A , P H I L I P P I N E S
27
The Development of C++

• The purpose of most application programs is to


process data to produce specific results

Figure 1.3 Basic procedural operations.

11/18/2019 M A N I L A , P H I L I P P I N E S
28 28
The Development of C++ (continued)
• Early procedural languages included
• FORTRAN: Formula Translation
• ALGOL: Algorithmic Language
• COBOL: Common Business Oriented Language
• BASIC: Beginners All-purpose Symbolic Instruction Code
• Pascal
• C

11/18/2019
M A N I L A , P H I L I P P I N E S
29 29
The Development of C++ (continued)
• Early object-oriented language:
• C++

11/18/2019
M A N I L A , P H I L I P P I N E S
30 30
Program Development Cycle
1. Analyze: Define the problem
2. Design: Plan the solution to the problem
3. Choose the Interface: Select the objects
4. Code: Translate the algorithm into a programming
language.
5. Debug and Test: Locate and remove any errors in
the program.
6. Complete the Documentation: Organize all the
materials that describe the program.

11/18/2019 M A N I L A , P H I L I P P I N E S
31
Algorithms

• Algorithm: the step-by-step sequence of


instructions that describe how the data is to be
processed to produce the desired output
• Programming = the translation of the selected
algorithm into a language the computer can use

11/18/2019
M A N I L A , P H I L I P P I N E S
32 32
Algorithms (continued)

• Pseudocode: English-like phrases used to


describe the algorithm
• Formula: description of a mathematical
equation
• Flowchart: diagram showing the flow of
instructions in an algorithm
– Flowcharts use special symbols

11/18/2019 M A N I L A , P H I L I P P I N E S
33 33
Expressing Algorithms
Algorithms can be expressed in many kinds of notation,
including:
• Natural Languages
• Pseudocode
• Flowcharts
• Programming Languages

11/18/2019 M A N I L A , P H I L I P P I N E S
34
Pseudocode
• A program design technique that uses English words.
• Has no formal syntactical rules.
• Pseudo means false, thus pseudocode means false code. It looks like
(imitates) real code but it is NOT real code.
• Pseudocode cannot be compiled nor executed, and there are no real
formatting or syntax rules.
• Pseudocode should not include keywords in any specific computer
languages.
• The benefit of pseudocode is that it enables the programmer to concentrate
on the algorithms without worrying about all the syntactic details of a
particular programming language.

11/18/2019 M A N I L A , P H I L I P P I N E S
35
How do I write Pseudocode?
• First you may want to make a list of the main tasks that must
be accomplished on a piece of scratch paper.

• Then, focus on each of those tasks. Generally, you should try


to break each main task down into very small tasks that can
each be explained with a short phrase.

• There may eventually be a one-to-one correlation between


the lines of pseudocode and the lines of the code that you
write after you have finished pseudocoding.

11/18/2019 M A N I L A , P H I L I P P I N E S
36
• It is not necessary in pseudocode to mention the need to
declare variables. It is wise however to show the initialization
of variables.

• All statements showing "dependency" are to be indented.


These include while, do, for, if, switch.
Some Keywords That Should be Used
For looping and selection, the keywords that are to be used
include:
Do While...EndDo Call ... with (parameters)
Do Until...Enddo Call
Case...EndCase Return
If...Endif When
11/18/2019 M A N I L A , P H I L I P P I N E S
37
As verbs, use the words:

Generate, Compute, Process


Set, Reset, Increment, Compute, Test
Calculate, Add, Sum, Multiply, ...
Print, Display, Input, Output, Edit

11/18/2019 M A N I L A , P H I L I P P I N E S
38
Pseudocode Examples
Original Program Specification:

Write a program that obtains two integer numbers from


the user. It will print out the sum of those numbers.

Pseudocode:

Prompt the user to enter the first integer


Prompt the user to enter a second integer
Compute the sum of the two user inputs
Display an output prompt that explains the answer as the
sum
Display the result

11/18/2019 M A N I L A , P H I L I P P I N E S
39
Original Program Specification:

Write a program that will display if the entered student’s


grade is passed of failed. Passing grade is 70.

Pseudocode:

Prompt the user to enter a student grade


If student's grade is greater than or equal to 70
Print "passed"
else
Print "failed"

11/18/2019 M A N I L A , P H I L I P P I N E S
40
Original Program Specification:

Write a program that will allow the student to input the grade
on his 5 subjects and will compute for his average grade.

Original Program Specification:

Set total to zero


Set grade counter to one
While grade counter is less than or equal to 5
Input the next grade
Add the grade into the total
Set the class average to the total divided by 5
Print the class average.

11/18/2019 M A N I L A , P H I L I P P I N E S
41
Pseudocode example : If then Else

If age > 17
Display a message indicating you can vote.
Else
Display a message indicating you can't vote.
Endif

Pseudocode example : Case

Case of age
0 to 17 Display "You can't vote."
18 to 64 Display "Your in your working years."
65 + Display "You should be retired."
Endcase

11/18/2019 M A N I L A , P H I L I P P I N E S
42
Pseudocode example : While loop

count assigned zero


While count < 5
Display "I love computers!"
Increment count
Endwhile

Pseudocode example : For loop

For x starts at 0, x < 5, increment x


Display "Are we having fun?"
Endfor

11/18/2019 M A N I L A , P H I L I P P I N E S
43
Pseudocode example : Do While loop

count assigned five


Do
Display "Blast off is soon!"
Decrement count
While count > zero

Pseudocode example : Repeat Until loop

count assigned five


Repeat
Display "Blast off is soon!"
Decrement count
Until count < one

11/18/2019 M A N I L A , P H I L I P P I N E S
44
Pseudocode example : Function with no parameter
passing

Function clear monitor


Pass In: nothing
Direct the operating system to clear the monitor
Pass Out: nothing
Endfunction
Pseudocode example : Function with parameter passing

Function delay program so you can see the monitor


Pass In: integer representing tenths of a second
Using the operating system delay the
program
Pass Out: nothing
Endfunction

11/18/2019 M A N I L A , P H I L I P P I N E S
45
Pseudocode of : Function main calling
the clear monitor function

Function main
Pass In: nothing
Doing some lines of code
Call: clear monitor
Doing some lines of code
Pass Out: value zero to the operating system
Endfunction

11/18/2019 M A N I L A , P H I L I P P I N E S
46
Flowcharts Symbols

11/18/2019 47
M A N I L A , P H I L I P P I N E S
Flowcharts Symbols (continued)

11/18/2019 48
M A N I L A , P H I L I P P I N E S
Flowcharts (continued)

Flowchart for calculating the


average of three numbers.

11/18/2019
M A N I L A , P H I L I P P I N E S
49 49
Guidelines for Drawing a Flowchart
• In drawing a proper flowchart, all necessary requirements
should be listed out in logical order.

• The flowchart should be clear, neat and easy to follow. There


should not be any room for ambiguity in understanding the
flowchart.

• The usual direction of the flow of a procedure or system is


from left to right or top to bottom.

11/18/2019 50
M A N I L A , P H I L I P P I N E S
• Only one flow line should come out from a process
symbol.

• Only one flow line should enter a decision symbol, but two or
three flow lines, one for each possible answer, should leave the
decision symbol.

11/18/2019 51
M A N I L A , P H I L I P P I N E S
• If the flowchart becomes complex, it is better to use
connector symbols to reduce the number of flow lines.
Avoid the intersection of flow lines if you want to make it
more effective and better way of communication.

• Ensure that the flowchart has a logical start and finish.

• It is useful to test the validity of the flowchart by passing


through it with a simple test data.

11/18/2019 52
M A N I L A , P H I L I P P I N E S
Advantages of using flowcharts

The benefits of flowcharts are as follows:

• Communication: Flowcharts are better way of communicating


the logic of a system to all concerned.

• Effective analysis: With the help of flowchart, problem can be


analyzed in more effective way.

• Proper documentation: Program flowcharts serve as a good


program documentation, which is needed for various purposes.

11/18/2019 53
M A N I L A , P H I L I P P I N E S
• Efficient Coding: The flowcharts act as a guide or blueprint during the systems
analysis and program development phase.

• Proper Debugging: The flowchart helps in debugging process.

• Efficient Program Maintenance: The maintenance of operating


program becomes easy with the help of flowchart. It helps the
programmer to put efforts more efficiently on that part

11/18/2019 54
M A N I L A , P H I L I P P I N E S
Limitations of using flowcharts
• Complex logic: Sometimes, the program logic is quite
complicated. In that case, flowchart becomes complex and
clumsy.

• Alterations and Modifications: If alterations are required the


flowchart may require re-drawing completely.

• Reproduction: As the flowchart symbols cannot be typed,


reproduction of flowchart becomes a problem.

11/18/2019 55
M A N I L A , P H I L I P P I N E S
Flowcharting Examples

Example 1

Draw a flowchart to find


the sum of first 50
natural numbers.

11/18/2019 56
M A N I L A , P H I L I P P I N E S
Example 2
Draw a flowchart
to find the largest
of three numbers
A,B, and C.

11/18/2019 57
M A N I L A , P H I L I P P I N E S
Example 3
Draw a flowchart for
computing factorial N (N!)
Where N! = 1 ´ 2 ´ 3 ´ …… N
.

11/18/2019 58
M A N I L A , P H I L I P P I N E S
Problem Solving Process

Input Process Output

11/18/2019 M A N I L A , P H I L I P P I N E S
59
Example Problem #1

Calculate and display the price of a number


of apples if the quantity in kg and price per kg
are given.

Input Process Output


• Quantity Price = Quantity * Price_per_kg
• Price_per_kg Price

11/18/2019 M A N I L A , P H I L I P P I N E S
60
Pseudocode

1. Start
2. Read quantity
3. Read price_per_kg
4. price  quantity *
price_per_kg
5. Print price
6. End

11/18/2019 M A N I L A , P H I L I P P I N E S
61
Flowchart: Calculate Price of Apples
Start

Input
Quantity
Input
Price_per_kg

Price = Quantity * Price_per_kg

Output
Price

End

11/18/2019 M A N I L A , P H I L I P P I N E S
62
Source Code
#include<iostream> Pre-processor
using namespace std;
int main() Main function
{ Start
int price = 0, quantity = 0;
Variable declarations
float price_per_kg = 0;
cout << "Enter quantity of an apple : " ;
cin >> quantity; Input
cout << "Enter price of an apple per kilogram : " ;
cin >> price_per_kg; Input
price = quantity * price_per_kg; computations
cout << "The price of an apple in kilogram is " << price << "\n";
system ("pause");
return 0;
Output
}
Stop

11/18/2019 M A N I L A , P H I L I P P I N E S
63
Example #2

A car park has the following charges:


The 1st hour costs Php 5.00. The
subsequent hour cost Php 2.00 per hour.
Write an algorithm based on a vehicle’s
entry and exit time.

Input Process Output

• Entry_time Charge
????
• Exit_time
11/18/2019 M A N I L A , P H I L I P P I N E S
64
Flowchart:
Calculate Car Park Charge

Start

Input Entry_time
Input Exit_time

Period = Exit_time – Entry_time

Charge = 5 If Charge = (Period -1) * 2


No Period > 1 Yes

Output
Charge

End
11/18/2019 M A N I L A , P H I L I P P I N E S
65
C++ Program: Calculate Car Park Charge
#include<iostream>
using namespace std;

int main()
{
int entry_time = 0, exit_time = 0, period = 0;
float charge = 0;
cout << "Enter entry time : " ;
cin >> entry_time;
cout << "Enter exit time : " ;
cin >> exit_time;
period = exit_time – entry_time;
If (period > 1)
charge = (period – 1)* 2;
else
charge = 5;
cout << “The total parking charge is “ << charge << “\n”;
system ("pause");
return 0;
11/18/2019 } M A N I L A , P H I L I P P I N E S
66
End of Module
Credit to FEU Institute of Technology

11/18/2019 M A N I L A , P H I L I P P I N E S
67
ASSIGNMENT 1

List and differentiate C++ from Python and in your research, which
is better?

ASSIGNMENT 2
How Python is applied in a Business Degree?

Cite some career opportunities that may lie ahead in learning


Python in your field of specialization.

Cite your references.


Printed in a short bond paper per assignment.
Calibri 11
1.5 spacing
Minimum of 200 words per assignment.
Deadline, October 8, 2019

11/18/2019 M A N I L A , P H I L I P P I N E S
68
ACTIVITY 2

Write a pseudocode and flowchart for the following situation:


A program will generate 1 random number (from 1 to 10). The program will ask
until such time that the user can guess the correct number.

Write a pseudocode and flowchart for the following situation:


A program will ask a number identify if number is greater or less than 50. If it is
50, the program will display “at the middle”.

Write a pseudocode and flowchart for the following situation:


A program will ask for your tuition fee, it will compute and display the breakdown
for: down payment, midterm, and final payment.

Printed in a short bond paper per assignment.


Calibri 11
You can use draw.io
Deadline, October 10, 2019

11/18/2019 M A N I L A , P H I L I P P I N E S
69

You might also like