Saturday, March 24, 2007

Advanced For Loops

There are some skills in our career that we have reached proficient enough to an extent that we think there's nothing extra to learn.

One good example is the for loop, I mean, I dont think there is anything left I need to learn about a for loop.

Well, I was wrong, daaaaaaaaaaaaaaaamn wrong, here's one extra piece of information that'll leave you scratching your head the rest of the day.

for (int i = 0; i < arrCustomers.length(); i++)
{
println(arrCustomers[i]);
}

Becomes:

for (int i = 0, len = arrCustomers.length(); i < len; i++)
{
println(arrCustomers[i]);
}

Now the code above saves your interpreter/compiler from calling the method length() each and every iteration.

Meaning, a wonderful optimization with approximately zero extra code.

Whether you like it or not:
"You ain't a for loop expert as much you thought you were!"

5 comments:

Anonymous said...

oh well, i knew that one, so that makes me more of an expert than YOU are :pppp :pppp :pppp

Basil 3ibs said...

Ok, you know this one.

Then what about this one:

int iVal = 0;
for (char* pPtr = 0;
*pPtr != 0x00; pPtr++)
{
iVal &= (*pPtr & 0x0000FFFF);
iVal << 1;
}

ساري said...

أنا ما كنت بعرفها مع انّي عبقري... مش عارف كيف

بس جد حلوة... شكراً عالمعلومة

بدّك ننزل نتغدّى؟؟؟؟؟

جارك في الطابق الثالث

Anonymous said...

تسلم على أول كومنت عربية

Anonymous said...

whats that? english? shoo the "*" next to char? oh u! tab do u know this:

var shshs = "lissa moo hon";
for (shshs = "moo hon"; shshs<"honak"; shshs++){
// do something to go honak
}
:)