@CQQL:
In C++, for is just a rewrite of a while loop (with a slightly different syntax). Everything that can be done with for can be done with while or goto (although the latter is really not advice-able to use, since it unnecessarily makes your code complicated). The same can be said in reverse, everything that can be done with while and goto can be done with for (although this will jump around the basic syntax of for-loops, since you'll end up to not use the initialization or increment step in some cases).
I personally prefer for when I'm using something that needs to be iterated through. I use while for everything else loop-related. That's only my preference, however. I recommend you decide for yourself what best suits you, although there's little doubt in my mind that for can make your code more readable.
Reason is simple. We can use the while to simulate the do-while.
In terms of language looping construct, I think Perl is really fascinating. You have if-else,for,while and their reverse cousin unless, until and others like foreach etc.
A do-while loop can not only make the code look more elegant but it may make the program run faster as well,in some situations.If one had to loop several times to get input from a user and store the input in a variable,and assumming the condition for the loop depends on such variable,then with a do-while the variable wouldn't have to be unnecesarily initialized as with a while loop.Besides that,i never understood why so many egg-headed nerds think that their code is better and more readable because they write such things as:
when using more lines and a while loop does the same thing but does not look like basic chinese to someone reviewing the code,and is in my imho a better programming practice in such cases.But that's just my opinion.Cheers.
Yeah! I love doing that! Here's the worst offenders I could find:
1 2 3 4 5 6
for (long src_y=y0,dst_y=y;src_y<box.h && dst_y<(long)sp.h;src_y++,dst_y++){ /*...*/ }
//Note: This for is actually empty. Also one of my favorite things to do.
for (ulong a=0;offset<size && str[offset]!='\"' && a<4;a++,offset++);
for (unsignedlong b=array[a-2],c=array[a-1];c;a++,b++,c--){ /*...*/ }
I also like to stick critical operations in conditionals.
if (can_do_something && (do_something(data) && do_something_else(data) || cleanup(data)){ /*...*/ }
"while/for" - these are that strange constructs they left in Scala not to annoy all those coming from C++/Java that don't understand folds, projections, selections and recursion. :D
I haven't used them for months. Loops are for low-level programming. Like John Carmack said somewhere in his presentation: most of the loops in Unreal Tournament Engine could be replaced easily by higher-level, more readable and less error-prone abstractions, if only C++ allowed this.
Jacopini s' work shows that xorebxebx is wrong.There is no need to complicate a language with such things you say Scala does.The beauty of c++ is that it is both simple and powerful at the same time.BTW,why don't you go to a Scala forum? I am surprised to see how many people replied your posts at the "Earn Money" thread (you know which one).You are a troll.