Top Banner
Iterations (Loops statements)
18

Programming

Dec 31, 2015

Download

Documents

otto-cote

Programming. Iterations (Loops statements). What is an Iteration. 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. Two Types of Loop statements. - PowerPoint PPT Presentation
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Programming

Iterations(Loops statements)

Page 2: Programming

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.

Page 3: Programming

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.

Page 4: Programming

Syntax: For controlvariable = 1 to N do

◦ Statement(s) endfor

E.g. For gender=“male” do

◦ NAF = AF – GenderDiscount endfor

Page 5: Programming

Syntax: While <condition> do

◦ Statement(s) Endwhile

E.g. While gender <>“” do

◦ NAF = AF – GenderDiscount endwhile

Page 6: Programming

Every loop has the following elements:

◦ Initialization◦ Repetitive statement◦ Loops statements (block)◦ Conclusion

Page 7: Programming

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.

Page 8: Programming

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.

Page 9: Programming

Specify what statements are to be repeated.

Page 10: Programming

When a loop is over we may need to perform some tasks.

E.g. Print the results of a calculation.

Page 11: Programming

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.

Page 12: Programming

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

Page 13: Programming

1. What type of loop do I need? Definite or Indefinite?

2. 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

Page 14: Programming

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.

Page 15: Programming

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.

Page 16: Programming

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.

Page 17: Programming

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.

Page 18: Programming

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.