• Tidak ada hasil yang ditemukan

Structured Programming

N/A
N/A
Protected

Academic year: 2018

Membagikan "Structured Programming"

Copied!
47
0
0

Teks penuh

(1)

Structured Programming

(2)

Objectives

1.To describe flowcharts

2.To show some standard symbols for flowcharts

3.To show examples of flowcharts 4.To practice using flowcharts

5.To describe pseudocode

6.To show examples of pseudocode 7.To practice using pseudocode

(3)

Introduction

• Before writing a program:

• Have a thorough understanding of the problem

• Carefully plan an approach for solving it

• While writing a program:

• Know what “building blocks” are available

(4)

Flowcharting

• Flowcharting is a method of designing a

solution to a problem by using symbols which indicate actions to be performed.

• It is customary to use geometric figures of

certain shapes to indicate the various kinds of acts.

(5)

Algorithm

• An algorithm is a well-defined sequence of

acts or procedures to perform for the solution of a problem in a finite number of steps.

(6)

Flowcharting Symbols

• START or STOP symbol

• Input/Output symbol representing one or more input/output operations • Processing symbol representing

(7)

• Decision symbol representing a branching point in the program

• Flowlines or arrows that indicate the next step of the flowchart

• In-Connector and

• Out-Connector used to show the flow from one part to another part of the program

1

(8)

Control Structures

• Bohm and Jacopini

• All programs written in terms of 3 control structures

• Sequence structures. Programs executed sequentially by default

• Selection structures: Java has three types: if, if/else, and case

• Repetition structures: Java has four types:

(9)

9

Control Structures

Single-entry/single-exit control

structures

Connect exit point of one control

structure to entry point of the next (control-structure stacking)

(10)

Control Structures - Sequence

• Each action is in a processing symbol by itself.

• The actions are performed in the sequence (following the arrows)

Example : What do you do in the

morning?

• Brush teeth • Wash face • Comb hair

(11)

11

Control Structures-Transfer of

Control

• When the next statement executed is not the next one in sequence

(12)

The

if

Selection Structure

Ex. If grade >= 60, print

something

Diamond symbol (decision

symbol)

Indicates decision is to be made

Contains an expression that can

be true or false

Test the condition, follow

(13)

13

The

if

Selection Structure

if structure is a

single-entry/single-exit structure

true

false

grade >= 60 print “Passed”

 

A decision can be made  on any expression. 

zero - false  nonzero - true Example:

(14)

The

if

/

else

Selection

Structure

if

Only performs an action if the

condition is true

if/else

Specifies an action to be

performed both when the

condition is true and when it is

false

(15)

15

The

if

/

else

Selection Structure

Flow chart of the if/else selection structure

Nested if/else structures

Test for multiple cases by placing if/else selection structures inside if/else selection structures

Once condition is met, rest of statements skipped

Deep indentation usually not used in practice

true false

print “Failed” print “Passed”

(16)

The

while

Repetition Structure

• Repetition structure

• Programmer specifies an action to be repeated while some condition remains

true

• Psuedocode:

While there are more items on my shopping list Purchase next item and cross it off my list

• while loop repeated until condition becomes false

(17)

17

The

while

Repetition Structure

Example:

int product = 2;

while ( product <= 1000 ) product = 2 * product;

product <= 1000 true product = 2 * product

(18)

Example 1:Calculating

Revenue

Suppose that we have the problem of calculating revenue which has the following specification

• Basic Service Charge: $25.00 • Unit Cable Cost: $2.00

(19)

The Beginning of the Flowchart

START

?

(20)

First Step-Input

START

Input

1.Basic Service Charge 2.Unit Cable Cost 3.Number of Installations

(21)

Processing

START

Input

1.Basic Service Charge 2.Unit Cable Cost 3.Number of Installations

4.Yards Of Cable

Process

(22)

Output

START

Input

1.Basic Service Charge 2.Unit Cable Cost 3.Number of Installations

4.Yards Of Cable

Process

(23)
(24)

Example 2:Averaging

Grades for 5 students

• Suppose that we have the problem of calculating the average grade for

each student in a class where five examinations have been given.

(25)

The Flowchart

Start

INPUT Five grades

Calculate the Average

Print the Result

(26)

Example 3:Averaging Final

Grades for all Students

• Suppose that the students in a

class took their final examination yesterday and the instructor has just finished grading the exam

and is studying the grades. • We want to start planning a

(27)

The Steps

• We begin to think about the

problem as we look at the grades in the grade book .

(28)

Start With the Input

Start

(29)

Next Step is Processing

Start

INPUT Five grades for the first student

(30)

Controlling the Flow

• Then some statement will be

needed to allow control to return to the top of the program to see if

there is another set of student records to process.

(31)

The Flowchart For

