Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inserting 'continue' statement within a function to be called later in another functions 'For loop' statement

To simplify my code, I have made a function that contains an 'if' statement. The function is meant to be called within a 'For' loop in another function. I want to put a 'continue' statement inside the custom function to be called later. Is there any possible way to do this without error? Code is written in Google Apps Script.

function example1() {
  //code here
  continue;
  }
}
        
function example2() { 
  for(var x = 1 ; x <= 2 ; x++) {
  example1();
  }
}

I get an error for calling the 'continue' statement within the example1() function because the 'continue' statement is not in a 'For' loop. But the example1() function is only created to be called within the example2() 'For' loop statement.

like image 793
Braydon Avatar asked Oct 22 '25 04:10

Braydon


1 Answers

Nope, you have to directly continue inside of the for loop. You could make your function return a boolean then perform a continue as needed.

function example1() {
  return shouldLoopContinue; // Add your own logic here
}

function example2() {
  for (var x = 1; x <= 2; x++) {
    if (example1()) {
      continue;
    }

    doSomeOtherWork();
  }
}
like image 142
Mike Cluck Avatar answered Oct 25 '25 07:10

Mike Cluck