Programming Iterations Loops statements What is an Iteration

  • Slides: 18
Download presentation
Programming Iterations (Loops statements)

Programming Iterations (Loops statements)

What is an Iteration � Also known as a loop statement, is a conditional

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 � Definite – facilitates a block of instructions being

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.

Definite Loops - The For Loop � Syntax: � For controlvariable = 1 to

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

Indefinite Loop – The While Loop � Syntax: � While <condition> do ◦ Statement(s)

Indefinite Loop – The While Loop � Syntax: � While <condition> do ◦ Statement(s) � Endwhile � E. g. � While gender <>“” do ◦ NAF = AF – Gender. Discount � endwhile

Anatomy of a loop � Every ◦ ◦ loop has the following elements: Initialization

Anatomy of a loop � Every ◦ ◦ loop has the following elements: Initialization Repetitive statement Loops statements (block) Conclusion

Element 1 - Initialization � Before a loop is started we may need some

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.

Element 2 – Repetitive Statements � The while - endwhile statement or the for

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.

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

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

Element 4 - Conclusion � When a loop is over we may need to

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

Accumulator � An important principle in pseudocode development. � Start with a value of

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.

Counters � Counting the number of times a value is encountered or a statement

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

Questions to ask when constructing a solution that contains a loop. 1. What type

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

3. 4. Do I need to count anything? If yes how many? ◦ For

3. 4. 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.

5. 6. Do I need to count anything? If yes how many? ◦ For

5. 6. 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.

7. Is there any condition(s) affected by the questions? ◦ If Yes for each

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

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

Activities � 1. Write a pseudocode algorithm to read a sequence of numbers terminated

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.