Fundamental instructions are "generic" instructions available for constructing algorithms. They are the "building blocks" for program design. Each fundamental instruction is language independent but it has its counterpart in a high-level language.
The fundamental instructions are-- assign instruction, read in instruction, write out instruction, loop instruction, selection instruction. Subroutine calls are considered by some to be fundamental instructions.
First, some definitions:
VARIABLE
Name of a memory
location whose value is allowed to change while the program is being executed.
CONSTANT
A name of a
memory location whose value is NOT allowed to change while the program
is being executed.
EXPRESSION
A combination
of operators, constants, variables, parentheses which has a value.
Expression: Value:
A+2
8
B-A/2
7
(B-A)/2
2
Condition: Value:
86 = 90 - 4
true
A < B
true
(B/2) > (A+1)
false
In the examples below, condition (bold-face), will represent a single instance of some condition; variable will represent a single instance of some variable; etc.
format: Assign to variable the value of expression or variable expression
meaning: a. Evaluate the expression
on the right
b. Store that value (in memory!) in the variable named on the left.
ex: newSalary <-- oldSalary + 3000
Before:
meaning: From input, get the (next) value (in the READ instruction)
and store it in memory in the location
named variable. Then continue for each of the remaining variables (if any)
in the list.
Ex: Write out "x has the value " , x
Memory:
Output:
x 876
x has the value 876
Each high level language has at least one looping instruction available to its programmers. C++ has several looping instructions.
The WHILE LOOP and the FOR loop are commonly available looping instructions. In the examples below, condition represents a condition which must evaluate to true; fundinstrepresents a fundamental instruction of some kind.
For readability, the instructions in the loop body should always be indented.
WHILE LOOP's general format:
While condition
fundinst
fundinst
...
fundinst
Where the loop "body" contains one or more fundamental instructions.
meaning:
Evaluate the condition. If TRUE, execute the loop instructions; otherwise,
fall out of the loop. Continue this evaluate-execute sequence until the
condition evaluates to FALSE.
The loop body will not be executed at all if the condition is initially false.
amountOwed <-- 400
while (amountOwed > 0)
amountOwed <--
amountOwed - 100
write out amountOwed
Trace of the algorithm:
Memory:
amountOwed (garbage) 400
300 200 100 0
Trace:
Memory:
counter
(garbage) 1 2 3 4
number
(garbage) 10 20 40 80 160
. Some compilers will leave the value of counter as 4 instead of 5. This is system dependent. Thus, when a FOR loop has finished execution, do not try to use the value of the for loop variable in a meaningful way. The FOR loop variable's sole purpose is to act as a counter during the FOR loop execution only.
. C++ has a very powerful for loop which allows you to specify more than one loop variable; more than one condition for execution; more than one instruction for modifying the loop variables. However, for this course, the basic for loop model shown above will satisfy your needs just fine.
The "if" instruction
meaning: If condition has the value TRUE, each instruction in the if clause (ie here, indented under the "if") is executed once. Otherwise, those instructions are not executed at all.
Ex: if (balance 0) write out "Yippee! No more payments!"
Trace:
Memory:
balance 250
meaning: If condition has the value TRUE, each instruction in the IF clause is executed once. Otherwise, each instruction in the ELSE clause is executed once.
note: The IF clause might have 1 or more instructions; the ELSE clause likewise.
Ex: read in salary
if (salary
< 25000)
taxRate <-- 0.20
else
taxRate 0.25
Trace!
Input:
32000