Author Topic: What the loop??  (Read 394 times)

Legacy_Xardex

  • Sr. Member
  • ****
  • Posts: 414
  • Karma: +0/-0
What the loop??
« on: February 26, 2012, 03:55:24 pm »


                   for (int i = 0; i<GetStringLength(sID); ++i)
ERROR: UNKNOWN STATE IN COMPILER


    int i = 0;
    for (; i<GetStringLength(sID); ++i)
No error.

Anyone?
               
               

               


                     Modifié par Xardex, 26 février 2012 - 03:56 .
                     
                  


            

Legacy_Shadooow

  • Hero Member
  • *****
  • Posts: 7698
  • Karma: +0/-0
What the loop??
« Reply #1 on: February 26, 2012, 04:16:19 pm »


               declaring variables inside for/while loop is not possible with standard compiler

you can write this:

   int i;
   for (i; i<GetStringLength(sID); ++i)

or

   int i;
   for (i=0; i<GetStringLength(sID); ++i)

or you can even left the first expression as you did, you can even do this:

   int i ;
   for (; i++<GetStringLength(sID)'<img'>

but then you shoul consider while loop '<img'>
               
               

               
            

Legacy_Xardex

  • Sr. Member
  • ****
  • Posts: 414
  • Karma: +0/-0
What the loop??
« Reply #2 on: February 26, 2012, 04:37:06 pm »


               Madness. I have used half a dozen compilers and this is the first time I find myself unable to declare variable inside a loop. I suppose its a fairly common function in compilers, or I have been rather lucky.

Ironically, I have used nwscript easily the most and it's where I began coding!
               
               

               


                     Modifié par Xardex, 26 février 2012 - 04:40 .
                     
                  


            

Legacy_Lightfoot8

  • Hero Member
  • *****
  • Posts: 4797
  • Karma: +0/-0
What the loop??
« Reply #3 on: February 26, 2012, 05:29:47 pm »


               

Xardex wrote...

Madness. I have used half a dozen compilers and this is the first time I find myself unable to declare variable inside a loop. I suppose its a fairly common function in compilers, or I have been rather lucky.

Ironically, I have used nwscript easily the most and it's where I began coding!


 The differance here is that the NWN compiler creates the variable at the same place in the code where it hit the declairation.   Where normaly compilers produce code that create all of the variables at the begining of the compiled code that they create.   In short if what you wrote for(int i...   was allowed to compile, the compiled code would end up creating a new variable on every loop iteration. The when the script finished you would end up with a stack overflow/underflow error during run time.    The same thing also often happens when variable declrations are placed in the loops, Not the compiler error, just the stack errors during run time.  
               
               

               


                     Modifié par Lightfoot8, 26 février 2012 - 05:30 .
                     
                  


            

Legacy_Failed.Bard

  • Hero Member
  • *****
  • Posts: 1409
  • Karma: +0/-0
What the loop??
« Reply #4 on: February 26, 2012, 05:45:50 pm »


               

Xardex wrote...

    for (int i = 0; i<GetStringLength(sID); ++i)
ERROR: UNKNOWN STATE IN COMPILER


    int i = 0;
    for (; i<GetStringLength(sID); ++i)
No error.

Anyone?


  You're checking string length every loop either way.  You'd be better off assigning a variable to the string length before starting the for loop, or swapping out the for loop completely for a simpler while loop (edit: as ShadoOow'd apparently already mentioned.  I missed that first skim over the replies).

  int i;
  int nLength =  GetStringLength(sID);
  while (++i < nLength)
      {
       // code block in here
      }
               
               

               


                     Modifié par Failed.Bard, 26 février 2012 - 05:47 .
                     
                  


            

Legacy_Xardex

  • Sr. Member
  • ****
  • Posts: 414
  • Karma: +0/-0
What the loop??
« Reply #5 on: February 26, 2012, 06:08:10 pm »


               I had just wrote the code and just wanted to know why it caused the error. I ended up using a different approach all together.

Anyway, thanks for the info and the help.