user2288683
user2288683

Reputation: 11

Nested 'for loops not initializing correctly

the variable j initializes to 172, which is the length of the wordList it is supposed to be iterating through. wordList is an ArrayList Thanks for any help.

for (i = 0; i < wordList.size(); i++)
     {
        row0 = wordList.get(i);
        ara = breakWord(row0);
        my2Dara[0] = ara;

            for (j = 0; j < wordList2.size(); j++);
        {
           row1 = wordList2.get(j);
           ara = breakWord(row1);
           my2Dara[1] = ara;

                for (k = 0; k < wordList.size(); k++);
           {
              row2 = wordList.get(k);
              ara = breakWord(row2);
              my2Dara[2] = ara;

                    for (l = 0; l < wordList.size(); l++);
              {
                 row3 = wordList.get(l);
                 ara = breakWord(row3);
                 my2Dara[3] = ara;

                 for (m = 0; m < wordList.size(); m++);
                 {
                    row4 = wordList.get(m);
                    ara = breakWord(row4);
                    my2Dara[4] = ara;

                    if(isSolution(my2Dara, wordList) == true)
                       print2Dara(my2Dara);             
                 }
              }
           }
        }
     } 

Upvotes: 1

Views: 89

Answers (1)

Patashu
Patashu

Reputation: 21773

You've been stung by a C/C++ism that Java has faithfully inherited - the ability to end a for, while or if immediately by placing a semicolon after it.

Code that looks like this:

for(whatever);
{
   doThing()
}

is actually doing this:

1) Set up the for loop - do initializer, check that we can perform the first loop

2) The semicolon tells the compiler 'this is the end of the for loop', so the for loop iterates furiously over doing nothing until the condition evaluates to false.

3) The part in braces { } afterwards is executed once - this is not a compiler error because you're allowed to place brace pairs more or less at any time for any reason (this is used to declare a new scope, kind of like how in an if you create a new variable scope - it's not the if, it's the braces!)

The reason why 2) is possible is because if, for, while and so on are syntactically defined to execute only ONE thing - the very next statement. A ; is a statement that does nothing - think of it as doSomething(); without the doSomething() part. { doX(); doY(); } is also one statement - braces can be thought of as syntax to define lots of statements as one statement. The compiler is faithfully doing what you type.

The consequence of 2) and 3) is that all of these things are syntactically valid and usually not what you expected:

for(whatever);

if(true);

if(false);
{
    crashProgram(); //This will always execute! Good prank to play on computer science undergrads
}

{
   doThing(); //don't need an if, for, while or anything else to open {}s
}

{
 {
  {
    doThing(); //open as many as you like!
  }
 }
}

;;;;;;; //do a whole lot of nothing

Upvotes: 3

Related Questions