Anda di halaman 1dari 6

Flowcharts and Pseudocode

There is little difference between writing a script and writing a program. The fundamental concepts are the
same: Both use a series of commands to complete a task. They both use the same logic, the same
design rules, and (sometimes) even the same syntax (syntax refers to the way a command is phrased).
For the purposes of this class, we will think of a script and a program as being the same thing, and well
use the terms interchangeably. But, in case youre ever at a cocktail party and someone asks you the
difference between scripts and programs, here is a quick list that you can use in your reply.

Scripts are short and use, at most, a dozen lines of code, while programs are often comprised of
thousands of lines of code.

Scripts solve a single problem, whereas programs solve a bunch of problems (MS Word and
Excel are programs).

Scripts are stored as text files and depend upon a program called an Interpreter to run. Programs
are compiled into machine language and execute on their own.

Examples of scripting languages include Visual Basic Script (VBS), JScript, HTML, Batch files,
PowerShell, PHP, Perl, Python, and Bash Shell scripts. These are all interpreted languages that require a
program to read and carry out the commands contained in a text file. Examples of non-scripting
languages include C++, C#, Cobol, Fortran, Pascal, and Visual Basic.net. These are all compiled
languages in which the commands are converted into executable files that can run on a computer by
themselves.
This section of the lecture is designed to help you focus on the key elements of pseudocode, flowcharts,
and programming. Throughout this class, we will continue to add to our understanding of the topics that
follow.
IPO: Input, Processing, and Output
The programming model that we will mainly study is called the IPO model. This technique divides a
programming problem into three phases: data input, data processing, and output. During the input phase,
data are entered into a program either through the keyboard, from a file, or from an instrument (such as
an electronic thermometer). In the data processing phase, the data are manipulated to produce a result.
In the output phase, the processing result is displayed to a monitor, printed, or saved to a file.
Variables
Data that are read from a keyboard or file are stored in the computers random access memory.
Programmers are required to give unique names to each memory location where data are stored. These
named memory locations are known as variables. They are called variables because their values can be
changed during the execution of the program. Variables have three essential characteristics: a name, a
data type, and a value.
To create a variable, you must declare it by specifying a name for it. This allows memory to be allocated
to store data. The name is used to access the data stored in memory (the value). There are rules for
naming variables:

It MUST start with a letter.


It CANNOT include spaces.
It CANNOT be a keyword (word with special meaning in the language).
It can be any combination of letters (upper and lower case) and numbers.
The only special character allowed is the underscore character.

Flowcharts and Pseudocode

Page 1

In addition, your variable names should be meaningful. For example, a variable that will store the size of
your hard drive could be called diskSize or diskSpace. If you named this variable num, you could still do
the programming, but you might get confused at some point regarding the data stored in the variable.
Examples of valid variable names: user_ID, ipaddress, filename, budget2013
Examples of invalid variable names:
User ID
Cannot have a space
2013budget
Cannot start with a number
File#name
Cannot use any special characters other than the underscore.
When you declare a variable, you must also specify the type of data it will store, which is its datatype.
Datatypes allow the computer to reserve the proper amount of space in memory for the data. Numbers,
dates, and characters all require different storage space in a computer's memory. Each programming
language has a unique technique required to declare a variable. The pseudocode in our textbook uses a
generic approach to declaring variables in which the data type precedes the variable's name. For
example, here are two pseudocode-variable declarations.
char myName
num myAge

declare or create a variable that will store text or character (char) data
declare or create a variable that will store numeric (num) data

Assignment Statements
The data stored in a variable determine the value of the variable. Variables can be assigned a value
through an input statement or an assignment statement. The first time that a variable receives a value is
known as variable initialization. Good programming practice requires that all variables be initialized to a
value before they are used in a calculation.
Creating an assignment statement involves using the variable's name, the assignment operator, and the
value that will be stored to the variables memory location. Different programming languages use different
assignment operators, but most use some form of the equal symbol (=) to represent an assignment.
Consider the examples below:

Pseudocode

Explanation

num numStudents

Declare a variable named numStudents that holds


numeric data.

char userName

Declare a variable named userName that holds


character or text data.

numStudents = 20

Assign the value 20 to the variable numStudents.

numStudents = numStudents + 1

Add 10 to the value stored in the variable


numStudents. The new value is 30.

userName = cindy

Assign the value cindy to the variable userName.


Notice that character or text data must be
surrounded by double quotation marks to
distinguish it from a variable name.

