0% found this document useful (0 votes)
61 views50 pages

1 Basic Programming Concepts

The document discusses basic programming concepts including what a computer is made of and its key elements. It then covers the concepts of a computer program, what programmers do, and different levels of programming languages from low-level machine language to high-level languages. High-level languages make programming easier for humans by abstracting away from the underlying hardware.

Uploaded by

:]
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)
61 views50 pages

1 Basic Programming Concepts

The document discusses basic programming concepts including what a computer is made of and its key elements. It then covers the concepts of a computer program, what programmers do, and different levels of programming languages from low-level machine language to high-level languages. High-level languages make programming easier for humans by abstracting away from the underlying hardware.

Uploaded by

:]
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/ 50

Topic 1

Basic Programming Concepts

ITP3914 – Programming

(C) VTC, IVE 1


Part 1 – What is a computer?

(C) VTC, Prepared by [email protected] 2


A computer accepts external input, processes
the data, and produces certain output.

Performs computations and makes logical


decisions.

Millions / billions / trillions times faster than


human brains.
(C) VTC, Prepared by [email protected] 3
Key Elements in a Computer

(C) VTC, Prepared by [email protected] 4


 Input Unit  Arithmetic and Logic Unit
 Obtains information (data and (ALU)
programs) from various input  Performs actual calculations
devices, such as keyboard and and comparisons.
disk drive.  Control Unit
 Output Unit  Supervises and coordinates
 Places processed information the entire operations of a
onto various output devices, computer.
such as screen or printer.  Secondary Storage
 Memory Unit  A device, such as tapes or
 Retains information entered disks, for storing programs or
from the input unit. data that are not actively
 Stores programs. being used by other units.
 Stores intermediate results
during processing.
(C) VTC, Prepared by [email protected] 5
Part 2 – A first taste on programming

(C) VTC, Prepared by [email protected] 6


What is a computer program?

Computer programs (software)


• Sets of instructions for which computer processes
data
• E.g. Windows-8, MS Office 2013, Internet Explorer,
Adobe Photoshop, and lots more…
A computer can’t do anything without software.
It is the software that makes computer
intelligent!

(C) VTC, Prepared by [email protected] 7


Job of a Programmer

Software refers to a collection of instructions


for the computer to follow and execute.
A computer only follows what the programmer
tells it to do.
Therefore, the programmer has to know how to
solve problems, and

The job of a programmer is to design a


complete and precise set of instructions for
a computer to carry out a particular problem-
solving task.
(C) VTC, Prepared by [email protected] 8
Machine Language
At
Atthe
thelowest
lowestlevel
level(hardware),
(hardware), So
Soyou
youmay
mayinstruct
instructthe
thecomputer
computer
computers
computerswork
workwith
withbinary
binary directly
directlyusing
using00and
and1.1.This
Thisisiscalled
called
numbers
numbers(0s’
(0s’and
and1s’).
1s’). Machine
MachineLanguage
LanguageProgramming
Programming
0110001111000010
0001000111100011

Machine : c aann
a n
ss andd c
Language di
tteedi oouu y..
e r:yy
VVer rrorrsseeas a s iill y
Program a k ee eerro
mak
0011000100001000
m
nntt
e nddee
i ne ddeeppen
M aacchhine
M
1000001001010101
9

(C) VTC, Prepared by [email protected] 9


Do
Do you
you really
really want
want to
to program
program in
in
machine
machine language?
language?

(C) VTC, Prepared by [email protected] 10


Assembly Language

Assembly
AssemblyLanguage
Language––English-like
English-like
abbreviations
abbreviationsto
torepresent
represent computer
computer
operations.
operations. More
Morehuman-readable.
human-readable.

Assembly Language Machine Language


LD R0, #05 0110001111000010
ADD R0, $1234 0001000111100011
: :
:
Assembler
Assembler :
SUB R0, #22 0011000100001000
LD $2345, R0 1000001001010101
(C) VTC, Prepared by [email protected] 11
An
An assembly
assembly language
language program
program
is
is still
still too
too low-level
low-level and
and difficult
difficult
to
to aa human
human programmer!
programmer!

(C) VTC, Prepared by [email protected] 12


Low-Level Languages

Since assembly and machine languages are


oriented towards CPU, they are called low-
level languages.
Assembly and machine languages are very
difficult for human to read, write, and
understand.
Assembly and machine languages are CPU
dependent; a CPU cannot read programs
written for other CPU architectures.

(C) VTC, Prepared by [email protected] 13


High-Level Languages

Solution
Solution
High
High level
level programming
programming languages
languages
are
are provided
provided to
to close
close the
the gap
gap between
between
human
human programmers
programmers and and computers.
computers.

High Level Program


score = Homework + Quiz + Test;
if (score < 40)
retake();
else
print “Congratulations!”;

(C) VTC, Prepared by [email protected] 14


High-level Languages

Allow for writing more “English-like” instructions.


Contain commonly used mathematical operations.
By using a “translator”, high-level language
programs can run on different CPUs.
The speed of “translated programs” will be slower
than a low-level language programs.

(C) VTC, Prepared by [email protected] 15


score
score==Homework
Homework++Quiz
Quiz++Test
Test
IfIf(score
(score<<40)
40)
High
HighLevel
LevelLanguage
Language
retake();
retake();
Else
Else
print
print“Congratulations!”
“Congratulations!”

LD
LD R0,
R0,#05
#05
ADD R0, $1234
Assembly
AssemblyLanguage
Language
ADD
SUB
SUB
R0, $1234
R0,
R0,#22
#22
LD
LD $2345,
$2345,R0
R0

0110001111000010
0110001111000010
Machine
MachineLanguage
Language 0001000111100011
0001000111100011
0011000100001000
0011000100001000

Easier and Easier! No more excuse saying that this module is difficult!

(C) VTC, Prepared by [email protected] 16


C++, Java, C# are object-oriented
programming languages; reusable
Example HLLs software components that model real-
world items.

• Dinosaurs
• Cobol – for business data processing; still in use in banks
• Fortran – for scientific computation
• Middle Aged
• Pascal – a sophisticated but overly complicated HLL
• C – a widely used HLL for general computing
• Youngsters
• C++ – an enhancement over C with object-oriented features
• Java – an OO language with networking and lots other features
• C# - developed by Microsoft in competition with Java

Java
(C) VTC, Prepared by [email protected] 17
Your First Java Program

/*
/*
**The
TheHelloWorld
HelloWorldApplication
Application
*/
*/

