Questions tagged [while-loop]
A while loop is a control structure used in many programming languages to continuously execute a set of instructions as long as a particular condition is met.
24,212
questions
998votes
19answers
1.6mviews
How to emulate a do-while loop?
I need to emulate a do-while loop in a Python program. Unfortunately, the following straightforward code does not work:
list_of_ints = [ 1, 2, 3 ]
iterator = list_of_ints.__iter__()
element = None
...
755votes
14answers
834kviews
Syntax for a single-line while loop in Bash
I am having trouble coming up with the right combination of semicolons and/or braces. I'd like to do this, but as a one-liner from the command line:
while [ 1 ]
do
foo
sleep 2
done
644votes
5answers
264kviews
Why is “while ( !feof (file) )” always wrong?
What is wrong with using feof() to control a read loop? For example:
#include <stdio.h>
#include <stdlib.h>
int
main(int argc, char **argv)
{
char *path = "stdin";
FILE ...
607votes
23answers
94kviews
Which is faster: while(1) or while(2)?
This was an interview question asked by a senior manager.
Which is faster?
while(1) {
// Some code
}
or
while(2) {
//Some code
}
I said that both have the same execution speed, as the ...
369votes
12answers
232kviews
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..."
312votes
13answers
696kviews
How do I plot in real-time in a while loop using matplotlib?
I am trying to plot some data from a camera in real time using OpenCV. However, the real-time plotting (using matplotlib) doesn't seem to be working.
I've isolated the problem into this simple ...
304votes
6answers
175kviews
How can you run a command in bash over and over until success?
I have a script and want to ask the user for some information, but the script cannot continue until the user fills in this information. The following is my attempt at putting a command into a loop to ...
285votes
34answers
133kviews
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 ...
272votes
5answers
425kviews
How to check if all elements of a list match a condition?
I have a list consisting of like 20000 lists. I use each list's 3rd element as a flag. I want to do some operations on this list as long as at least one element's flag is 0, it's like:
my_list = [["a"...
265votes
20answers
170kviews
Declaring variables inside or outside of a loop
Why does the following work fine?
String str;
while (condition) {
str = calculateStr();
.....
}
But this one is said to be dangerous/incorrect:
while (condition) {
String str = ...
245votes
8answers
215kviews
A variable modified inside a while loop is not remembered
In the following program, if I set the variable $foo to the value 1 inside the first if statement, it works in the sense that its value is remembered after the if statement. However, when I set the ...
229votes
3answers
17kviews
Java method with return type compiles without return statement
Question 1:
Why does the following code compile without having a return statement?
public int a() {
while(true);
}
Notice: If I add return after the while then I get an Unreachable Code Error.
...
225votes
21answers
201kviews
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 ...
190votes
15answers
14kviews
How can I make sense of the `else` clause of Python loops?
Many Python programmers are probably unaware that the syntax of while loops and for loops includes an optional else: clause:
for val in iterable:
do_something(val)
else:
clean_up()
The body ...
190votes
2answers
243kviews
How to break out of a loop in Bash?
I want to write a Bash script to process text, which might require a while loop.
For example, a while loop in C:
int done = 0;
while(1) {
...
if(done) break;
}
I want to write a Bash script ...