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.

607
votes
13answers
1.0m views

Emulate a do-while loop in Python?

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...
566
votes
22answers
84k views

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 ...
511
votes
11answers
512k views

Syntax for a single-line Bash infinite while loop

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 ]dofoosleep 2done
486
votes
5answers
189k views

Why is “while ( !feof (file) )” always wrong?

I've seen people trying to read files like this in a lot of posts lately.Code#include <stdio.h>#include <stdlib.h>int main(int argc, char **argv){char * path=argc > 1 ? ...
245
votes
8answers
126k 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=5while n !=0:print nn -=1else:print "what the..."
233
votes
34answers
97k views

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 ...
225
votes
3answers
15k views

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....
210
votes
21answers
144k 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 ...
203
votes
20answers
107k views

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=...
185
votes
14answers
11k views

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 ...
170
votes
5answers
93k views

How can you run a command in bash over until success

I have a script and want to ask the user for some information, the script cannot continue until the user fills in this information. The following is my attempt at putting a command into a loop to ...
165
votes
10answers
328k views

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 ...
134
votes
6answers
127k views

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 ...
131
votes
5answers
197k views

How to check if all elements of a list matches 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"...
129
votes
8answers
18k 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 ...

153050per page
angop.ao, elkhabar.com, noa.al, afghanpaper.com, bbc.com, time.com, cdc.gov, nih.gov, xnxx.com, github.com,