public
publicclass
classHelloWorld
HelloWorld{{

public
publicstatic
staticvoid
voidmain(String
main(String[][]argv)
argv)
{{
System.out.println("Hello
System.out.println("HelloWorld!");
World!");
}}
}}

(C) VTC, Prepared by [email protected] 18


Part 3 – The Java program development process

(C) VTC, Prepared by [email protected] 19


There are just too much details that you don’t
have to understand at this moment.

But now we go through some more details in a


Java program.

(C) VTC, Prepared by [email protected] 20


HLL and compilation

HLL programs are written as text files (source code).


Source codes have to be compiled before it can be
executed.
A compiler is a piece of software that translates a high-
level language program to a machine language program.

(C) VTC, Prepared by [email protected] 21


Java compilation process

Edit

Compile

Execute

(C) VTC, Prepared by [email protected] 22


Use VSCode to edit
Edit
HelloWorld.java

c:\> javac HelloWorld.java


c:\> dir
Compile
1/09/2014 11:30 HelloWorld.java
1/09/2014 11:35 HelloWorld.class
c:\>

c:\> java HelloWorld


Execute
Hello World!

(C) VTC, Prepared by [email protected] 23


/*
/*
**The
TheHelloWorld
HelloWorldApplication
Application
*/
*/
If you type anything
public
publicclass
classHelloWorld
HelloWorld{{ incorrect, even missing a
public
publicstatic
staticvoid
voidmain(String
main(String[][]argv)
argv) single letter or a punctuation,
{{
System.out.println("Hello your program will fail to be
System.out.println("HelloWorld!")
World!")
}} compiled.
}}

c:\> javac HelloWorld.java


HelloWorld.java:9: ‘;’ expected
System.out.println(“Hello World!”)
^
1 error
c:\> dir
1/09/2014 11:30 HelloWorld.java
c:\>
(C) VTC, Prepared by [email protected] 24
Use VSCode to write
Edit
HelloWorld.java

c:\> javac HelloWorld.java


Compile
Compilation Error

c:\> java HelloWorld


Execute
Hello World! Execution Error

(C) VTC, Prepared by [email protected] 25


Java class library
Java already provides a huge library (more than
3000 ready-for-use classes in J2SE 7)

Collectively, known
as Java APIs
Application
Programming
Interfaces

(C) VTC, Prepared by [email protected] 26


IDE for Java

IDE (Integrated Development Environment)


• A tool that helps programmer to develop and debug
his/her programs.
• Netbeans (from Sun, thus Oracle)
• Jdeveloper (from Oracle)
• JBuilder (from Borland)
• Eclipse (An IDE that supports
many different programming
languages, not just Java)

Eclipse
(C) VTC, Prepared by [email protected] 27
Part 4 – Some more details on Java programs

(C) VTC, Prepared by [email protected] 28


Java Program Structure

/*
* The HelloWorld Application Multi-line Comment
*/

public class HelloWorld { Class header

public static void main(String [] argv) Method main(). The core


{ part of the program which
System.out.println("Hello World!"); defines what the program
} should do.
}

This is the core statement in this


Don’t
Don’task
askme
mewhat
what simple program. It prints the string
these
theseare!
are!Just
Justcopy!
copy! enclosed by “ “ on the screen.
(C) VTC, Prepared by [email protected] 29
A Java program is made up of class definitions.
A class definition contains a header and a body.
• The header defines the name of the class (and others).
public class HelloWorld
• In this example, the body consists of main().

The filename must be same as the class name


with .java appended, i.e. HelloWorld.java, in
this example.

(C) VTC, Prepared by [email protected] 30


A More Complex Java Program
public
publicclass
classCircle
Circle{{
What is the
public purpose of this
publicstatic
staticvoid
voidmain(String
main(String[][]args)
args)
{{ program?
int
int radius;
radius; Can you guess?
double
double area;
area;
c:\> java Circle
radius Radius = 3
radius==3;
3;
System.out.println("Radius Area = 28.26
System.out.println("Radius==""++radius);
radius);

area The program needs


area==radius
radius**radius
radius**3.14;
3.14;
a place to store the
System.out.println("Area
System.out.println("Area==""++area);
area); area of the circle
}} before it is printed.
}}
VARIABLES!!!!!
(C) VTC, Prepared by [email protected] 31
Variables

Variables are storage spaces for your program


to hold data values.
Variables are being stored in the computer
memory.
Variables must be declared (created):
• Type of data to be stored inside
• Name of the variables

(C) VTC, Prepared by [email protected] 32


public
publicclass
classCircle
Circle{{
Data type
public
publicstatic
staticvoid
voidmain(String
main(String[][]args)
args)
{{
int
int Name of the variable
radius;
radius; radius area
double
double area;
area;

radius Assignment
radius==3;
3;
3
System.out.println("Radius
System.out.println("Radius==""++radius);
radius); radius area

area
area==radius
radius**radius
radius**3.14;
Expression
3.14;
System.out.println("Area 3 28.26
System.out.println("Area==""++area);
area);
}} radius area
}}
You
Youcan
canalso
alsoprint
printthe
thevalue
value
stored in a variable.
stored in a variable.
(C) VTC, Prepared by [email protected] 33
Does it work???

public
publicclass
classCircle
Circle{{

public
publicstatic
staticvoid
voidmain(String
main(String[][]args)
args)
{{
radius
radius==3;
3;
int
int radius;
radius;

System.out.println("Radius
System.out.println("Radius==""++radius);
Strangely, students just
radius);
System.out.println("Area
System.out.println("Area==""++area);
area);put all the things into a
program and HOPE they
double
double area;
area;
area
work correctly together.
area==radius
radius**radius
radius**3.14;
3.14;
}}
}}

(C) VTC, Prepared by [email protected] 34


Part 5 – Steps in program development

(C) VTC, Prepared by [email protected] 35


Recall that …

(C) VTC, Prepared by [email protected] 36


Steps in Program Development

1. Define the problem


2. Outline the solution
3. Turn the outline into steps for solving the problem
4. Test the steps for correctness
5. Code the steps into a targeted programming
language
6. Run the program on a computer
7. Document and maintain the program

(C) VTC, Prepared by [email protected] 37


Based on the problem description, divide the
problem into three separate components:
• Input source data
• Perform processing steps needed to produce the
required outputs
• Output results required

Processing
(C) VTC, Prepared by [email protected] 38
Basic operations

There are six basic operations in a solution:


• Inputting the data
• Outputting the result (and other intermediate messages)
• Performing processing and calculation
• Storing a data into variables
• SELECTION – comparing two different values and
selecting alternative actions
• REPETITION – repeating a group of actions

(C) VTC, Prepared by [email protected] 39


How can the steps in a solution be expressed
precisely and concisely?

Answer: Flowcharts
Graphically depict the logical steps to
carry out a task and show how the steps
relate to each other.

(C) VTC, Prepared by [email protected] 40


Flowchart Symbols
Used to connect symbols
Flow line and indicate the flow of logic.

Used to represent the beginning (Start) or


Terminal the end (End) of a task.

Used for input and output operations,


Input/Output such as reading and displaying. The data to
be read or displayed are described inside.

Used for arithmetic and data-manipulation


Processing operations. The instructions are listed
inside the symbol.

Used for any logic or comparison operations.


Unlike the input/output and processing symbols,
Decision which have one entry and one exit flowline, the
decision symbol has one entry and two exit
paths. The path chosen depends on whether the
answer to a question is “yes” or “no”.

(C) VTC, Prepared by [email protected] 41


Connector Used to join different
flowlines.

Used to indicate that the


Offpage Connector flowchart continues to a
second page.

Used to represent a group of


Predefined Process statements that perform one
processing task.

Used to provide additional


Annotation information about another
flowchart symbol.

(C) VTC, Prepared by [email protected] 42


The Structure Theorem

The Structure Theorem states that it is possible


to write ANY computer program by using only
the three basic control structures:
• Sequence
• Selection
• Repetition

(C) VTC, Prepared by [email protected] 43


Series of steps – Sequence
Start
Process:
Input: speed = distance / time
distance
Input:
Input: distance Strangely, students
time like their programs to
rush for calculation
Input: before data input.
Process: time
speed = distance / time

Output:
Output:
speed
speed

End
(C) VTC, Prepared by [email protected] 44
Start
Example
Input:
number1
Problem:
A program is required to get three Input:
numbers and then add them together number2
and give their total.
Input: Input:
number3
3 numbers
Process: Process:
calculate the total of the 3 numbers total = number1 +
number2 + number3
Output:
total of the 3 numbers
Output:
total

End
(C) VTC, Prepared by [email protected] 45
Decision flow chart – Selection

Comparing two
different values and
selecting alternative
actions false true
Is condition true?

Process step(s) 2 Process step(s) 1

(C) VTC, Prepared by [email protected] 46


Start

Example Input:
Octopus balance

Problem:
Determine if Octopus payment Input:
is accepted. If so, what the payment amount
new balance is?
Input: amount
Octopus balance, <=
false true
(balance+50)
payment amount
Process: Output: newBalance =
balance - amount
Check existing balance. “Insufficient
Balance”
Deduct payment amount from
balance if it is sufficient. Output:
newBalance
Output:
new balance or “insufficient
Balance”

(C) VTC, Prepared by [email protected] End 47


Looping flow chart – Repetition

Repeating a group of
actions

Is condition true?
false

true

Process step(s)

(C) VTC, Prepared by [email protected] 48


Start

set numStudent=0

Example
set total=0

Problem: Loop Input:


an individual mark
Calculate and report the
average mark for a class of an
examination. add mark to total

Input:
add 1 to numStudent
individual marks
Process: true more student
Sum individual marks to a total. marks?
Count the number of students.
Calculate average as false
total/count.
average = total / numStudent
Output:
average mark
Output: average

(C) VTC, Prepared by [email protected] 49


End
END

(C) VTC, Prepared by [email protected] 50

You might also like