n

Students

Start

INPUT Five grades for the first student

Calculate the Average

More students ?

Yes

(32)

End

•When there are no more

(33)

Complete Flowchart For

n

Students

Start

INPUT Five grades for the first student

Calculate the Average

End

More students ?

Yes

(34)

Pseudocode

• Flowcharts were the first design tool to be widely used, but unfortunately they do not reflect some of the

concepts of structured programming very well.

• Pseudocode, on the other hand, is a newer tool and has features that make it more reflective of the structured concepts.

(35)

What is it?

• Pseudo code is a kind of structured english for describing algorithms.

• It allows the designer to focus on the logic of the algorithm without being distracted by details of language syntax.

• At the same time, the pseudo code needs to be complete.

It describes:

• the entire logic of the algorithm so that implementation becomes a rote

• mechanical task of translating line by line into source code.

(36)

Pseudocode

Artificial, informal language that

helps us develop algorithms

Similar to everyday English

Not actually executed on

computers

Helps us “think out” a program

before writing it

(37)

Rules for Pseudocode

• Write only one statement per line • Capitalize initial keyword

• Indent to show hierarchy • End multiline structures

(38)

One Statement Per Line

Each statement in pseudocode should express just one action for the computer. If the task list is properly drawn, then in most cases each task will correspond to one line of pseudocode.

Task List

Read name, hours worked, rate of pay

Perform calculations

Pseudocode

READ name, hoursWorked, payRate

(39)

Capitalize Initial Keyword

In the example below note the words: READ and WRITE. These are just a few of the

keywords to use, others include:

READ, WRITE, IF, ELSE, ENDIF, WHILE, ENDWHILE

Pseudocode

READ name, hoursWorked, payRate

gross = hoursWorked * payRate

(40)

Indent to Show Hierarchy

• Sequence:

Keep statements in sequence all starting in the same column

• Selection:

Indent statements that fall inside selection structure, but not the keywords that form the selection

• Loop:

Indent statements that fall inside the loop but not keywords that form the loop

Each design structure uses a particular indentation pattern

(41)

End Multiline Structures

In the IF/ELSE/ENDIF as constructed above, the ENDIF is in line with the IF.

The same applies for WHILE/ENDWHILE etc… READ name, grossPay, taxes

IF taxes > 0

net = grossPay – taxes ELSE

net = grossPay ENDIF

(42)

Language Independence

• Resist the urge to write in whatever language you are most comfortable with, in the long run you will save time.

(43)

The Selection Structure

amount < 100

interestRate = .06 interestRate = .10

yes no

IF amount < 100

interestRate = .06 ELSE

Interest Rate = .10 ENDIF

(44)

The Looping Structure

In flowcharting one of the more

confusing things is to separate selection from looping. This is because each

structure uses the diamond as their control symbol.

In pseudocode we avoid this by using specific keywords to designate looping

(45)

WHILE / ENDWHILE

WHILE count < 10 ADD 1 to count

(46)

REPEAT / UNTIL

UNTIL count >= 10 WRITE “The End”

Mainline count = 0 REPEAT

DO Process UNTIL count >= 10

(47)

Advantages & Disadvantages

Easily modified

Implements structured

concepts

Done easily on Word

Processor

Flowchart

Disadvantages:

Hard to modifyStructured design

elements not implemented

Special software required

Pseudocode

Disadvantages:

Not visual

No accepted standard,

Referensi

Dokumen terkait

[r]

Namun secara spesiik yang dimaksud pekerja sosial profesional pada Bagian ketiga Undang-Undang Nomor 11 Tahun 2012 tentang Sistem Peradilan Pidana Anak Pasal 66

Apabila dari peserta merasa keberatan atas Pengumuman Pemenang Pelelangan Umum ini, dapat menyampaikan sanggahan secara elektronik melalui aplikasi SPSE kepada

Demikian Pengumuman Pemenang Pelelangan ini dibuat dan ditandatangani pada hari, tanggal dan bulan sebagaimana tersebut di atas untuk dipergunakan sebagaimana

Pembuktian kualifikasi harus dihadiri oleh penanggung jawab atau yang menerima kuasa dari direktur utama/pimpinan perusahaan atau pejabat yang menurut perjanjian kerja

Dengan ini POKJA II Bagian Layanan Pengadaan Barang dan Jasa Sekretariat Daerah Kota Makassar mengundang calon penyedia Paket Pekerjaan Belanja Modal Pengadaan

Berdasarkan ketentuan yang tertuang dalam Perpres 54 Tahun 2010 dan Perpres 70 Tahun 2012 beserta perubahannya serta hasil pembukaan tersebut diatas, maka

On the other hand, the odds a male student would choose business a major were about 30% lower if he had AP credit in high school, although this may not be true of female students