rse2 has asked for the wisdom of the Perl Monks concerning the following question:

Hey there! i have to make a program of mine loop.

Do you have any ideas on the condition in the loop i can put?

by the way i have to make this program loop.

#! /usr/bin/perl -w print "Type in a number: "; $x = <>; print "Type in another number: "; $y = <>; print "What would you like done?\n\nYour choice is:\n1) Add\n2) Subtra +ct\n3) Multiply\n4) Divide\n"; $ask = <>; chomp ($ask); if ($ask eq "Add") { print $x + $y,"\n";} if ($ask eq "Subtract") { print $x - $y,"\n";} if ($ask eq "Divide") { print $x / $y, "\n";} if ($ask eq "Multiply") { print $x * $y, "\n";}

Replies are listed 'Best First'.
Re: any loop ideas?
by moritz (Cardinal) on Aug 18, 2011 at 13:25 UTC
    Do you have any ideas on the condition in the loop i can put?

    Well, anything you like, really. You can make it loop 3 times, or infinitely (until the user aborts by pressing Ctrl+C), or until STDIN/<> has EOF, or until the user enters "get me out of here", or...

    You must decide what the program should do, we can't do that for you.

Re: any loop ideas?
by blue_cowdawg (Monsignor) on Aug 18, 2011 at 13:50 UTC
        Do you have any ideas on the condition in the loop i can put?

    There are many ways to be loopy (in Perl) and it just boils down to what you are trying to accomplish. As I tell my students, first identify the problem and tailor the solution to fit.

    Here are a few examples:

    # loop until I get a null input while(1){ print "Type in a nubmer: "; my $x=<>; exit 0 unless $x # rest of the code here }
    #this also loops until a null happens. for(;1;){ # see above for rest of approach
    # Iterate 10 times and quit for(0..9) { # rest of your code }

    So there ya have it... a few examples of how it is done. You should read up on flow control in Perl in a handy dandy How To Program in Perl type of book grasshopper.


    Peter L. Berghold -- Unix Professional
    Peter -at- Berghold -dot- Net; AOL IM redcowdawg Yahoo IM: blue_cowdawg
Re: any loop ideas?
by planetscape (Chancellor) on Aug 18, 2011 at 21:23 UTC
Re: any loop ideas?
by raybies (Chaplain) on Aug 18, 2011 at 16:27 UTC
    You'd have an idea on how to loop, if you'd paid attention in class instead of playing Angry Birds on your smartphone.