0% found this document useful (0 votes)
18 views

Algorithms and Programs 3

Uploaded by

jude
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Algorithms and Programs 3

Uploaded by

jude
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

AS Computer Science

Algorithms and programs (3)

Key terms Nested Selection / If statements Sample AS exam question

Term Definition A nested “if” statement is the true condition in a Write an algorithm, using pseudo code or a high-
series of conditions in computer programming. It is level programming language, which will use
Programming The building blocks of a used when multiple responses are possible and the twelve monthly rainfall figures as input.
constructs computer program. The outcome for each response is different.
The program should output:
three basic programming Declare x is Integer
constructs are sequence, Declare y is Integer • the total rainfall for the year
selection and iteration. set x = 5 • the mean monthly rainfall for the year
set y = 20 • the months where the rainfall was above the
if x > y then mean.
Sequence A sequence construct tells
if x >= 10 then
the CPU which statement output “x value > = to 10” Your algorithm should use meaningful identifiers.
is to be executed next. By else
default, this is the statement output “x value < 10” [6]
following the current end If
statement, or first statement else Marking of Sample Exam Question
if y <= 20 then
in the program. • Declare array and initialise variables (1).
output “y value < = to 20”
else • Input loop structure + increment (1).
Selection Also called a decision. output “y value > 20” • Calculate mean (1).
In a selection statement, end If • Output Total and Mean (1).
a question is asked, and end If • Output loop structures (1).
depending on the answer, the • Detect and output above mean months (1).
In some languages, (e.g. Python) incorrect
program takes one of two
courses of action, after which
indentation will result in syntax errors. Indicative
Indicative response
response
the program moves on to the
next event. 1 declare Rainfall array (1..12) of
Count controlled loop integer
IF…/THEN…/ELSE… 2 set Total = 0
for i = 1 to len array [] 3
Iteration The repetition or looping of 4 for Count = 1 to 12
a block of instructions (code) output array [i,2] 5 input Rainfall (Count)
within a computer program, 6 set Total = Total +
next i
for a number of instances or Rainfall (Count)
until a specified condition is 7 end for
met. 8
Condition controlled loops 9 set Mean = Total / 12
Program Integer variable used within 11 output “Total = ”, Total
counts a loop that increments or Repeat loop will run at least once. 12 output “Mean = ”, Mean
decrements during each 13 output “Months above Mean = ”
iteration. Used to record and/ repeat 14 for Count = 1 to 12
or control the number of output “***” 15 if Rainfall (Count) > Mean
iterations executed. then
x = x + 1 16 output Count
Rogue values A special value, also called until x = 20 17 end for
a sentinel value, that is used
to terminate a loop. Typically While loop will not run if the condition is not true.
chosen so as to not be a
while x < 20
legitimate data value that
the loop will encounter and output “***”
attempt to process.
x = x + 1
repeat

You might also like