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

new i ... or for( new i ...?


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Ghosted
Veteran Member
Join Date: Apr 2015
Location: Georgia
Old 08-26-2016 , 03:59   new i ... or for( new i ...?
Reply With Quote #1

which one is better ?

Code:
new i
for( i ; ... )
Code:
for( new i ; ... )
Ghosted is offline
ddhoward
Veteran Member
Join Date: May 2012
Location: California
Old 08-26-2016 , 05:08   Re: new i ... or for( new i ...?
Reply With Quote #2

The first one doesn't make any sense. It's either:
Code:
new i
for( ; ... )
or
Code:
for( new i ; ... )
Yeah? Yeah.
__________________
ddhoward is offline
PRoSToTeM@
Veteran Member
Join Date: Jan 2010
Location: Russia, Ivanovo
Old 08-26-2016 , 05:10   Re: new i ... or for( new i ...?
Reply With Quote #3

The second one, because you will access 'i' variable only in 'for' body. Use the first one if you need last 'i' value after loop (but better use another variable for this goal). If you will access variable only in this scope then declare it exactly in this scope.
Also with the second you will not have problem if you already declared variable 'i' in this scope. But be careful with the same variable naming.

P.S. Try not to reuse same variable for another goal, because this can break code understandability and may hard to modify code.
__________________
PRoSToTeM@ is offline
Send a message via ICQ to PRoSToTeM@ Send a message via Skype™ to PRoSToTeM@
redivcram
Veteran Member
Join Date: Jul 2014
Location: Serbia
Old 08-26-2016 , 12:20   Re: new i ... or for( new i ...?
Reply With Quote #4

If you don't plan to use it below the "for" loop, then second.
redivcram is offline
Craxor
Veteran Member
Join Date: Jan 2016
Location: Romania
Old 08-26-2016 , 16:21   Re: new i ... or for( new i ...?
Reply With Quote #5

The second one, first arg. from for() is initialization, so better do something like: for ( new i =0 ....
Craxor is offline
Send a message via ICQ to Craxor
Bad_Bud
Senior Member
Join Date: Oct 2006
Location: The internet
Old 08-27-2016 , 13:04   Re: new i ... or for( new i ...?
Reply With Quote #6

Or if you really want

Code:
static i
for( i = 0 ; ... )
__________________

Last edited by Bad_Bud; 08-27-2016 at 13:05.
Bad_Bud is offline
klippy
AlliedModders Donor
Join Date: May 2013
Location: Serbia
Old 08-27-2016 , 21:00   Re: new i ... or for( new i ...?
Reply With Quote #7

Quote:
Originally Posted by Bad_Bud View Post
Or if you really want

Code:
static i
for( i = 0 ; ... )
That's actually the worst option.
klippy is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 08-28-2016 , 01:29   Re: new i ... or for( new i ...?
Reply With Quote #8

Quote:
Originally Posted by Bad_Bud View Post
Or if you really want

Code:
static i
for( i = 0 ; ... )
static should only be used if the function where it is located is called extremely often or you need to retain the value of the previous execution. This use case, as far as we can tell, meets neither of these requirements.
__________________
fysiks is offline
siriusmd99
Veteran Member
Join Date: Oct 2013
Location: Republic of Moldova
Old 08-28-2016 , 04:47   Re: new i ... or for( new i ...?
Reply With Quote #9

If using "for (new i)" , you won't be able to use "i" outside the loop.
For example if you want to find first admin and you break loop you won't be able to use "i" next as admin id, it will be only in the loop .

Last edited by siriusmd99; 08-28-2016 at 04:47.
siriusmd99 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 15:18.


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