while-loop

Timing R code with Sys.time()

半城伤御伤魂 提交于 2019-12-01 16:02:12
I can run a piece of code for 5 or 10 seconds using the following code: period <- 10 ## minimum time (in seconds) that the loop should run for tm <- Sys.time() ## starting data & time while(Sys.time() - tm < period) print(Sys.time()) The code runs just fine for 5 or 10 seconds. But when I replace the period value by 60 for it to run for a minute, the code never stops. What is wrong? As soon as elapsed time exceeds 1 minute, the default unit changes from seconds to minutes. So you want to control the unit: while (difftime(Sys.time(), tm, units = "secs")[[1]] < period) From ?difftime If ‘units =

Python While Loop, the and (&) operator is not working

a 夏天 提交于 2019-12-01 15:26:23
问题 I am trying to find the greatest common factor. I wrote a bad (operation intensive) algorithm that decrements the lower value by one, checks using % to see if it evenly divides both the numerator and denominator, if it does then it exits the program. However, my while loop is not using the and operator, and thus once the numerator is divisible it stops, even though its not the correct answer. The numbers I am using are 54 and 42, the correct GCD (greatest common denominator) is 6. #heres a

Difference between moving an Iterator forward with a for statement and a while statement

不羁的心 提交于 2019-12-01 15:09:51
When I use an Iterator of Object I use a while loop (as written in every book learning Java, as Thinking in Java of Bruce Eckel): Iterator it=... while(it.hasNext()){ //... } but sometime i saw than instead somebody use the for loop : Iterator it=... for (Iterator it=...; it.hasNext();){ //... } I don't' understand this choice: I use the for loop when I have a collection with ordinal sequence (as array) or with a special rule for the step (declared generally as a simple increment counter++ ). I use the while loop when the loop finishes with I have'nt this constraints but only a logic condition

Unable to break while loop immediately on button click c# mvc

狂风中的少年 提交于 2019-12-01 14:49:53
I have mvc action method where while loop is running I want to stop that while loop on another button click so I have maintain a flag and set it to false to break loop. bool flag = true; public async Demo() { while(flag == true) { do something... } } now on button click I'm calling one action method to stop the while loop - public ActionResult StopLoop() { flag = false; Return View("Index") } But it is taking almost a minute time to hit this StopLoop after click on button. why ? may be all are on same UI page and sharing same homecontroller any trick i can do ? i have 2 buttons on one button

C++ infinite loop

落花浮王杯 提交于 2019-12-01 14:44:01
I am attempting to write a loop that will repeat until the user enters one of the correct choices (either 1 or 0). For some reason when I have the loop written as below it creates an infinite loop. I am intending for the loop to only execute while control is not 0 OR not 1, but for some reason it will always execute and becomes an infinite loop. cout<<"Please enter 1 for another customer or 0 to quit : "; cin>>control; while ((control != 0 )|| (control != 1)) { cout<<"Invalid Entry! Please enter a 1 to enter another customer or 0 to quit: "; cin>>control; } I changed it to be while control

Update Label In While Loop Swift

若如初见. 提交于 2019-12-01 14:18:05
Correct me if I'm wrong, but when I try to update my label in a while loop it won't update it. Is the reason that the while loop runs too quickly for the label to update within that time so it'll cancel the label from updating? I need a solution to this? I need to be able to update the label straight away? func Download(complete: (canMoveOn: Bool) -> Void) { isDownload = true if (connected!) { Reset() let data = NSData(contentsOfFile: path!)! let buffer = (UnsafeMutablePointer<UInt8>(data.bytes)) var leftOverSize = data.length let bytesFile = data.length var totalBytesRead = 0 var bytesRead =

How does this while block work? [closed]

回眸只為那壹抹淺笑 提交于 2019-12-01 13:40:44
问题 Closed . This question needs details or clarity. It is not currently accepting answers. Want to improve this question? Add details and clarify the problem by editing this post. Closed 4 years ago . var result = 1 var counter = 0 while (counter < 10) { result = result * 2 counter += 1 }; console.log(result); I am confused how does counter update result here? We are increasing counter by 1 but how does that affect the result? Can someone please dumb it down to me? I am new to programming. Edit

What is the purpose of a do-while loop?

孤街浪徒 提交于 2019-12-01 13:38:57
问题 I know what do does, and how it cooperates with the while loop, but won't a while loop code be the same, whether or not the do is there? 回答1: Consider the following: while(condition){ myFunction(); } and do{ myFunction(); }while(condition); The second form executes myFunction() at least once then checks the condition ! To do so with a while loop you've to write: myFunction(); while(condition){ myFunction(); } 回答2: Use do-while() construct when you have to get your task executed at least once

checking if input from scanner is int with while loop java

流过昼夜 提交于 2019-12-01 13:32:30
问题 I basically want the following while loop to check if the input is an integer. It cannot contain decimals because it is pointing to an array. If the value entered is a decimal it should prompt the user again. Problem is I get two prompts before the while loop starts with this code. Any ideas? System.out.print("Enter month (valid values are from 1 to 12): "); Scanner monthScan = new Scanner(System.in); int monthInput = monthScan.nextInt(); // If the month input is below 1 or greater than 12,

Unable to break while loop immediately on button click c# mvc

此生再无相见时 提交于 2019-12-01 13:30:52
问题 I have mvc action method where while loop is running I want to stop that while loop on another button click so I have maintain a flag and set it to false to break loop. bool flag = true; public async Demo() { while(flag == true) { do something... } } now on button click I'm calling one action method to stop the while loop - public ActionResult StopLoop() { flag = false; Return View("Index") } But it is taking almost a minute time to hit this StopLoop after click on button. why ? may be all