Programming Fundamentals/Loops/Lua: Difference between revisions

From Wikiversity
Jump to navigation Jump to search
Content deleted Content added
Creating
(No difference)

Revision as of 04:08, 8 February 2017

loops.lua

-- This program displays a temperature conversion table showing Fahrenheit
-- temperatures from 0 to 100, in increments of 10, and the corresponding 
-- Celsius temperatures using While, For, and Do loops.
 
function while_loop()
    local f
    local c
 
    io.write("F°    C°", "\n")
    f = 0
    while f <= 100 do
        c = (f - 32) * 5 / 9
        io.write(f, " = ", c, "\n")
        f = f + 10
    end
end
 
function for_loop()
    local f
    local c
 
    io.write("F°    C°", "\n")
    for f = 0, 100, 10 do
        c = (f - 32) * 5 / 9
        io.write(f, " = ", c, "\n")
    end
end
 
function do_loop()
    local f
    local c
 
    io.write("F°    C°", "\n")
    f = 0
    repeat
        c = (f - 32) * 5 / 9
        io.write(f, " = ", c, "\n")
        f = f + 10
    until not (f <= 100)
end
 
function main()
    while_loop()
    for_loop()
    do_loop()
end
 
main()

Try It

Copy and paste the code above into one of the following free online development environments or use your own Lua compiler / interpreter / IDE.

See Also