Author Topic: Quick 'for loop' question...  (Read 416 times)

Legacy_Dark Defiance

  • Full Member
  • ***
  • Posts: 125
  • Karma: +0/-0
Quick 'for loop' question...
« on: September 06, 2010, 01:52:00 am »


               Can you decrement the loop cycle while in the for loop itself? For example (a snipet of code):

[snip]

        int iLoop;
        for (iLoop = 1; iLoop <= 5; iLoop++)
        {

            if(Random(2)+1 == 2) iLoop--;

        }
[snip]

The lexicon does not seem to cover this type of subject regarding the for loop.

TIA!
               
               

               
            

Legacy_Shadooow

  • Hero Member
  • *****
  • Posts: 7698
  • Karma: +0/-0
Quick 'for loop' question...
« Reply #1 on: September 06, 2010, 02:20:32 am »


               Yes, but your code above would make infinite loop, the value would be still the same.

I think this is what you want.

int iLoop;
 for(iLoop=5; iLoop > 0; iLoop--)
 {
  if(Random(2)+1 == 2)
  {
  do something
  }
 }


btw I would write it this way

int iLoop = 5;
 for(; iLoop-- > 0;)
 {
  if(Random(2))
  {
  do something
  }
 }


               
               

               


                     Modifié par ShaDoOoW, 06 septembre 2010 - 01:24 .
                     
                  


            

Legacy_Invisig0th

  • Sr. Member
  • ****
  • Posts: 279
  • Karma: +0/-0
Quick 'for loop' question...
« Reply #2 on: September 06, 2010, 02:30:50 am »


               Dark Defiance,

Most of the experienced professional programmers I know (myself included) prefer the first of these two ways. The second is harder to read/understand at a glance, and can easily lead to errors. Just because one can write something with fewer characters does not mean it is a good idea, or that it has any benefits at all (other than showing off).

Since you also used the syntax of the first one in your own code, it's probably best to stick with what you are familiar with.
               
               

               


                     Modifié par Invisig0th, 06 septembre 2010 - 01:39 .
                     
                  


            

Legacy_Shadooow

  • Hero Member
  • *****
  • Posts: 7698
  • Karma: +0/-0
Quick 'for loop' question...
« Reply #3 on: September 06, 2010, 02:42:40 am »


               

Invisig0th wrote...

Dark Defiance,

Most of the experienced professional programmers I know (myself included) prefer the first of these two ways. The second is harder to read/understand at a glance, and can easily lead to errors. Just because one can write something with fewer characters does not mean it is a good idea, or that it has any benefits at all (other than showing off).

Since you also used the syntax of the first one in your own code, it's probably best to stick with what you are familiar with.

Well there is a matter of efficiency, its not less characters, but less commands which matter. Anyway this does not make any difference at performancy, the gain is too small there.

Otherwise sure, I just want to show different method of writing statements. When writing script for public use, you should avoid these techniques in order to script be easily readable by anyone.
               
               

               
            

Legacy_Invisig0th

  • Sr. Member
  • ****
  • Posts: 279
  • Karma: +0/-0
Quick 'for loop' question...
« Reply #4 on: September 06, 2010, 03:04:04 am »


               

ShaDoOoW wrote...
Well there is a matter of efficiency, its not less characters, but less commands which matter. Anyway this does not make any difference at performancy, the gain is too small there.

Incorrect. Using abbreviated syntax like this does not produce fewer commands to the processor. The FOR loop will be processed the exact same way at the machine language level, regardless of which particular way you choose to type it.  So the gain is not "small", because there is actually no gain at all.

So considering that your abbreviated syntax is not actually any more efficient -- not even a little bit -- I'm sure you can begin to understand why most experienced programmers don't bother to use it.
               
               

               


                     Modifié par Invisig0th, 06 septembre 2010 - 02:24 .
                     
                  


            

Legacy_Shadooow

  • Hero Member
  • *****
  • Posts: 7698
  • Karma: +0/-0
Quick 'for loop' question...
« Reply #5 on: September 06, 2010, 03:28:18 am »


               

Invisig0th wrote...
bull****s

I know too well that nobody can persuade you, because you have always right, so I won't lose my time.
               
               

               


                     Modifié par ShaDoOoW, 06 septembre 2010 - 06:04 .
                     
                  


            

Legacy_Invisig0th

  • Sr. Member
  • ****
  • Posts: 279
  • Karma: +0/-0
Quick 'for loop' question...
« Reply #6 on: September 06, 2010, 03:37:43 am »


               

ShaDoOoW wrote...

Invisig0th wrote...
bull****s

Profanity - impressive. Very respectful to the original poster.

