Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

skip current iteration

Tags:

loops

php

I have a php array $numbers = array(1,2,3,4,5,6,7,8,9)

if I am looping over it using a foreach foreach($numbers as $number)

and have an if statement if($number == 4)

what would the line of code be after that that would skip anything after that line and start the loop at 5? break, return, exit?

like image 750
Hailwood Avatar asked Feb 17 '11 00:02

Hailwood


People also ask

What skip the current iteration of a loop?

Use the JavaScript continue statement to skip the current iteration of a loop and continue the next one.

Which keyword is used to skip the current iteration?

The continue keyword is used to end the current iteration in a for loop (or a while loop), and continues to the next iteration.

How do I skip the next iteration in Python?

Use the continue statement inside the loop to skip to the next iteration in Python. However, you can say it will skip the current iteration, not the next one.


1 Answers

You are looking for the continue statement. Also useful is break which will exit the loop completely. Both statements work with all variations of loop, ie. for, foreach and while.

$numbers = array( 1, 2, 3, 4, 5, 6, 7, 8, 9 ); foreach( $numbers as $number ) {     if ( $number == 4 ) { continue; }     // ... snip } 
like image 196
Matthew Scharley Avatar answered Sep 18 '22 17:09

Matthew Scharley