English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

in his question i'll give you 2 loops and pick thebest one the script is written in java

a)
int i=0;
int endOfLoop=10;

for (i;i<=endOfLoop;i++)
{
//code
}

b)
for (int i=0;i<=10;i++)
{
//code
}

i prefer b

2006-09-15 17:31:25 · 4 answers · asked by Rami 5 in Computers & Internet Programming & Design

4 answers

In this example, where you are insulated from any other code, yes, b would be the best choice. If you knew that the surrounding code required at least one other for loop (non-nested), one could arguably say that initialization of the integer i outside the for loop could be more efficient in speed and storage. These might be achieved by re-use of i as a loop control parameter. The only pre-requisite here would be that the initialization be in a proper scope so that the integer not be left for the garbage collector to get at program close. A rarity, but possible. Real world and "best known practice" sometimes do not agree. Best to think carefully on each case, IMHO.

2006-09-16 05:19:42 · answer #1 · answered by Anonymous · 1 0

b is better because it does not initialize variables outside of the for loop. This will help later on with recursion and sorting algorithms.

2006-09-15 17:53:46 · answer #2 · answered by procomp9 1 · 1 0

I agree with you guys, b is the best pick between the two. It's cleaner and more straightforward.


KaBalweg
http://smartcoder.awardspace.com

2006-09-15 20:05:09 · answer #3 · answered by dabsani 3 · 0 0

extremely tough issue. do a search on bing and yahoo. just that may help!

2015-04-27 17:05:54 · answer #4 · answered by ? 2 · 0 0

fedest.com, questions and answers