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:
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
char userName
numStudents = 20
numStudents = numStudents + 1
userName = cindy
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
Page 2
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
userName = cindy
numStudents = two
userName = 75
userName = cindy
Pseudocode
statement
num test1, test2, total,
average
Vbscript statement
Page 3
wscript.stdout.write(Enter test 1: )
test1 = wscript.stdin.readline
wscript.stdout.write(Enter test 2: )
test2 = wscript.stdin.readline
Average = total / 2
wscript.stdout.writeline(average is & 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:
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
num MyNumber
Print Total
Print "Enter a Number"
Page 5
lozenge
Start and Stop
Start
Stop
Page 6