Pseudocode Rules for use in exercises:
Programs written in pseudocode are like blueprints. They will not really run on
a computer, but help the programmer understand the plan for getting a computer
to solve a problem. Psuedocode is very high-level
(English-like) and is therefore easy to understand.
Keywords:
A very
simple set of instructions to be used to express an algorithm. Use only these keywords in expressing your
algorithms.
INPUT
Example:
INPUT X
Description: allows the user
to input a value which goes into a specified variable.
OUTPUT
Example:
OUTPUT X
Description: prints out the
value of a variable.
WHILE
Example:
WHILE X < Y
INPUT X
Description: Keeps doing a
set of statements until a condition is true.
FOR <variable> = <value> to <value>
Example:
FOR X = 1 TO 100
INPUT Y
Description: repeats a set
of statements a definite number of times. The example will repeat INPUT Y 100
times.
IF - THEN
Example:
IF X = Y THEN
OUTPUT “X and Y are equal”
Description: Performs a
check to see if a comparison is true or not. If it is true, the set of
statements associated with the IF-THEN are performed, otherwise they are
skipped.
ELSE
Example:
IF X = Y THEN
OUTPUT “X and Y are equal”
ELSE
OUTPUT “X and Y are not equal”
Description: Performs a
check to see if a comparison is true or not. If it is true, the set of
statements associated with the IF-THEN are performed, otherwise they are
skipped and a set of statement associated with the ELSE are performed instead.
Variables:
Make up your own names such
as X or PAYCHECK or SUM
Assignment Statements:
Use the Variables and
constant values such as 67.9 in your expressions, then
assign the results of these expressions into variables you have created.
Indentation to show control:
FOR, WHILE and IF-THEN
instructions require that a set of statements be associated with them. Indent the lines below these instructions
that are controlled by them. Indenting a line after and IF-THEN, for example, will
show that line will only run if the IF-THEN is true.
X = 1
WHILE X <= 7
OUTPUT X
X = X + 1
(above
statements print 1 to 7)
FOR Y = 1 TO 7
OUTPUT Y
(above
statements print 1 to 7)
OUTPUT 1
OUTPUT 2
OUTPUT 3
OUTPUT 4
OUTPUT 5
OUTPUT 6
OUTPUT 7
(above
statements print 1 to 7)