A while loop is a control structure used in many languages to loop over a set of instructions as long as a particular condition is met.

learn more… | top users | synonyms (2)

141
votes
22answers
18k views

Are “while(true)” loops so bad? [closed]

I've been programming in Java for several years now, but I just recently returned to school to get a formal degree. I was quite surprised to learn that, on my last assignment, I lost points for using ...
115
votes
12answers
150k views

do-while loop in Python?

I need to emulate a do-while loop in a python. But, unfortunately, following straightforward code does not work: l = [ 1, 2, 3 ] i = l.__iter__() s = None while True : if s : print s try : ...
92
votes
8answers
12k views

How to optimize for-comprehensions and loops in Scala?

So Scala is supposed to be as fast as Java. I'm revisiting some Project Euler problems in Scala that I originally tackled in Java. Specifically Problem 5: "What is the smallest positive number that is ...
71
votes
33answers
14k views

JavaScript - Are loops really faster in reverse…?

I've heard this quite a few times. Are JavaScript loops really faster when counting backward? If so, why? I've seen a few test suite examples showing that reversed loops are quicker, but I can't find ...
43
votes
4answers
38k views

Timer & TimerTask versus Thread + sleep in Java

I found similar questions asked here but there weren't answers to my satisfaction. So rephrasing the question again- I have a task that needs to be done on a periodic basis (say 1 minute intervals). ...
35
votes
3answers
2k views

Perl: while with no conditional

According to the doc, the while statement executes the block as long as the expression is true. I wonder why it becomes an infinite loop with an empty expression: while () { # infinite loop ... } ...
32
votes
16answers
6k views

Declaring variables inside or outside of a loop

Why is this: String str; while(condition){ str = calculateStr(); ..... } better than this? while(condition){ String str = calculateStr(); ..... ...
31
votes
5answers
6k views

Else clause on Python while statement

I've noticed the following code is legal in Python. My question is why? Is there a specific reason? n = 5 while n != 0: print n n -= 1 else: print "what the..." Thanks.
29
votes
17answers
10k views

For vs. while in C programming?

There are 3 loops in C: for, while, do-while. What's the difference between them? For example, it seems nearly all while statement can be replaced by for statement, right? Then, what's the advantage ...
26
votes
16answers
2k views

Best Loop Idiom for special casing the last element

I run into this case a lot of times when doing simple text processing and print statements where I am looping over a collection and I want to special case the last element (for example every normal ...
23
votes
24answers
12k views

do…while vs while [duplicate]

Possible Duplicates: While vs. Do While When should I use do-while instead of while loops? I've been programming for a while now (2 years work + 4.5 years degree + 1 year pre-college) and ...
22
votes
8answers
9k views

What's the difference between iterating over a file with foreach or while in Perl?

I have a filehandle FILE in Perl, and I want to iterate over all the lines in the file. Is there a difference between the following? while (<FILE>) { # do something } and foreach ...
22
votes
8answers
16k views

Writing a while loop in the C preprocessor

I am asking this question from an educational/hacking point of view, (I wouldn't really want to code like this). Is it possible to implement a while loop only using C preprocessor directives. I ...
19
votes
16answers
3k views

Is the code “while(condition);” valid and what does it mean?

Can we put semicolon like while(condition); in a C Programming? If while(condition); is valid, what does it mean?
19
votes
16answers
2k views

Is there any performance difference between for() and while()?

Or is it all about semantics?

1 2 3 4 5 243
15 30 50 per page