Professional Documents
Culture Documents
Dasar Pemrograman: Anita Qoiriah
Dasar Pemrograman: Anita Qoiriah
PEMROGRAMAN
Anita Qoiriah
.
Program
kumpulan langkah-langkah instruksi yang
mengatur komputer untuk mengerjakan tugas
yang diinginkan dan menghasilkan hasil yang
diinginkan.
Bahasa Pemrograman
Bahasa pemrograman
adalah sekumpulan
aturan untuk
memberitahu
komputer, operasi apa
yang harus dilakukan.
What Can a Program Do?
Sebuah hanya program dapat memberi instruksi
pada komputer untuk:
Membaca Input
Sequence
Menghitung
Menyimpan data
Membandingkan dan branch
Iterate or Loop
Menulis Output
Sequence Control Structures
Sequence control structures mengatur urutan
instruksi program.
Satu instruksi mengikuti instruksi yang lain(dalam
rangkaian) menentukan control dan urutan operasi.
Calculate
Sebuahprogram dapat
memberi instruksi
Add 1 to
sebuah komputer untuk Counter
melakukan operasi
matematika.
Store
A program will often
instruct a computer to Place 1
store intermediate results. in
Counter
Program Control Flow
Repetition statements
executed multiple times
continues to make a choice to carry out an action
until some criterion is met
Decisions
Exit
True
statement a
Syntax
Form 1 -- carry out an action or ignore it
if (boolean expression)
executable statement;
if (average < 0)
printf( You cannot have an average less than zero.\n);
Action
Test
condition p false true
false true
statement a Exit statement a
Syntax
if (boolean expression)
executable statement 1;
else
executable statement 2;
A simple IF-ELSE statement:
if (average >= 6)
printf( You have passed this course.\n);
else
printf( You did not pass this course.\n);
Action
No
Loops
q General purpose
handles all repetition situations
WHILE-DO statement
q Special-purpose
handles only some repetition situations, based
on characteristics of repetition
FOR-DO and REPEAT-UNTIL statements
Iteration Control Structures
Iterationcontrol structures are looping
mechanisms.
Loops repeat an activity until stopped. The
location of the stopping mechanism
determines how the loop will work:
Leading decisions
Trailing decisions
Leading Decisions
Ifthe stop is at the beginning of the iteration, then
the control is called a leading decision.
The command DO WHILE performs the iteration
and places the stop at the beginning.
Trailing Decisions
q Counting loops
q Sentinel (event)-controlled loops
Counting loops
count = 0;
while (count <= 10) {
count = count + 1;
printf (Counter: %d\n, count);
}
Comment
read (letter);
while (letter <> )
read (letter);
repeat
read (letter);
until (letter = );
DO WHILE Loop
Entry
Exit
Test No
condition p
Yes
Loop
statement a
Limitations
q None, really
q Some situations may be handled more
elegantly
with another type of loop, but thats really not a
limitation
Generic Syntax
Entry
Loop
statement a
Exit
Test No Yes
condition p
Programs are Solutions
to Problems
Programmers arrive at these solutions by using one
or more of these devices:
Logic flowcharts
Structure charts
Pseudocode
Structured Programming
Logic Flowcharts
Theserepresent the
flow of logic in a
program and help
programmers see
program design.
Common Flowchart Symbols
Common Flowchart Symbols
Terminator. Shows the starting and ending points of the program. A terminator has
flowlines in only one direction, either in (a stop node) or out (a start node).
Data Input or Output. Allows the user to inputdata and results to be displayed.
Decision. The diamond indicates a decision structure. A diamond always has two
flowlines out. One flowlineout is labeled the yes branch and the other is labeled the
no branch.
Connector. Connectors avoid crossing flowlines, making the flowchart easier to read.
Connectors indicate where flowlines are connected. Connectors come in pairs, one with
a flowline in and the other with a flowline out.
Off-page connector. Even fairly small programs can have flowcharts that extend several
pages. The off-page connector indicates the continuation of the flowchart on another
page. Just like connectors, off-page connectors come in pairs.
Flowline. Flowlines connect the flowchart symbols and show the sequence of operations
during the program execution.
Flowchart for a
Cash Register Program
Start
sum=0
Input price
sum=sum+price
Yes More
items?
No
tax=sum x 0.0725
total=sum+tax
Stop
Structure Charts
Structurecharts illustrate the structure of a
program by showing independent hierarchical
steps.
Major divisions are subdivided into smaller pieces
of information.
Psuedocode
This device is not visual but is considered a
first draft of the actual program.
Pseudocode is written in the programmers
native language and concentrates on the logic
in a programnot the syntax of a
programming language.
Example Loop Pseudo-code
q for count := 1 to 10 do
write (count);
q count := 0;
repeat
count := count + 1;
write (count);
until count > 10
Pseudocode for a
Cash Register Program
sum=0
While More items do
Input price
sum=sum+price
End While
tax=sum x 0.0725
total=sum+tax
Output sum, tax, total
Structured Programming
Structured program languages lend
themselves to flowcharts, structure charts,
and pseudocode.
Structured programming languages work
best where the instructions have been
broken up into small, manageable parts.
The Program Development Cycle
Analyze the problem
Machine language
Assembly Language
High Level Languages
Fourth Generation Languages (4GL)
Machine Languages
different for each computer processor
0100
001101 100000 001101 110001
00101 10001 10000
01110
111001
. . .
Assembly Languages
different for each computer processor
main proc pay
mov ax, dseg
mov ax, 0b00h
add ax, dx
mov a1, b1
mul b1, ax
mov b1, 04h
High-Level Languages
Higher Level Languages
Use traditional programming logic where the
programming instructions tell the computer what to do
and how to perform the required operations.
4GLs
Use high-level English-like instructions to specify what
to do, not how to do it .
Interpreter vs Compiler
Interpreter
Translates instructions to machine code line-by-line.
Compiler
Translates the entire program to machine code before
running it.
Types of Programming Languages
Machine language
Procedure-oriented languages
Object-oriented languages
Event-driven languages
Procedure-Oriented Languages
FORTRAN
COBOL
Pascal
C
Ada
OOED Languages
Object-oriented languages
Smalltalk
C++
Ada 95
Event-driven languages
VisualBasic
most Visual languages
Programmers Lingo
Program - detailed set of instructions for a computer
Interpreter Process
Programmers Lingo
Compiler Process