4

I have a very simple For Loop in Google Apps Script to check some conditions in a Google Sheet. What I want is to add another condition, if it is met, then I want to skip current iteration and move on to next. This is pretty easy in VBA, but I am not sure how to do it on JavaScript.

Current code:

for (var i=1 ; i<=LR ; i++)
    {
     if (Val4 == "Yes")
      {
       // Skip current iteration...   <-- This is the bit I am not sure how to do
      }
     elseif (Val1 == "Accepted" && !(Val2 == "") && !(Val3 == ""))
      {
        // Do something..
       }
      else
      {
       // Do something else...
      }

    }
4
  • 1
    Read about loop control statements in JavaScript. Namely, break and continue. Note that if your for loop has only a single logical statement (your if-else if chain), then simply adding that check and using an empty conditional body will suffice. Commented Sep 20, 2018 at 12:58
  • 1
    you probably mean continue statement w3schools.com/js/js_break.asp. If not, then showing the VBA alternative should make the question clear Commented Sep 20, 2018 at 12:58
  • @Slai - thanks for that. Really useful. Please put that in answer and I will mark as correct. Commented Sep 20, 2018 at 13:10
  • This question was solved with a minimum of research. An internet search with generic keywords should have led you to the answer - part of asking a good question. Did you do any research before asking? Commented Sep 20, 2018 at 13:29

1 Answer 1

12

continue statement can be used to continue to next itteration :

for (var i=1 ; i<=LR ; i++)
{
  if (Val4 == "Yes")
  {
    continue; // Skip current iteration... 
  }
  // Do something else...
}

In your sample case, leaving the if block empty will achieve the same result:

for (var i=1; i <= LR; i++)
{
  if (Val4 == "Yes")
  {

  }
  elseif (Val1 == "Accepted" && !(Val2 == "") && !(Val3 == ""))
  {
    // Do something..
  }
  else
  {
    // Do something else...
  }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.