Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Decision
There are two decision / selection constructs: (i) IF THEN and (ii) IF ... THEN ... ELSE
(i) The IF ... THEN construct syntax:
IF (logical expression) THEN
{Statements}
Execute if expression is TRUE
This construct is used when no actions need to be performed if the expression is false.
The IF ... THEN ... ELSE construct syntax:
IF (logical expression) THEN
{Statements} executed if expression is TRUE
ELSE
{Statements} executed if expression is FALSE
END IF
This syntax uses the BLOCK IF construct and determines the execution of statements if the
expression is proven true of false. If true, then the block of statements following the IF will be
executed. If false, then the statements following the ELSE will be executed.
In our example algorithm Unsatisfactory will be printed if the expression is proven true i.e. if
the average is less than 60. If its proven to be false i.e. if the average is 60 or more, then the
ELSE statement will be executed Well done will be printed.
Relational Operators
Logical expressions include the use of comparisons using several relational symbols:
> means greater than
< means less than
In our IF . .THEN and IF ... THEN .. ELSE,
= means equal to
constructs described above, the logical
>= means greater than or equal to
expressions are actually comparisons
<= means less than or equal to
utilizing relational operators
<> means not equal to
e.g. IF age >= 18 THEN; IF cost <300.00 THEN; IF Name <> Andrew THEN
{statements}
{statements}
{statements}
e.g. Problem 1: Accept five scores obtained by a student in examinations attempted. Find and
print the average score. Should this score be less than 60 then Unsatisfactory should be
printed. Otherwise, Well done should be printed.
Algorithm:
Total = 0
Count = 0
FOR X = 1 to 5 DO
Enter marks obtained in subject # X; Mark
Sum = Sum + Mark
Count = Count + 1
END FOR
Average = Sum/Count
IF Average < 60 THEN
PRINT Average, Unsatisfactory
ELSE
PRINT Average, Well done
END IF
STOP
Nested If
The Nested If construct consists of IF.. THEN.. ELSE constructs nested inside each other:
IF (logical expression (i))THEN
IF (logical expression (ii)) THEN
IF (logical expression (n)) THEN
{ statement (if expression n is true))
ELSE
{ statement (if expression ii is true)}
ELSE
{statement (if expression i is true)}
ELSE
{statement (if all expressions are false)}
This construct is ideal when several numeric comparisons are to be made, requiring several
unique decision /selection statements of action.
e.g. Problem 2: A research laboratory recently discovered that the incubation temperature in
degrees Fahrenheit) of the eggs determined the color of the feathers of hatched chickens:
45 - 50 - yellow, 40 - 44 - white, 35 -39 - brown, 30 - 34- Beige, Under 30- Black.
Write an algorithm to accept the incubation temperature and print the color of the chickens
hatched.
Algorithm:
INPUT Temperature
IF Temperature> 29 THEN
IF Temperature> 34 THEN
IF Temperature> 39 THEN
IF Temperature> 44 THEN
PRINT Yellow Chicks
ELSE
PRINT White Chicks
ELSE
PRINT Brown Chicks
ELSE
PRINT Beige Chicks
ELSE
PRINT Black Chicks
STOP
The statements are repeated as long as the value of the identifier specified by the <counter>
element does not exceed the value specified by <end>. A comparison is made each time the loop
iterates to check whether the <end> is exceeded. If it is not then the counter value is incremented
by one (the default step value) or by the value specified in STEP.
The FOR loop is used when the required number of iterations (loops) is known beforehand. In
example problem 1 the FOR loop is used because the problem stated the number of marks to be
entered. Thus the <end> value was set to 5. It looped five times in accepting five marks obtained
by the student. The STEP statement was not used and so the default value of 1 is in effect.
E.g. Problem 4: The manager of a department store requests a program which automates cashier
service at a check-out counter. A customer may purchase several items. The total cost of
purchases should be calculated and printed after allowing for a 12% discount for items priced
under $200. Other items attract a 15% discount. The algorithm should also print the number of
items purchased. The item entry is terminated by the sentinel/dummy value 999.
Solution:
TotalCost = 0
NumItems =0
Input cost of item, Cost
WHILE Cost <>999 Do
IF Cost <200 THEN
Discount = Cost *.12
NewCost = Cost - Discount
TotalCost = TotalCost + NewCost
Num = Numltems + 1
ELSE
Discount = Cost * .15
NewCost = Cost - Discount
TotalCost = TotalCost + NewCost
Num = Numltems + 1
END IF
Input cost of item, Cost
END WHILE
PRINT Numltems, TotalCost
STOP
Amend the algorithm above to print the total cost of the items before discount, the total discount
and the net discounted cost. Accept a value as payment from the customer. Print this value as
well as the amount of change the customer is entitled to.
..
.
..
.
..
.
..
..
..
..
.
..
..
..
.
.
.....
.
.
.
..
ALGORITHM PROBLEMS
Algorithms
1. Write an algorithm to convert pounds
to Kilograms. (1 Kg = 2.2 lb.) The
algorithm must cater for several
conversions. Assume that at least
one conversion must be done and
the program must be terminated if the
total number of kilograms reaches or
exceeds 1000.
.....
.
.
.
.
....
...
...
..
4. Accept the names and scores of eleven
batsmen on a cricket team.
Determine and print the names
of the batsman who scored the
most runs as well as the one who
scored the least.
..
..
..
..
..
..
..
..
..
e.g. Problem 5: Accept as input the value of sales reported by fifteen salesmen. Any salesman
repol sates in excess of 10,000 should receive a commission of 10% of his sales. Calculate and
print the sales along with the total amount of commission paid.
Algorithm:
TotalSales =0
TotalComm =0
FORX=1 to l5 Do
Enter name of salesman, Name
Enter value of sales, Sales
TotalSales = TotalSales + Sales
IF Sales> 10,000 THEN
Comm = Sales* .10
PRINT Name, Commission
TotalComm = TotalComm + Comm
END IF
END FOR
PRINT TotalSales, TotalComm
STOP
e.g. Problem 6: Write an algorithm to determine a batsmans batting statistics over the last
season. Algorithm must accept the score made each innings. The average score must be
determined and print (total score / innings played). Additionally, the number of 100s scored as
well as his highest score should be printed. The scores made should be terminated with entry of
rogue value 999.
Algorithm:
Total_Score = 0
Total Innings =0
Highest = 0
Century =0
Enter batsmans score, Runs
WHILE Runs <> 999 Do
Total_Score = TotalScore + Runs
..
..
..
..
..
..
..
..
..
..
ENDIF
..
IF Runs = 100 THEN
..
Century = Century + 1
..
END IF
..
Enter batmans score, Runs
..
END WHILE
..
Average = Total_Score / Total_Innings
..
PRINT Total_Score, Average, Highest, Century
..
STOP
..
Modify the above algorithm to determine and print the number of zeros scored, and the
number of fifties (scores between 50 and 99).