Your SlideShare is downloading. ×
  • Like
If412 02
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×

Now you can save presentations on your phone or tablet

Available for both IPhone and Android

Text the download link to your phone

Standard text messaging rates apply

If412 02

  • 409 views
Published

 

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
    Be the first to like this
No Downloads

Views

Total Views
409
On SlideShare
0
From Embeds
0
Number of Embeds
0

Actions

Shares
Downloads
3
Comments
0
Likes
0

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. IF412 – Konsep Bahasa Pemrograman Pertemuan : 02 PROGRAMMING WITH ASSIGNMENT Perhatikan potongan program berikut : while x < > A[i] do i := i - 1; end potongan program di atas mengandung 3 konsep utama dalam pemrograman, yaitu : • Assignment • Assignable data structures • Control flow statement Bahasa pemrograman Modula-2 dan C adalah contoh bahasa pemrograman Imperative, yang prinsip rancangannya berdasarkan pada : • Machine model • Structured programming Evolusi Bahasa Imperative Fortran Lisp Algol 60 ISWIM CPL* Prolog Algol 68 Concurrent BCPL Pascal Pascal Scheme Simula 67 ML C Distributed CSP Processes Mesa Smalltalk-80 Modula-2 C++ Ada Oberon Standard ML BINA NUSANTARA Edisi : 1 Revisi : 1 Sept - 1998
  • 2. IF412 – Konsep Bahasa Pemrograman Pertemuan : 02 Publication Format for Program • Contoh : publication format Pascal Modula-2 C While x < > A[i] do i := i - 1; while x < > A[i] do i := i - 1; while (x != A[i]) end i := i - 1; The Effect of an Assignment Assigment mengubah nilai dari lokasi tertentu di dalam mesin.Random access machine terdiri dari • Memori • Program (Assignment, I/O, Control Flow) • Input / Output File 1: read M[1] Input 2: read M[2] 3: M[1] := M[1] - M[2] Control 4: if M[1] >= 0 then goto 3 5: M[1] := M[1] + M[2] 6: write M[1] 7: halt Output 0 1 2 3 Memory 27 10 ... Fig. 3.1. A random-access machine (RAM). • Bentuk assignment pada gambar di atas : <expression>1 := <expression>2 <expression>1 = menunjuk ke suatu lokasi memori (disebut dengan L-value). <expression>2 = menunjukkan sebuah nilai (R-value). • Contoh Control Flow if M[j] > = 0 then goto I =================================== M[l] := n M[l] := M[j] + M[k] Assignments M[l] := M[j] - M[k] M[l] := M[M[j]] M[M[j]] := M[k] ________________________________________ read M[l] Input/Output write M[j] ________________________________________ Control Flow if M[j] >= 0 then goto I halt Fig. 3.2. Instruction set for a random-access machine. BINA NUSANTARA Edisi : 1 Revisi : 1 Sept - 1998
  • 3. IF412 – Konsep Bahasa Pemrograman Pertemuan : 02 • Assignment melalui indirect address : l j k M[l] := M[M[j]] l j k k n n k n (a) l j k M[M[j]] := M[k] l j k l n n l n (b) Fig. 3.3. Assignments through indirect addresses Dynamic Thread THREAD STATE Instruction Remaining Location Output Input 1 2 Produced 27, 10 1: read M[1] 10 27 2: read M[2] 27 10 3: M[1] := M[1] - M[2] 17 10 4: if M[1] >= 0 goto 3 17 10 3: M[1] := M[1] - M[2] 7 10 4: if M[1] >= 0 goto 3 7 10 3: M[1] := M[1] - M[2] -3 10 4: if M[1] >= 0 goto 3 -3 10 5: M[1] := M[1] + M[2] 7 10 6: write M[1] 7 10 7 7: halt 7 10 7 Fig. 3.4. A thread through the program in Fig. 3.1. STRUCTURED PROGRAMMING • Definisi • Latar Belakang Structured Control Flow • Composition Jika S1, S2, ... Sk adalah statement-statement maka komposisi urutan statement ditulis: S1; S2 ; ... Sk; Control flow : S1⇒ S2 ⇒ ... Sk; Jika k = 0 ⇒ disebut empty statement • Conditional BINA NUSANTARA Edisi : 1 Revisi : 1 Sept - 1998
  • 4. IF412 – Konsep Bahasa Pemrograman Pertemuan : 02 Jika E adalah sebuah ekspresi dan SL1 dan SL2 adalah statement list, kemudian Conditional Statement dibentuk sbb: if E then SL1 else SL2 end if E then SL1 end Control flow : SL1 bila E benar SL2 bila E salah • Loop forever Jika SL adalah statement list, maka bentuk iterasi / loop sbb : loop SL end Ada beberapa statement yang digunakan untuk keluar dari loop seperti : exit, break. • While - Loop Jika E adalah ekspresi, dan SL adalah statement list : while E do SL end Control flow : E & SL dikerjakan secara bergantian selama E benar, bila E salah kerjakan statement berikutnya setelah while-loop. if E then SL1 else SL2 end while E do SL end • • true E true E SL1 SL2 • SL • Fig. 3.5. Conditionals and while-loops. Invariant sebagai jembatan antara static & dynamic computation while x ≥ y do { apabila sampai di sini → x ≥ y } ⇐ invariant x := x - y; end • Tempat-tempat invariant : 1. while {loop invariant} E do SL end 2. loop {loop invariant} while E do SL end 3. Precondition → tepat sebelum statement 4. Postcondition → tepat sesudah statement • Contoh : Linear Search 0 1 n limit ... used free Fig. 3.6. Table organization for linear search with a sentinel. BINA NUSANTARA Edisi : 1 Revisi : 1 Sept - 1998