# 2 really basic questions

This topic is 4348 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I have had 2 basic questions that I have always avoided, and did it my way, so I may as well ask them to see what is correct Snippet 1: use this:

int variable;
for(int y =0; y < 100; y++)
{
variable = [anything];
}


or this:

for(int y =0; y < 100; y++)
{
int variable = [anything];
}


Snippet 2: use this:

int variable = [anything];
int variable2 = [anything] + 1;

both are used throughout a entire class


or this:

int variable = [anything];

used throughout the class, but when we come to a place that needs what was variable2, just have variable + 1


Note these are just samples, I am using them in much more complex terms as well as simple ones like this. in both cases, I always went for the top case for both snippets, but in some cases the bottom one can be cleaner. What is faster/better/stronger?

##### Share on other sites
in example 1. Both are correct for different situations:

a. this is correct if you want to use variable outside the scope of the for loop (i.e. outside the curly braces) or if you want the value to persist between iterations in the loop. variables only exist in the scope within which you declare them. So in a, variable is available both inside and outside the for loop.

b. this is correct if you only use variable inside the for loop. because it's declared within the for loop, it is only available inside the for loop. it should also be noted that it is a different variable with each iteration of the loop. i.e. values put into variable on y == 1 will not exist in y == 2

-me

##### Share on other sites
ok, I was assuming that the variables are not used out of scope, It is alright to allocate for it each loop, or do I have it wrong?

##### Share on other sites
Quote:
 Original post by biohazok, I was assuming that the variables are not used out of scope, It is alright to allocate for it each loop, or do I have it wrong?

If it's just an int or other POD type, you're golden, it should be optimized out. Otherwise, it's going to reconstruct (but probably not reallocate) the object each time. This may or may not be what you want. Also, if you're accumulating something in the variable (e.g. each new step has some dependence on the last step other than the loop's index variable) you should place the variable outside the loop (Since if it's in the loop you're technically handed a new variable each time so you shouldn't assume anything about its value).

As for the second, I'd say it depends on context. Is "variable + 1" expensive? (e.g. if variable is a class that's overloaded operator+()) Does "variable + 1" clearly express what's going on? Is "variable + 1" used often? Usually, I'd say it's better (more readable, faster, etc.) to use a second variable, but there are times I'd use just the one variable.

##### Share on other sites
Sample 1 has been discussed well already.

Sample 2 has also been talked about, let me chime in. Basically, if you do it often enough that it's computationally expensive, or if it's confusing as the +1 looks like a "magic variable" being added to a defined value use the second variable.

If it's a temp counter that's only used once or twice then maybe the + 1 is more convenient.

• 11
• 16
• 11
• 13
• 11