For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > May 2006 > Re: Simple loop question [SOLVED]









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Re: Simple loop question [SOLVED]
Danny

2006-05-31, 7:00 pm

Hi list,

Thank you all who responded to my question and in particular Dr MindHacker. After I saw the
corrected code, I realized how I am supposed to "think" in perl.

Thank you very much.

Danny

>
> Danny,
> The code is below, I ran a simple test and
> it worked. The "Please choose ..." part should
> be at top because that will always execute so
> long as the final elseif path is not executed...
> Also, the other $guess = <STDIN> were simply
> redundant after this fix so I removed them.
>
> Dr MindHacker
>
> #!/usr/bin/perl -w
>
> $upper = 20 ;
> $lower = 1 ;
> $target = 11 ;
>
> while ( )
> {
> print "Please choose a number between ${lower} and
> ${upper}\n" ;
> $guess = <STDIN>;
> if ( $guess > $target )
> { print "Too high\n" ; }
> elsif ( $guess < $target )
> { print "Too low\n" ; }
> elsif ( $guess == $target )
> {
> print "You guessed right\n" ;
> exit ;
> }
> }
>
> Dr MindHacker

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com