0

Am I correct in assuming that when the first if statement and nested if within the else statement both fail, I then go back up to the first for loop and increment i by 1?

So I can continue until j < totalCols fails even though neither the if or else statement are executing?

var rowCount = [];
for (var i = 0; i < totalRows; i++) {
   rowCount[i]="";
   spaceCount = 0;

   for (var j = 0; j < totalCols; j++) {
      if (puzzle[i][j] == "#") { // if this fails?
         spaceCount++;
         if (j == totalCols-1) rowCount[i] += spaceCount + "&nbsp;&nbsp;"; 
      } else {
         if (spaceCount > 0) { //and this fails?
            rowCount[i] += spaceCount + "&nbsp;&nbsp;";
            spaceCount = 0;
         } 
      }    
   }

}
1
  • So I can continue until j < totalCols fails even though neither the if or else statement are executing? Commented Oct 15, 2012 at 19:18

4 Answers 4

2

No, if either of those if statements fails, you are still in the inner loop that is incrementing j. In order to get out of the inner loop you need to use the break statement.

Sign up to request clarification or add additional context in comments.

2 Comments

Justin, so I can continue until j < totalCols fails even though neither the if or else statement are executing?
Justin, I'm sorry but now I am confused by your second comment. How can either statements execute if they both returns false? Such as when (puzzle[i][j] == "#") return false, and (spaceCount > 0) returns false as well? FYI... I am very new, and have only ever seen cases where there is a default statement.
1

Nope, you first finish looping through the second loop and thus increment the j.

    for (var j = 0; j < totalCols; j++) {

Only once you're done with that loop, do you go back to the 1st and increment the i.

Comments

0

If the if statement fails the nested if won't execute. It goes to the else block and the loop continues until the condition i < totalRows is met.

If the if statement passes and the nested if fails, the loop still continues until i < totalRows is met.

If both if statements passes the loop continues the condition until i < totalRows is met.

What I am saying in essence is the if statement in the nested loop has nothing to do with the outer loop.

Comments

0

You can use the break statement to achieve this behavior. edit: should have read the answers first.

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.