Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

if .. elsif .. else

Status
Not open for further replies.

inforeqd

Technical User
Joined
Jan 8, 2001
Messages
95
Location
US
I have a script that uses if elsif else repeatedly.

Basically its like this

if ($answer eq "y") {
do something here;
}
elsif ($answer eq "n") {
print "bye \n";
exit;
}
else {
print "skipping\n";
}

I want a user to answer the question and if yes do some code. If no exit and any other answer skip to the next itereation of an if elsif else.

Is there an easier way? How can I exit and kill the entire script if chosen "n". The way it is now if a user chooses n then it just skips. I think its something to do with my looping but I'm just lost.

TIA
Info
 
i would put this in a subroutine and then if the user chooses 'n' then use the 'exit' command. HTH.

Mike Mike
~~~~
simanek@uiuc.edu
"It's a Swingline!"
~~~~
 
I thought about a sub routine that covers the
exit. Something like this

sub exit {
chomp($answer);
if ($answer eq "n") {
print "Bye \n";
exit;
}
}

however how do I put the exit() into the if elsif else???

 
make the WHOLE thing into a subroutine... i.e.

sub your_function{
if ($answer eq "y") {
do something here;
}
elsif ($answer eq "n") {
print "bye \n";
exit();
}
else {
print "skipping\n";
}
}
This should force the exit command to exit out of the subroutine and then execute the next line after the subroutine call in your main function...if i'm understanding your question correctly. So put all of your if's elsif's and else's in the function and once the interpreter sees 'exit' it should jump past everything else in that subroutine. Mike
~~~~
simanek@uiuc.edu
"It's a Swingline!"
~~~~
 
Mike,

Thanks for the help. that sould do it

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top