0

Which one of the following two is better in terms of good programming design, speed, efficiency and memory point of view?

1)
for(int i=0;i<10;i++)
{
...
}

2)
int i;
...
...
for(i=0;i<10;i++)
{
...
}

I mean, if there are many such (or maybe different) loops in program, then which one should be used?

7 Answers7

5

Definitely the first one.

  1. It is more concise and clear;
  2. The variable i will be visible only inside the for block;
  3. Try to never micro-optimize unless you really need it.

Quote from Java docs :

If the variable that controls a for statement is not needed outside of the loop, it's best to declare the variable in the initialization expression.

In Java 8 you can also do this :

IntStream.rangeClosed(1, 10).forEach(System.out::println);
Bax
  • 4,260
  • 5
  • 43
  • 65
1

(This is for Java) Use "int" keyword outside of loop if you want to access it after/before the loop. But if you use it outside I recommend using "while" loop, and adding a one to the int each time. You don't need to assign int variable to 0, since int can't be null, and by default is 0.

int i;
...
...
...
while (i < ***){
   ...
   i++;
}

2nd variant is only useful if you break the loop if there something happened, so you can get how many iterations happened later on.

1

If you don't need to access i outside of the loop, it should be inside the loop. If you put it inside the loop, it is destroyed when the control goes out of the loop, which saves memory. Also putting i inside the loop will make the code more readable.

Yogesh Umesh Vaity
  • 41,009
  • 21
  • 145
  • 105
1

If you wanna use i again after the usage of for like the code below, then go with second one.

int i;
for (i = 0; i < length; i++) {
  ...
  }
}
if (i > 50) { /* your code */ }

However, if you do not need to use 'i' after the usage of for, then go with the first one for the sake of memory usage and simplicity.

Eray Balkanli
  • 7,752
  • 11
  • 48
  • 82
1

The both are bad because they use magic number 10.:)

As for which loop to use then it depends on the context: whether variable i has to be used outside a loop.

If so then I would write

int i;
...
...
i = 0;
for ( ; i < 10; i++ )
{
...
}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
1

Which one of the following two is better in terms of good programming design, speed, efficiency and memory point of view?

In Java you can use both, in C before C99, you can only use the 2nd approach.

As for your question:

  • good programming design: 1 is better as the variable x was only used and meant for the loop, thus enclosing it within scope of the loop will be a better design.

  • speed: Both the same

  • efficiency: Both the same

  • memory: 1 is better since the variable will readily to be discarded after use within the loop.


User Alter Mann brought up a good point about portability for using the 2nd approach.

user3437460
  • 17,253
  • 15
  • 58
  • 106
1

in this scenario.....

Note :-There is no performance issue in both program ...

But only if we can talk about scope of i then First loop is better if there is no requirement of i beyond the loop and If not Needed then you should declare it as per you 2ond Way of Code

Vikrant Kashyap
  • 6,398
  • 3
  • 32
  • 52