Programming - Bellefield High School

Download Report

Transcript Programming - Bellefield High School

Iterations
(Loops statements)

Also known as a loop statement, is a
conditional statement that requires a block of
statements to be repeated either for a
specific number of times or a condition is
met.


Definite – facilitates a block of instructions
being repeated for a specific number of
times.
Indefinite – facilitates a block of instructions
being repeated until a particular condition
has been met.


Syntax:
For controlvariable = 1 to N do
◦ Statement(s)



endfor
E.g.
For gender=“male” do
◦ NAF = AF – GenderDiscount

endfor


Syntax:
While <condition> do
◦ Statement(s)



Endwhile
E.g.
While gender <>“” do
◦ NAF = AF – GenderDiscount

endwhile

Every loop has the following elements:
◦
◦
◦
◦
Initialization
Repetitive statement
Loops statements (block)
Conclusion


Before a loop is started we may need some
statements to get started.
i.e. a variable may need to be initialized to a
start value or an initial value read into a
variable.

The while - endwhile statement or the for –
endfor statement specifying that the
sequences of instructions must be carried out
until the condition is met or a definite
number of times.

Specify what statements are to be repeated.


When a loop is over we may need to perform
some tasks.
E.g. Print the results of a calculation.





An important principle in pseudocode
development.
Start with a value of 0 and each time you are
given a number you add it to your present
sum.
Syntax:
Sum = Sum + New_number
Each time a new number is given (inputted) it
is added to sum.






Counting the number of times a value is
encountered or a statement is carried out, is
another important concept in pseudocode
development.
Syntax:
Counter = Counter + N
E.g.
Counter = Counter + 1
Counter = Counter + 5
1.
2.
What type of loop do I need? Definite or
Indefinite?
Does the question require any input? Yes or
No?
◦ If Yes then for a For Loop your first input statement
must take place somewhere immediately following
the beginning of the loop.
◦ If No then for a While Loop you will have to use an
input statement:
 Before the beginning of the lop
 Towards the end of the loop
3.
Do I need to count anything? If yes how many?
◦ For each item to be counted you need to initialize each
counter variable before the start of the loop and you need
to put each counter construct inside the loop.
4.
Do I need to sum or accumulate any value? Do I
need a product, average etc. If yes how many?
◦ For each value to be accumulated you need to initialize an
accumulator to 0 outside the loop and you need to place
the accumulator construct inside the loop.
5.
Do I need to count anything? If yes how many?
◦ For each item to be counted you need to initialize each
counter variable before the start of the loop and you need
to put each counter construct inside the loop.
6.
Do I need to sum or accumulate any value? Do I
need a product, average etc. If yes how many?
◦ For each value to be accumulated you need to initialize an
accumulator to 0 outside the loop and you need to place
the accumulator construct inside the loop.
7.
Is there any condition(s) affected by the
questions?
◦ If Yes for each counter, accumulator or print
statement within loop block , see under which
condition it does that.
8.
Do I need to print anything?
◦ If Yes where do I put my Print statement? Inside the
loop or outside the loop? A print statement placed
inside a loop will be executed (carried out) each
time the loop block is repeated.

What action must I perform when the loop
terminates?
◦ You may need to calculate an average, a difference
or a print value.

After each question above is answered you
make the necessary change to the
pseudocode. The processing is very similar
to baking a cake or building a house.

1. Write a pseudocode algorithm to read a
sequence of numbers terminated by 999. The
pseudocode should count and print the
number of negative values and zero values.
The algorithm should also print the sum of
their positive numbers.