Quick Index
Purpose


In this example we'll demonstrate how to write an algorithm for the common problem of iteration over a collection by index.

Writing Algorithm


printAll(elements)
Problem Statement: Write an algorithm to print a collection of elements using the "toString" method for each element.

Given: Variable 'elements' that holds a collection of objects.
Writing Algorithm in Human Language
Here is algorithm written in human language (English).

This approach is often a good way to start working on an algorithm.
Human Language:
printAll(elements) {
	Let 'i' equal to zero
	Let 'len' equal to length of 'elements'
	While 'i' is less than 'len'
		Let 'nextElement' equal to the element in 'elements' at index 'i'
		Call nextElement's toString method and print result
		Let 'i' equal to 'i' plus 1
}
Writing Algorithm in Pseudocode
Using the original problem statement combined with our human language algorithm, we have written the pseudocode here.

Here is our solution (pseudocode). We simply loop over the collection 'elements' and do a toString for each element.

Next we will examine the pseudocode in detail
Pseudocode:
function printAll(elements) {
	let len = elements.length
	let i = 0
	while (i < len) {
		let nextElem = elements[i]
		println(nextElement.toString())
		i = i + 1
	}
}
//try it


Pseudocode Breakdown


let len = elements.length
let i = 0

  • let len = elements.length -- says to let variablelen" have the length of "elements"
  • let i = 0 -- says to let variable "i" have value "0" (zero)

while (i < len) {

  • while -- says to do a while (true) loop
  • i < len -- says to keep looping while "i < len"
  • We could think of this statement generally as "while (CONDITION)"

while (i >= 0) {
	let nextElem = elements[i]
	println(nextElem.toString())
	i = i + 1
	}
 

  • {} -- The braces define the loop block. The block of code within the braces executes for every loop
  • let nextElem = elements[i] -- says to get the element at index "i" and assign to var "nextElem"
  • println(nextElem.toString()) -- says to println "nextElement" using toString()
  • i = i + 1 -- says to increment var "i"

Notes:
  • A loop variable is the same as a local variable (the local context is the "loop block").
  • If the loop block contains only one statement, the braces "{}" are optional.


Walking Through Algorithm


We're now going to walk through the algorithm using example data.

Given Data
Let us say that the provided array is a collection of student (objects) as follows:

elements = [Riya, Asha, Chin]
[Riya, Asha, Chin]
printAll(elements) {
Initialization
Before beginning the loop we initialize the loop variable and also the length variable "len".
    0
let i = 0
     3
let len = elements.length
Loop 1
The first loop.
Get the element at index "i" and assign to var "nextElem"
Print out nextElem
Lastly, increment "i"
       0    3
while (i < len) {
          Riya              0
    let nextElem = elements[i]
                  Riya
    println(nextElement.toString())
    1   0
    i = i + 1
}
Loop 2
Second loop.
Same loop logic except loop var "i" is "1",
and "nextElem" (from elements) is thus "Asha"
       1    3
while (i < len) {
          Asha              1
    let nextElem = elements[i]
                 Asha
    println(nextElement.toString())
    2   1
    i = i + 1
}
Loop 3
Third loop.
Same loop logic except loop var "i" is "2",
and "nextElem" (from elements) is thus "Chin"
       2    3
while (i < len) {
          Chin              2
    let nextElem = elements[i]
                 Chin
    println(nextElement.toString())
    3   2
    i = i + 1
}
LOOP ENDS
The condition "i < len" (3 < 3) is false, and thus the loop ends.
       3    3
while (i < len) {