Pooh program example 02-loop.p

Test 02-loop.p

Source of programm

# loops: function computes the sum of squares over integer range

sub squares( from, to)
  i = from
  res = 0
  while i < to
    res = res + i * i
    i = i + 1
  end
  return res
end

println( ~msg 'the sum of numbers is ' .. squares( ~from 1 ~to 7 ) )

Standard output for 02-loop.p

the sum of numbers is 91

Trace output for 02-loop.p

013|println( ~msg 'the sum of numbers is ' .. squares( ~from 1 ~to 7 )...
004| i = from:1
005| res = 0
006| while (i:1 < to:7):true
007|  res = (res:0 + (i:1 * i:1):1):1
008|  i = (i:1 + 1):2
009| end
006| while (i:2 < to:7):true
007|  res = (res:1 + (i:2 * i:2):4):5
008|  i = (i:2 + 1):3
009| end
006| while (i:3 < to:7):true
007|  res = (res:5 + (i:3 * i:3):9):14
008|  i = (i:3 + 1):4
009| end
006| while (i:4 < to:7):true
007|  res = (res:14 + (i:4 * i:4):16):30
008|  i = (i:4 + 1):5
009| end
006| while (i:5 < to:7):true
007|  res = (res:30 + (i:5 * i:5):25):55
008|  i = (i:5 + 1):6
009| end
006| while (i:6 < to:7):true
007|  res = (res:55 + (i:6 * i:6):36):91
008|  i = (i:6 + 1):7
009| end
006| while (i:7 < to:7):false
009| end # finish loop
010| return res:91
013|println( ~msg 'the sum of numbers is ' .. squares( ~from 1 ~to 7 ):91 )...