while-loop

Use a for-loop or a for-each loop? [closed]

大城市里の小女人 提交于 2019-12-04 13:42:53
Closed . This question is opinion-based. It is not currently accepting answers. Learn more . Want to improve this question? Update the question so it can be answered with facts and citations by editing this post . Closed 6 years ago . Should we prefer the for-each loop instead of the traditional for-loops? Is the while-loop advantageous? List<String> names = Arrays.asList("John", "Jeff", "Mary", "Elise"); //for-each loop for(String name: names){ log(name); } //traditional for-loop for(int index=0; index < 10; ++index){ log(names.get(index)); } //Iterator while Iterator<String> iter1 = names

While loops using Await Async.

强颜欢笑 提交于 2019-12-04 10:11:48
问题 This Javascript function seems to use the while loop in an asynchronous way. Is it the correct way to use while loops with asynchronous conditions? var Boo; var Foo = await getBar(i) while(Foo) { Boo = await getBar3(i) if (Boo) { // something } Foo = await getBar(i) i++ } What I think it does is this: var Boo; var Foo; getBar(i).then( (a) => { Foo = a; if(Foo) { getBar3(i).then( (a) => { Boo = a if(Boo) { //something i++; getBar(i).then( (a} => { Repeat itself...} } } } }) If that's totally

For-loop vs while loop in R

懵懂的女人 提交于 2019-12-04 09:57:20
问题 I have noticed a curious thing whilst working in R. When I have a simple program that computes squares from 1 to N implemented using for-loop and while-loop the behaviour is not the same. (I don't care about vectorisation in this case or apply functions). fn1 <- function (N) { for(i in 1:N) { y <- i*i } } AND fn2 <- function (N) { i=1 while(i <= N) { y <- i*i i <- i + 1 } } The results are: system.time(fn1(60000)) user system elapsed 2.500 0.012 2.493 There were 50 or more warnings (use

how to continuously switch textswitcher between two values every 2 seconds

有些话、适合烂在心里 提交于 2019-12-04 09:32:20
I've looked at the code here as well as the code here but I still can't seem to get my code to work right. With the 2nd link, I can get a "timer" that counts up on the page, but with the first, my UI locks up. What I'm trying to do is have a seperate thread that continually flips the text in a textswitcher every 3 seconds as long as the app is open. I need it to switch between two values, and have tried something like the following: private Runnable mUpdateTimeTask = new Runnable() { public void run() { while(true){ try { mHandler.post(new Runnable(){ @Override public void run() { try {

Best refactoring for the dreaded While (True) loop

我是研究僧i 提交于 2019-12-04 09:18:18
问题 If, like me, you shiver at the site of a While (True) loop, then you too must have thought long and hard about the best way to refactor it away. I've seen several different implementations, none really better than any other, such as the timer & delegate combination. So what's the best way you've come up with or seen to refactor the dreaded While (True) loop? Edit : As some comments mentioned, my intent was for this question to be an "infinite loop" refactoring, such as running a Windows style

How does while loop work with mysqli_fetch_assoc()?

风流意气都作罢 提交于 2019-12-04 07:28:15
问题 This is probably a silly beginner question and i don't think it's limited to mysqli_fetch_assoc() so it's probably a general programming question. Anyways, i have this PHP code for getting data from a database using mysqli $sql = "SELECT name FROM table1"; $result = mysqli_query($conn, $sql); if($result){ if(mysqli_num_rows($result) > 0){ while($row = mysqli_fetch_assoc($result)){ echo "Name: " . $row["name"]. "<br>"; } } } What i don't understand is how the while loop there works. How does

While loop freezes game in Unity3D

此生再无相见时 提交于 2019-12-04 07:18:43
问题 I've always struggled with while loops because they barely ever work for me. They always cause my Unity3D application to freeze, but in this instance I really need it to work: bool gameOver = false; bool spawned = false; float timer = 4f; void Update () { while (!gameOver) { if (!spawned) { //Do something } else if (timer >= 2.0f) { //Do something else } else { timer += Time.deltaTime; } } } Ideally, I want those if statements to run as the game runs. Right now it crashes the program and I

How to stop a running method with keyboard input in a Console Application on C#?

自作多情 提交于 2019-12-04 07:09:47
In short, I'm utilizing C# to scientific computation and I've written a method that has a while loop that may run to a user-specified quantity of steps... Actually, this method may take too long to execute (like more than 5 hours). When it takes this long, I may want to stop the method pressing Esc key, for example. As I read something about breaking while , it is as simple as a Boolean flag or something like this. So I thought in something like this: public Double? Run(int n) { int i = 0; while ((i < n) && (/* inputkey != ConsoleKey.Escape */)) { // here goes the heavy computation thing //

Nested WHILE loops in Python

岁酱吖の 提交于 2019-12-04 07:06:15
I am a beginner with Python and trying few programs. I have something like the following WHILE loop construct in Python (not exact). IDLE 2.6.4 >>> a=0 >>> b=0 >>> while a < 4: a=a+1 while b < 4: b=b+1 print a, b 1 1 1 2 1 3 1 4 I am expecting the outer loop to loop through 1,2,3 and 4. And I know I can do this with FOR loop like this >>> for a in range(1,5): for b in range(1,5): print a,b 1 1 1 2 .. .. .. .. // Other lines omitted for brevity 4 4 But, what is wrong with WHILE loop? I guess I am missing some thing obvious, but could not make out. Answer: The corrected WHILE loop.. >>> a=0 >>>

Java: while loop freezes program

风流意气都作罢 提交于 2019-12-04 06:59:59
问题 I'm making a game and i need to update the JProgressBar every 3 seconds. To do that i use a while loop. The problem is that my program freezes becuse of the while loop (i read it in other questions, they didn't help me to solve this). I don't know how to solve it. Here is my code: public static void city (String[] args){ //loading some of the saveData (not all of it is made yet) try{ File saveDataFile = new File("save.dat"); Scanner saveDataSc = new Scanner(saveDataFile); int power =