Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re^2: Looking for a loop

by jeffenstein (Friar)
on May 28, 2018 at 16:31 UTC ( #1215333=note: print w/replies, xml ) Need Help??


in reply to Re: Looking for a loop
in thread Looking for a loop

Translated to perl:

#!/usr/bin/env perl use strict; use warnings; use utf8; use feature 'say'; sub choose { say "choose a number:"; say for 1..5; } my ($correct, $response); while(!$correct){ choose(); chomp($response = <>); if($response >= 1 && $response <= 5){ $correct = 1; } else{ say "You have chosen the wrong number"; } }

But why not get rid of $correct, since you can just exit the loop?

#!/usr/bin/env perl use strict; use warnings; use utf8; use feature 'say'; sub choose { say "choose a number:"; say for 1..5; } my $response; while(1){ choose(); chomp($response = <>); last if $response >= 1 && $response <= 5; say "You have chosen the wrong number"; }

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1215333]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (7)
As of 2020-11-24 21:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?