Programming Fundamentals/Loops/Perl: Difference between revisions

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

Revision as of 04:16, 8 February 2017

loops.pl

#!/usr/bin/perl

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

sub while_loop 
{
    my $f;
    my $c;
    
    $f = 0;
    print "F°    C°", "\n";
    while ($f <= 100) 
    {
        $c = ($f - 32) * 5 / 9;
        print $f . " = " . $c, "\n";
        $f += 10;
    }
}

sub for_loop
{
    my $f;
    my $c;
    
    print "F°    C°", "\n";
    for ($f = 0 ; $f <= 100 ; $f += 10) 
    {
        $c = ($f - 32) * 5 / 9;
        print $f . " = " . $c, "\n";
    }
}

sub do_loop 
{
    my $f;
    my $c;
    
    print "F°    C°", "\n";
    $f = 0;
    do 
    {
        $c = ($f - 32) * 5 / 9;
        print $f . " = " . $c, "\n";
        $f += 10;
    } while $f <= 100;
}

sub main
{
    while_loop();
    for_loop();
    do_loop();
}

main();

Try It

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

See Also