2

Is there any difference if I were to write something like this:

int row,col;
    for(row = 0; row < data.length; row++){
        for(col = 0; col < data[row].length;col++){
            //do something
        }//end for loop(columns)
    }//end for loop(rows)
    for(row = 0; row < data.length; row++){
        for(col = 0; col < data[row].length;col++){
            //do something
        }//end for loop(columns)
    }//end for loop(rows)

compared to:

    for(int row = 0; row < data.length; row++){
        for(int col = 0; col < data[row].length;col++){
            //do something
        }//end for loop(columns)
    }//end for loop(rows)
    for(int row = 0; row < data.length; row++){
        for(int col = 0; col < data[row].length;col++){
            //do something
        }//end for loop(columns)
    }//end for loop(rows)

Are there any bennefits from either one?

Kilian Foth
  • 107,706
  • 45
  • 295
  • 310
user3189506
  • 31
  • 1
  • 2
  • What... is the difference between both versions? – John Dvorak Feb 20 '14 at 06:32
  • 1
    In the first case row and col will be available after the loops finish, in the second case the variables are available only inside the loop. Take a look at what is the scope of a variable. – superM Feb 20 '14 at 06:37
  • I know what is happening I just want to know if there are any real benefits from either like memmory allocation or anything really – user3189506 Feb 20 '14 at 06:40
  • In the first case you're declaring both variables only once, thus saving a little memory and time. But in the first case those variables will potentially stay in memory longer than in the second case, thus in the second case you're [potentially] saving memory for later. In such cases of micro-optimizations it's more of a personal preference than anything else. – superM Feb 20 '14 at 06:46
  • 4
    **Unclear what help you need.** Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it’s hard to tell what problem you are trying to solve or what aspect of your approach needs to be corrected or explained. See the [ask] page for help clarifying this question. – gnat Feb 20 '14 at 06:46

2 Answers2

2

Quick answer: most of the time I don't care about the value outside the loop, so I use the second form.

Occasionally I'll want the "first index" where I'll put a break (or an exit condition on the loop) so that the value represents as far as I got. Then I'll use the first form, but with a break or a loop condition that causes it to stop early.

...e.g. find the index of the first non-zero value in an array.

EDIT: I just looked more closely at your code... I would NEVER reuse the same index variable just for the sake of preserving variable declarations ;) lol!

sea-rob
  • 6,841
  • 1
  • 24
  • 47
2

Both versions may have a specific purpose.

1 Variables inside

Used for iterating(yes, I know about iterators and enhanced loop, but let's use the old for)

2 Variables outside

Let's say we want to find the index of an item from some array and we want that index to use for something else. You can't use the variables for the loop scope because you will need the index so your index must be outside the loop scope.

Example:

int i;
for (i = 0; i < length; i++) {
  if (someConditionAppliedForAnElement(arr[i])) {
    break;
  }
}
if (i != length) { /* logic here */ }

Of course, this is just an example, there are better ways to do this task, but I just wanted to point that sometimes, loop scope doesn't fit.

By the way, for your sample I'd use loop scope indexes.

Silviu Burcea
  • 618
  • 4
  • 13