Programming Notes
Programming Notes
Problem: Write an algorithm an pseudocode which prompts a user to enter the price of an
item and then calculate and print the new price after a discount of 12% is given.
Algorithm solution.
1. START
2. PROMPT USER FOR ITEM PRICE
3. CALCULATE THE DISCOUNT AT 12%
4. CALCULATE THE NEW PRICE
5. PRINT THE NEW PRICE
6. STOP
Pseudocode solution
BEGIN
INPUT PRICE
DISCOUNT = PRICE * 0.12
LET NEW PRICE = PRICE – DISCOUNT
PRINT NEWPRICE
END
Structured programming
The order in which instruction are performed by the computer must be carefully
controlled and programming languages normally contain features that allow the order of
instruction execution to be controlled. All programming problems, no matter how
complex, can be reduced to combinations of controlled sequences, selections, or
repetitions (iterations) of basic operations on data.
The concept of structured programming uses three basic program control structures to
design the solution to programming problems.
Control structures
1. Sequence: The program statements are executed in the order or sequence in which
they appear in the program.
e.g
Statement 1
Statement 2
Statement 3
etc.
One sub-problem is solved at a time, until the complete problem is solved.
2. Selection: selection forms part of the decision-making facilities within a
programming language. They allow alternative actions to be taken according to
the conditions which exist at particular stages in program execution. Conditions
are normally if the form of expressions, which when evaluated, give a Boolean
result. Examples of selection constructs are: (a) IF.. THEN..ELSE, and
SELECT…CASE.
3. Repetition (Iteration): There are many programming problems in which the same
sequence of statements need to be performed again and again for some definite or
Programming Notes
NO /FALSE
Rhombus: Used to specify a condition. (decision
box)
YES /TRUE
Off-Page connector
IF condition THEN
Statement 1
Statement 2
Statement n
ELSE
Statements
ENDIF
Start
Condition
?
TRUE FALSE
Statement
after
ENDIF
Condition
True False
First Second
First Statement
Statement Statement
Set Set
7. The word DO
8. One or more statements to be executed each time through the loop
9. The word ENDFOR indicating the end of the construct.
NOTE: The part of the construct between the words FOR and DO is called the control
part. This is what determines how many times the loop will be executed. The loop
variable is also called the control variable.
Enter FOR
CRTL_V = Expression
CTRL_V=CTRL_V+1
CTRL_V
>End
Value Statement (Body)
?
ENDFOR