成人快手

Representing iteration

There are two ways of representing :

  • a

Representing iteration in pseudocode

Writing in pseudocode is rather like writing in a . Each step of the algorithm is written on a line of its own, in sequence.

Consider this simple six-step algorithm for cleaning your teeth:

  1. set number_of_teeth_cleaned to 0
  2. put toothpaste on toothbrush
  3. use toothbrush to clean a tooth
  4. increase number_of_teeth_cleaned by 1
  5. if number_of_teeth_cleaned < 32 then go back to step 3
  6. rinse toothbrush

With each iteration, a decision needs to be made as to whether to continue iterating or not. Decisions are represented as selection.

In pseudocode, the algorithm would look like this:

number_of_teeth_cleaned = 0
put toothpaste on toothbrush
REPEAT
	procedure clean_tooth
	number_of_teeth_cleaned = number_of_teeth_cleaned + 1
UNTIL number_of_teeth_cleaned = 32
rinse toothbrush

Representing iteration in a flowchart

A flowchart uses a diagram to explain the steps of an algorithm.

In a flowchart, the algorithm would look like this:

In a tooth brushing flowchart the instructions will continue until the number of teeth cleaned is equal to 32. If the answer is yes, then stop. If the answer is no the process will continue.