1 / 18

Programming

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.

otto-cote
Download Presentation

Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Programming Iterations (Loops statements)

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

  3. Two Types of Loop statements • 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.

  4. Definite Loops - The For Loop • Syntax: • For controlvariable = 1 to N do • Statement(s) • endfor • E.g. • For gender=“male” do • NAF = AF – GenderDiscount • endfor

  5. Indefinite Loop – The While Loop • Syntax: • While <condition> do • Statement(s) • Endwhile • E.g. • While gender <>“” do • NAF = AF – GenderDiscount • endwhile

  6. Anatomy of a loop • Every loop has the following elements: • Initialization • Repetitive statement • Loops statements (block) • Conclusion

  7. Element 1 - Initialization • 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.

  8. Element 2 – Repetitive Statements • 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.

  9. Element 3 – Loop statements (block) • Specify what statements are to be repeated.

  10. Element 4 - Conclusion • When a loop is over we may need to perform some tasks. • E.g. Print the results of a calculation.

  11. Accumulator • 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.

  12. Counters • 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

  13. Questions to ask when constructing a solution that contains a loop. • 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

  14. 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. • 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.

  15. 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. • 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.

  16. 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. • 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.

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

  18. Activities • 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.

More Related