Note that in the examples above, the variables were first declared to specify their datatype. In order to
write a proper assignment statement, you must know the datatype of your variables. Assignment

Flowcharts and Pseudocode

Page 2

statements have two important rules:

The variable receiving the data MUST be on the LEFT SIDE of the assignment operator (=).
The datatypes on the left and right sides of the assignment operator must be the same.

Based on these rules and the datatypes assigned above, the following examples can be determined to be
valid or invalid assignment statements.

statement

Valid or invalid?

numStudents = 10

Valid because the variable numStudents was


specified to hold numeric data, and 10 is a number.

userName = cindy

Valid because userName was specified to hold


character or text data, and cindy is text data.
INVALID because numStudents is numeric and
cannot store character or text data.

numStudents = two

userName = 75

INVALID because userName is char and cannot


store numbers.

userName = cindy

INVALID because without quotes, cindy is assumed


to be a variable, and we do not know the data type
of this variable.

Algorithms and Pseudocode


Instructions are everywhere in the work world. Chefs have their recipes; pilots have their flight plans; and
programmers have their algorithms, pseudocode, and flowcharts. An algorithm lists the steps required to
solve a problem. A programmer will create an algorithm using an a la carte mixture of English and actual
programming instructions, known as pseudocode. The purpose of pseudocode is to help the
programmer organize his or her thoughts and focus on the logic of a problem before worrying about the
intricacies of coding a solution in a programming language. This is an important point, because
oftentimes, what can be expressed as a single sentence in English will take many lines of cryptic
programming code to implement. When you try to solve a complex problem, you dont want to wrestle
with both the problem and with trying to decipher the programming code that you just wrote. It is much
easier to flush out a plan of attack in English (pseudocode) and then convert it to the programming
language later. The box below demonstrates the value of pseudocode.

Pseudocode
statement
num test1, test2, total,
average

Vbscript statement

Dim test1, test2, total, average

Flowcharts and Pseudocode

Page 3

Read two test scores.

wscript.stdout.write(Enter test 1: )
test1 = wscript.stdin.readline
wscript.stdout.write(Enter test 2: )
test2 = wscript.stdin.readline

Calculate the total.

Total = test1 + test2

Calculate the average.

Average = total / 2
wscript.stdout.writeline(average is & average)

Display the average.

In this comparison of pseudocode and vbscript syntax, you can see that there is a big difference between
the pseudocode and the actual programming statements. Notice how much easier it is to understand the
pseudocode. When writing vbscript, you must pay attention to syntax such as parentheses and quotes,
along with specific keywords. This illustrates the power of pseudocode: It allows you to work through a
problem in English without worrying about all of the details required to create a working program.

Flowcharts
We all know that it is often easier to understand the picture provided in assembly instructions than it is to
read the text of those instructions. Flowcharts help us visualize an algorithms logic using special
symbols. Each element of an algorithm can be represented using one of four standard symbols: a circle,
rectangle, parallelogram, or diamond. The picture that the flowchart paints or, perhaps more appropriately,
the map it draws, allows a programmer and others to easily trace the different logical paths that the
program may take, and provides a means to see how all of the different program elements fit together.
Here is a flowchart for the previous problem:

Flowcharts and Pseudocode

Page 4

Below is a list of pseudocode keywords and flowchart symbols that we will use in this course. You need to
memorize this chart--all of the symbols, the keywords, and what each one means.
Item

Pseudocode
Keyword

Flowchart Symbol

Example

parallelogram
Get

Get UserNumber

Read

Read inventoryRecord

Assignment
statements

MyNumber = 5 + 6

Input

rectangle
Process

Variable declarations

Output

Print
parallelogram

Flowcharts and Pseudocode

num MyNumber

Print Total
Print "Enter a Number"

Page 5

lozenge
Start and Stop

Start
Stop

Program Development Cycle


An effective programmer develops a plan of action before writing a single line of
code. The programming process includes the following steps:
1. Analyze: Define the problem to determine the inputs and outputs.
2. Design: Plan and document the algorithm (processing steps) using a
flowchart or pseudocode.
3. Desk-check the algorithm: Walk through your algorithm with some data as
the computer would do to validate your algorithm. If you find problems,
return to Step 2 to correct the design.
4. Code the algorithm into a program: You should only start coding once you
have a valid algorithm to follow.
5. Test and debug the program.

Flowcharts and Pseudocode

Page 6

Anda mungkin juga menyukai