Search code examples
loopsturing-lang

Counted loops in turing language


% calculates the population of a city from year 2000 to 2020

var popstart : int := 80000
var popgrowth : real
var popend : real
var growthrate : real := 0.03

% popgrowth := popstart * growthrate
    for i : 2000..2020 by 1
popgrowth := popstart * growthrate
end for

put "year  population"
put "====  =========="
put  i, "  ", popgrowth

when I run the program, i get the error variable "i has not been declared" when I declare i as a variable, I get the error "i has already been declared"

the output should look something like this:

year population
==== ==========
2000 xxxxxxxxxx ~ 2020 XXXXXXXXXX

here's a similar, but much simpler program, where i was successful in doing what i'm attempting to do in the program above.

for i : 4 .. 19 by 3
    put i
end for

HELP PLEASE! and thanks in advance!


Solution

  • I don't know much about Turing, but I suspect that the for i... is an implicit declaration of i.

    So, I don't know how you could fix it, but you could get around it by doing this:

    var last_year: int
    for i : 2000..2020 by 1
        popgrowth := popstart * growthrate
        last_year = i
    end for
    
    put "year  population"
    put "====  =========="
    put  last_year, "  ", popgrowth