Regardless, what you said above is incorrect, and therefore it is bad scripting advice. I would suggest that you learn a lot more about computer programming before trying to tell actual computer programmers how things like this work.
               
               

               


                     Modifié par Invisig0th, 06 septembre 2010 - 03:10 .
                     
                  


            

Legacy_Tarmack

  • Newbie
  • *
  • Posts: 10
  • Karma: +0/-0
Quick 'for loop' question...
« Reply #7 on: September 06, 2010, 04:03:37 am »


               Let's keep the personal stuff out of it.  Thanks.
               
               

               
            

Legacy_Dark Defiance

  • Full Member
  • ***
  • Posts: 125
  • Karma: +0/-0
Quick 'for loop' question...
« Reply #8 on: September 06, 2010, 04:12:29 am »


               I'm sorry, but now I am confused. According to the 1.69 lexicon under

Home > Lyceum > Primer > Conditionals and Loops > for Loop


"If expr2 evaluates to FALSE the loop will break. "


And the code I wrote:

        int iLoop;
        for (iLoop = 1; iLoop <= 5; iLoop++)
        {

            if(Random(2)+1 == 2) iLoop--;

        }

Doesn't it mean "if iLoop is less than or equal to 5" continue the loop? So if iLoop reached 6, it would break the loop?

But my main question was can I decrement the loop from within it? In the case above say iLoop was equal to 4 (4th loop), the if statement equaled 2, it would decrement iLoop to equal 3? So when the loop cycles, it would be on its 3rd loop rather then 5th. Poor example? Did I just confuse anyone? lol.

TIA
               
               

               


                     Modifié par Dark Defiance, 06 septembre 2010 - 03:13 .
                     
                  


            

Legacy_Shadooow

  • Hero Member
  • *****
  • Posts: 7698
  • Karma: +0/-0
Quick 'for loop' question...
« Reply #9 on: September 06, 2010, 04:23:06 am »


               

Dark Defiance wrote...

I'm sorry, but now I am confused. According to the 1.69 lexicon under

Home > Lyceum > Primer > Conditionals and Loops > for Loop


"If expr2 evaluates to FALSE the loop will break. "


And the code I wrote:


        int iLoop;
        for (iLoop = 1; iLoop <= 5; iLoop++)
        {

            if(Random(2)+1 == 2) iLoop--;

        }

Doesn't it mean "if iLoop is less than or equal to 5" continue the loop? So if iLoop reached 6, it would break the loop?

But my main question was can I decrement the loop from within it? In the case above say iLoop was equal to 4 (4th loop), the if statement equaled 2, it would decrement iLoop to equal 3? So when the loop cycles, it would be on its 3rd loop rather then 5th. Poor example? Did I just confuse anyone? lol.

TIA

Eh, yes, you can, it  won't raise infinite loop, I was wrong, the example confused me...':?'

So, you can do anything with iLoop, its variable as any else except its used to break the loop. You can set it to be 9 in order to break loop immediately next loop (better to use break; statement), you can set it to 0 again, you can decrease it by 1 as in your example.
               
               

               
            

Legacy_Dark Defiance

  • Full Member
  • ***
  • Posts: 125
  • Karma: +0/-0
Quick 'for loop' question...
« Reply #10 on: September 06, 2010, 12:21:46 pm »


               Thanks guys! The help is appreciated. :-)
               
               

               
            

Legacy_Redunct

  • Jr. Member
  • **
  • Posts: 85
  • Karma: +0/-0
Quick 'for loop' question...
« Reply #11 on: September 06, 2010, 08:15:34 pm »


               

Dark Defiance wrote...

I'm sorry, but now I am confused. According to the 1.69 lexicon under

Home > Lyceum > Primer > Conditionals and Loops > for Loop


"If expr2 evaluates to FALSE the loop will break. "


And the code I wrote:


        int iLoop;
        for (iLoop = 1; iLoop <= 5; iLoop++)
        {

            if(Random(2)+1 == 2) iLoop--;

        }

Doesn't it mean "if iLoop is less than or equal to 5" continue the loop? So if iLoop reached 6, it would break the loop?

But my main question was can I decrement the loop from within it? In the case above say iLoop was equal to 4 (4th loop), the if statement equaled 2, it would decrement iLoop to equal 3? So when the loop cycles, it would be on its 3rd loop rather then 5th. Poor example? Did I just confuse anyone? lol.

TIA


Yes to the first question, no to the second. It would be on the fourth loop, since it will subtract one (-1) and then add one before the next loop fires (+1). So it would be 4-1+1=4.  

If you wanted it to fire either addition or subtraction, you can do it this way:

        int iLoop;
        for (iLoop = 1; iLoop <= 5;)
        {

            if(Random(2)+1 == 2)
            iLoop--;
            else
            iLoop++;
        }