Programming Fundamentals/Loops/Ruby: Difference between revisions

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

Revision as of 04:37, 8 February 2017

loops.rs

# 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.

def while_loop()
    puts "F°    C°"
    f = 0.0
    while f <= 100
        c = (f - 32) * 5 / 9
        puts (f).to_s + " = " + (c).to_s
        f += 10
    end
end

def for_loop()
    puts "F°    C°"
    for f in (0.0..100).step(10)
        c = (f - 32) * 5 / 9
        puts (f).to_s + " = " + (c).to_s
    end
end

def do_loop()
    puts "F°    C°"
    f = 0.0
    begin
        c = (f - 32) * 5 / 9
        puts (f).to_s + " = " + (c).to_s
        f += 10
    end while (f <= 100)
end

def 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 Ruby compiler / interpreter / IDE.

See Also