Aller au contenu

Photo

What the loop??


  • Veuillez vous connecter pour répondre
5 réponses à ce sujet

#1
Xardex

Xardex
  • Members
  • 217 messages
    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 .


#2
Shadooow

Shadooow
  • Members
  • 4 474 messages
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);)

but then you shoul consider while loop :)

#3
Xardex

Xardex
  • Members
  • 217 messages
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 .


#4
Lightfoot8

Lightfoot8
  • Members
  • 2 535 messages

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 .


#5
Failed.Bard

Failed.Bard
  • Members
  • 774 messages

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 .


#6
Xardex

Xardex
  • Members
  • 217 messages
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.