Pseudocode An Introduction. Flowcharts were the first design tool to be widely used, but...

9
Pseudocode An Introduction

description

What is Pseudocode? Pseudocode is "halfway" between english and a programming language. It is a description of a process in detail, though not necessarily in full sentences. The key is to provide enough information so that anyone could follow the instructions (a child, an alien, or even a computer, if the instructions were translated into a language it understands). Recipes are one type of pseudocode, instructions for assembling things are another.

Transcript of Pseudocode An Introduction. Flowcharts were the first design tool to be widely used, but...

Page 1: Pseudocode An Introduction. Flowcharts were the first design tool to be widely used, but unfortunately they do not reflect some of the concepts of structured.

Pseudocode

An Introduction

Page 2: Pseudocode An Introduction. Flowcharts were the first design tool to be widely used, but unfortunately they do not reflect some of the concepts of structured.

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. The drawback is that the narrative presentation is not as easy to understand and/or follow.

Page 3: Pseudocode An Introduction. Flowcharts were the first design tool to be widely used, but unfortunately they do not reflect some of the concepts of structured.

What is Pseudocode?• Pseudocode is "halfway" between english

and a programming language.• It is a description of a process in detail,

though not necessarily in full sentences. • The key is to provide enough information

so that anyone could follow the instructions (a child, an alien, or even a computer, if the instructions were translated into a language it understands).

• Recipes are one type of pseudocode, instructions for assembling things are another.

Page 4: Pseudocode An Introduction. Flowcharts were the first design tool to be widely used, but unfortunately they do not reflect some of the concepts of structured.

Rules for Pseudocode

• Write only one statement per line• Capitalize initial keyword• Indent to show hierarchy• End multiline structures• Keep statements language

independent

Page 5: Pseudocode An Introduction. Flowcharts were the first design tool to be widely used, but unfortunately they do not reflect some of the concepts of structured.

One Statement Per LineEach 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 ListRead name, hours worked, rate of pay

Perform calculations

gross = hours worked * rate of pay

Write name, hours worked, gross

PseudocodeREAD name, hoursWorked, payRate

gross = hoursWorked * payRate

WRITE name, hoursWorked, gross

Page 6: Pseudocode An Introduction. Flowcharts were the first design tool to be widely used, but unfortunately they do not reflect some of the concepts of structured.

Capitalize Initial KeywordIn 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

PseudocodeREAD name, hoursWorked, payRate

gross = hoursWorked * payRate

WRITE name, hoursWorked, gross

Page 7: Pseudocode An Introduction. Flowcharts were the first design tool to be widely used, but unfortunately they do not reflect some of the concepts of structured.

Example of Pseudocode for Washing the Dishes

• close the sink drain holesquirt some dish liquid in the sinkturn on hot waterWHILE sink is not full    wait 2 seconds    splash water to create bubblesturn off water(sink is now full of hot, soapy water)put a dishrag in the sinkWHILE there are dirty dishes    grab a dish    put dish in the sink    scrub dish with dishrag    IF dish is still dirty        THEN scrub dish with scouring pad    rinse dish    put dish in draineropen the sink drain holeput dishrag in laundry basket

• Drying the dishes is left as an exercise to the reader :) Even though this description is fairly complex, it still has some pieces missing. If there are nonstick dishes, we don't want to scratch them with the scouring pad. We don't want to wash greasy dishes first, since they will make the water dirty. There are no instructions for times when the dish drainer is full... The pseudocode you write should be detailed enough that it can be understood (by a child, alien, or computer) without being so detailed that it becomes difficult to follow.

Page 8: Pseudocode An Introduction. Flowcharts were the first design tool to be widely used, but unfortunately they do not reflect some of the concepts of structured.

Advantages & DisadvantagesFlowchart Advantages:

Standardized Visual

Pseudocode Advantages Easily modified Implements structured

concepts Done easily on Word

Processor

Flowchart Disadvantages: Hard to modify Structured design elements not

implemented Special software required

Pseudocode Disadvantages: Not visual No accepted standard, varies from

company to company