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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

exit() function 1

Status
Not open for further replies.

thendal

Programmer
Aug 23, 2000
284
What is the command to break out of the present if statement or for loop, etc. I've tried 'exit' but it exits the whole program.

 
You can use something like a next if statement.

The syntax would be

[tt]next if (some statement) {
# what ever you want to happen

}

[/tt]

So say if you have a for loop that says:

[tt]for ($i=0;$i<40;$i++) {

# do something
# but say that if you need to exit before it gets to 39, then you write:

next if ($i=20) {

&somesub;

}

}

[/tt] so it goes to somesub if $i equals 20.

Hope this helps.

-Vic
 
works like this:
[tt]
while(1){ # loop forever in other words
$i ++;
next if $i < 3; # jumps back to 'while',
last if $i == 3; # jumps out of 'while' loop
}
[/tt]

would work the same without the 'next if' line -- just wanted to show what it does
 
Thank you very much mike and vik.It worked fine.
 
hello vikter,
here is some loop control constructs which will be very useful to u
last - stop the execution of the loop,and continues with
the next statement after the loop (like break in C)
next - Stops the current iteration of the loop , goes back
to the top and starts continues with the next value
of the loop index variable( in for loop).
it is very useful method (like continue in C )
redo - Stops the current execution and goes to the top and
&quot;restart&quot; the loop revaluating the test .
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top