Raised This Month: $51 Target: $400
 12% 

Questions about "for" loop


Post New Thread Reply   
 
Thread Tools Display Modes
Catastrophe
Veteran Member
Join Date: Jul 2012
Location: somewhere between narnia
Old 11-07-2014 , 06:34   Re: Questions about "for" loop
Reply With Quote #11

Hey guys one more question, what would happen if suppose there was smthing like this:-
Code:
public lalal()
{
         for(new i; i <= ArraySize(somearray) - 1; i++)
         {
         Somethinghere()
         }
}
ok so if in the above code the array size of "somearray" turns out to be 0, what would the loop do ?
__________________
You will find everything u need :-
Catastrophe is offline
HamletEagle
AMX Mod X Plugin Approver
Join Date: Sep 2013
Location: Romania
Old 11-07-2014 , 06:54   Re: Questions about "for" loop
Reply With Quote #12

Quote:
Originally Posted by Catastrophe View Post
Hey guys one more question, what would happen if suppose there was smthing like this:-
Code:
public lalal()
{
         for(new i; i <= ArraySize(somearray) - 1; i++)
         {
         Somethinghere()
         }
}
ok so if in the above code the array size of "somearray" turns out to be 0, what would the loop do ?
Will check the condition, and will execute one time, cause you check <= 0. i starts from 0, which is <= 0, but what if you will test this yourself ? You should cache ArraySize.
__________________

Last edited by HamletEagle; 11-07-2014 at 06:55.
HamletEagle is offline
klippy
AlliedModders Donor
Join Date: May 2013
Location: Serbia
Old 11-07-2014 , 07:38   Re: Questions about "for" loop
Reply With Quote #13

Quote:
Originally Posted by HamletEagle View Post
Will check the condition, and will execute one time, cause you check <= 0. i starts from 0, which is <= 0, but what if you will test this yourself ? You should cache ArraySize.
No, it won't execute even once if ArraySize() returned zero, because he subtracted it by one, giving -1, and 0(i) is not less or equal to -1.

Last edited by klippy; 11-07-2014 at 07:39.
klippy is offline
HamletEagle
AMX Mod X Plugin Approver
Join Date: Sep 2013
Location: Romania
Old 11-07-2014 , 07:44   Re: Questions about "for" loop
Reply With Quote #14

Quote:
Originally Posted by KliPPy View Post
No, it won't execute even once if ArraySize() returned zero, because he subtracted it by one, giving -1, and 0(i) is not less or equal to -1.
I was thinking that his question is about arraysize -1 and not just arraysize.
__________________
HamletEagle is offline
Catastrophe
Veteran Member
Join Date: Jul 2012
Location: somewhere between narnia
Old 11-07-2014 , 09:52   Re: Questions about "for" loop
Reply With Quote #15

Quote:
Originally Posted by HamletEagle View Post
I was thinking that his question is about arraysize -1 and not just arraysize.

Yap my question was for arraysize -1, anyway thanks KliPPy and Hamlet

@Hamlet i dont cache arraysize because i change it in my code several times.....
__________________
You will find everything u need :-

Last edited by Catastrophe; 11-07-2014 at 09:53.
Catastrophe is offline
klippy
AlliedModders Donor
Join Date: May 2013
Location: Serbia
Old 11-07-2014 , 10:30   Re: Questions about "for" loop
Reply With Quote #16

Quote:
Originally Posted by Catastrophe View Post
@Hamlet i dont cache arraysize because i change it in my code several times.....
Not to cache it in that way, but to put it in a new variable before you start the "for" loop. It is better to make one native call and spend 1 cell of memory than to make X + 1 native calls, where X stands for the array size in this case.

Last edited by klippy; 11-07-2014 at 10:31.
klippy is offline
Catastrophe
Veteran Member
Join Date: Jul 2012
Location: somewhere between narnia
Old 11-08-2014 , 09:41   Re: Questions about "for" loop
Reply With Quote #17

Oh ok ... got it... thnx again ;)
__________________
You will find everything u need :-
Catastrophe is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 11-08-2014 , 21:31   Re: Questions about "for" loop
Reply With Quote #18

Quote:
Originally Posted by .Dare Devil. View Post
Its fine to use return in loop. Other ways we could not use it there if it would not be fine.
Just because you can do it does not mean it's a good way to do it.

Quote:
Originally Posted by Catastrophe View Post
Hey guys one more question, what would happen if suppose there was smthing like this:-
Code:
public lalal()
{
         for(new i; i <= ArraySize(somearray) - 1; i++)
         {
         Somethinghere()
         }
}
ok so if in the above code the array size of "somearray" turns out to be 0, what would the loop do ?
You should answer these types of questions by yourself since they are easily tested.
__________________
fysiks is offline
Backstabnoob
Veteran Member
Join Date: Feb 2009
Location: Iwotadai Dorm
Old 11-09-2014 , 03:42   Re: Questions about "for" loop
Reply With Quote #19

Of course it's a good practice to return in a loop, why would it not be?
__________________
Currently busy working on a very large scale anime database project.
Backstabnoob is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 11-09-2014 , 15:41   Re: Questions about "for" loop
Reply With Quote #20

Quote:
Originally Posted by Backstabnoob View Post
Of course it's a good practice to return in a loop, why would it not be?
[/sarcasm] (I hope ...)

I was referring to Dare Devil's reasoning not his conclusion. But in this case, it's probably a coding style thing or it depends on the specific purpose of the loop.
__________________

Last edited by fysiks; 11-09-2014 at 15:45.
fysiks is offline
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 17:07.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode