Do people not like...

Pages: 123
Do people here not like the while(__) thing or not like to bother with it?
Last edited on
while is not a function but a language construct.
Why do you say so anyway? It's quite commonly used
What else would you use for looping?
Well, for, for instance. Or goto, if you're into that sort of thing.
Well I was just looking around and people who asked things about while never really got replies so I just wondered
It depends on how they ask their question
In most situations for fits better.
I don't think there is much of a preference rather some constructs just pertain to a particular situation/problem better.
@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.

-Albatross
Last edited on
Every language feature has its own purpose
BTW why didn't anyone mention the poor do while ?
BTW why didn't anyone mention the poor do while ?


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.
We can use the while to simulate the do-while.
Actually, no. You can't. You can get something that behaves almost exactly like it, though.

their reverse cousin unless, until
Just NOTs with fancy names.
sohguanh wrote:
Reason is simple. We can use the while to simulate the do-while.

You kind of can; but sometimes a do-while loop is a better choice (for example, the bubblesort pseudocode on the relevant Wikipedia page).

helios wrote:
Just NOTs with fancy names.

This is true, but they're useful ones.
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:

 
for(int a,b,c,d,e?!;(a>b)?c<d=f>e!=((a==a));c++,d--,e--) ; //im'so smart 


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.
Last edited on
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 (unsigned long 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)){ /*...*/ }
helios wrote:
for (ulong a=0;offset<size && str[offset]!='\"' && a<4;a++,offset++);

Guilty :D

Here's one I made earlier:
while (n-- && *a != 0 && *b != 0) { /* ... */ }
Last edited on
closed account (EzwRko23)
"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.
Last edited on
I feel a faggening in the Force.
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.
Last edited on
Pages: 123