Author Topic: 'for' statement and flow control  (Read 1553 times)

Legacy_meaglyn

  • Hero Member
  • *****
  • Posts: 1451
  • Karma: +0/-0
'for' statement and flow control
« Reply #15 on: December 31, 2014, 02:02:26 am »


               



It sounds like you are trying to say that the FOR control structure is not just a glorified while loop. That due to the assumption that it does not have a 'continue' statement as part of the control structure it is entirely different. Where both the 'continue' and the 'break' statements will work in either loop.


 




 


I was not saying that the continue statement did not work in while loops. But because of the way for loops have the loop end statement already built in you get your state changed automatically without having to make sure you increment x (or whatever) before any continue statements.  Notice in your example how you put the x++ right at the top of your while loop body so you could use continue safely. Most while loops in NWN don't do that (oItem = GetNextItemInInventory anyone?). So to use the continue statement you need to do extra work which is done for you in a for loop.  A for loop is not just a while loop with a counter... (of course you can't do inventory walk loops in NWNScript in a for loop as such to take advantage of that but that's a different discussion).


 


And yes I said you can do anything with a for loop you can do with a while loop and vice-versa.


 


 






 





 



Just to sum this up.   The 'for' loop is nothing more then a while loop with a 'preloop statment'  and a 'endloop statement' added to the control structure. You can in fact write any 'for' loop as a while loop.   Both loops below will compile to the same thing.  


 



 


Right, and now drop a continue statement in each loop. The for loop terminates properly and the while loop does not, because the endloop statement is executed automatically in the for loop. When the things you are doing inside the loop get more interesting than these toy examples that can matter. Cleaner easier code with fewer bugs is a Good Thing.



for ('preloop statment'; 'conditional statement';'endloop statement')
{
  continue;
   SpeakString.....bla bla bla. 

}


'preloop statment';
while ('conditional statement')
{
   continue;
   SpeakString.....bla bla bla.

  'endloop statement'; 
}

My point was that the automatic execution of the "endloop statement" can be useful and is more than just a different way of writing the loop. Now if NWNScript allowed non-integer expressions in preloop and endloop statements this would be even more useful. E.g.  for(object oItem = GetFirstItemInINventory(oPC); GetIsObjectValid(oItem); oItem = GetNextObjectInIntory(oPC)) would be really handy (or the foreach mentioned earlier in the thread).


 


 


 



               
               

               
            

Legacy_Guest_TrillClinton_*

  • Newbie
  • *
  • Posts: 11
  • Karma: +0/-0
'for' statement and flow control
« Reply #16 on: December 31, 2014, 02:30:32 am »


               

I generally tend to keep a rule by Robert C Martin, who emphasizes on code. 


 


He states that for every for statement, I should only have one statement in it. Example,



for(int index=0;index<size;index++)
{

    doSomething();

}

doSomething(); gives me the ability to reduce the dependency on that for statement and increases my change for re-usability. Also, never use magic numbers. Here is an example,



for(int index=0;index<9;index++)
{

    doSomething();

}

the above for statement reduces the chance of variable re-usability,easier to change and can be used throughout the system without having to repopulate them throughout your system in terms of a problem.


 


Typically for statements are used for iteration of ranges. For a procedure that requires iteration based on condition, I would use a while loop.



while(Inventory.isEmpty())
{
    removeInventoryItem();

}

Again, not a scripter. I just love to program



               
               

               
            

Legacy_ShadowM

  • Hero Member
  • *****
  • Posts: 1373
  • Karma: +0/-0
'for' statement and flow control
« Reply #17 on: December 31, 2014, 04:31:04 am »


               

It is kind of like having a long debate on using 'i' or 'n'  in front of your integers


 


Oh no, do not mention the integer wars so many deaths so much pain!


'<img'>



               
               

               
            

Legacy_Proleric

  • Hero Member
  • *****
  • Posts: 1750
  • Karma: +0/-0
'for' statement and flow control
« Reply #18 on: December 31, 2014, 07:27:06 am »


               Let alone i versus n [versus b], any use of Hungarian notation is controversial in some quarters '<img'>
 
In a hobby environment, where there are no agreed standards, the most important aspect is the outcome, i.e. if it works, it's good.
 
Good style makes our code more maintainable, and more legible to other community members, but there's no absolute measure of right and wrong, only opinion.
 
Following Bioware convention helps (because we're familiar with it). Structured code is easier to read. The rest is a matter of preference.
               
               

               
            

Legacy_meaglyn

  • Hero Member
  • *****
  • Posts: 1451
  • Karma: +0/-0
'for' statement and flow control
« Reply #19 on: December 31, 2014, 03:22:55 pm »


               

@proleric, agreed (except for some of the Bioware convention - which I find painful to read), I was just trying to clear up Lightfoot's misunderstanding of the point I was trying to make, not to argue that people should use for loops more... 


 


Personally I like a tab-width indentation (8 characters) and so the use of continue in those loops can help keep the code from being indented all the way across the screen, thus making it more readable.



               
               

               
            

Legacy_Squatting Monk

  • Hero Member
  • *****
  • Posts: 776
  • Karma: +0/-0
'for' statement and flow control
« Reply #20 on: December 31, 2014, 10:53:49 pm »


               

I kinda wish nwnscript had a real implimentation of a foreach statement.

I think it might be achieveable with nwnx - but it would be very hacky.

[snip]

The obvious benefits of a foreach is that it doesnt require you to know the amount of items in the list
With the SetLocal/GetLocal approach - you are going to sorta need to know that information :
If you are just checking to see if an object is not OBJECT_INVALID - then you will run into issues when object 5 out of 15 (a creature), dies and gets cleaned up by his body disappearing.
Object 5 becomes OBJECT_INVALID, and it would break the loop - preventing progression to objects 6-15 etc


My solution to this was to make wrapper functions to deal with lists that handle keeping count of the number of items in the list. An example loop might look like this:
object oObject;
int i, nCount = GetObjectListCount(OBJECT_SELF, "SomeList");
for (i = 0; i < nCount; i++)
{
    oObject = GetObjectListItemByIndex(OBJECT_SELF, i, "SomeList");
    // Do stuff to oObject
}
What would really be magical is to be able to pass functions as parameters. Then you could make a foreach function and have it use the function on each item in the list. As is, you could do something similar to this by making your foreach function call ExecuteScript() or SignalEvent() on the object. That could be unnecessarily heavy depending on what you're looking to do, though.