while-loop

do-while and while comparison

匆匆过客 提交于 2019-12-03 21:00:18
问题 do-while: do { i++; ++j; System.out.println( i * j ); } while ((i < 10) && (j*j != 25)); I am learning about do-while vs while at the moment and would like to rewrite the above java fragment (already declared and initialized) using a while instead. Are the below rewritten codes correct way to do so: while: while ((i < 10) && (j*j != 25)) { i++; ++j; System.out.println( i * j ); } Cheers 回答1: The difference between a do-while and a while is when the comparison is done. With a do-while , you'll

Keep opening OpenFileDialog until selecting valid file

谁说我不能喝 提交于 2019-12-03 16:39:07
I have code that opens the OpenFileDialog, I'm checking the size of the file to make sure it doesn't exceed specific limit. But, if the user selected a big sized file I need to warn him and lead him back to the dialog to select a different file or click cancel. This is what I've tried: OpenFileDialog dialog = new OpenFileDialog(); dialog.Filter = "Jpeg files, PDF files, Word files|*.jpg;*.pdf;*.doc;*.docx"; while (dialog.ShowDialog() != DialogResult.Cancel) { var size = new FileInfo(dialog.FileName).Length; if (size > 250000) { MessageBox.Show("File size exceeded"); continue; } } EDIT: I also

Switch statement within while loop in C

丶灬走出姿态 提交于 2019-12-03 15:06:59
There are several postings concerning switch statements within while loops, except for the fact that none of them are done in C, at least from what I've seen. C++ can create boolean expressions, which I'm aware of, but not in C. I have a while loop that contains a switch control. However, when I write break statements within my switch, it goes back to the beginning of the loop and makes my program run forever. Ignore the functions I use, for they work for sure. I just need some clarification on my handling of the nesting. Thanks! Here is my main.c: while(1) { printf("0) Exit\n1) List Tasks\n2)

Why does while loop in an OMP parallel section fail to terminate when termination condition depends on update from different section

我怕爱的太早我们不能终老 提交于 2019-12-03 14:29:43
Is the C++ code below legal, or is there a problem with my compiler? The code was complied into a shared library using gcc version 4.4.6 20110731 (Red Hat 4.4.6-3) (GCC) and openMP and then called via R 2.15.2. int it=0; #pragma omp parallel sections shared(it) { #pragma omp section { std::cout<<"Entering section A"<<std::endl; for(it=0;it<10;it++) { std::cout<<"Iteration "<<it<<std::endl; } std::cout<<"Leaving section A with it="<<it<<std::endl; } #pragma omp section { std::cout<<"Entering section B with it="<<it<<std::endl; while(it<10) { 1; } std::cout<<"Leaving section B"<<std::endl; } } I

R, How does while (TRUE) work?

匆匆过客 提交于 2019-12-03 14:26:53
I have to write a function of the following method : Rejection method (uniform envelope) : Suppose that fx is non-zero only on [a, b], and fx ≤ k. Generate X ∼ U(a, b) and Y ∼ U(0, k) independent of X (so P = (X, Y ) is uniformly distributed over the rectangle [a, b] × [0, k]). If Y < fx(x) then return X, otherwise go back to step 1. rejectionK <- function(fx, a, b, K) { # simulates from the pdf fx using the rejection algorithm # assumes fx is 0 outside [a, b] and bounded by K # note that we exit the infinite loop using the return statement while (TRUE) { x <- runif(1, a, b) y <- runif(1, 0, K

PHP script stops running arbitrarily with no errors

岁酱吖の 提交于 2019-12-03 13:54:47
I have a PHP script that seemed to stop running after about 20 minutes. To try to figure out why, I made a very simple script to see how long it would run without any complex code to confuse me. I found that the same thing was happening with this simple infinite loop. At some point between 15 and 25 minutes of running, it stops without any message or error. The browser says "Done". I've been over every single possible thing I could think of: set_time_limit ( session.gc_maxlifetime in the php.ini) memory_limit max_execution_time The point that the script is stopped is not consistent. Sometimes

How to cycle with an array in MySQL?

末鹿安然 提交于 2019-12-03 13:34:17
问题 I'd like to create a stored procedure or a normal query with values passed with an array. Example: CREATE PROCEDURE proc() BEGIN DECLARE cont INTEGER; DECLARE var ARRAY; SET cont = 0; SET var = ("hi", "hello", "good", ...) WHILE cont < 12 DO SELECT * FROM tablex WHERE name = var[cont]; SET cont = cont + 1; END WHILE; END; Obviously this is will not work, but I'd like to know how to achieve this. 回答1: Try to do it without stored routine - SET @arr = 'hi,hello,good'; -- your array SELECT COUNT(

node.js: while loop callback not working as expected

纵然是瞬间 提交于 2019-12-03 12:40:50
问题 Knowing that while Node.js is working asynchronously, writing something like this: function sleep() { var stop = new Date().getTime(); while(new Date().getTime < stop + 15000) { ; } } sleep(); console.log("done"); ...would call the sleep(), block the server for the duration of the while loop (15secs) and just THEN print "done" to the console. As far as I understand, this is because Node.js is giving JavaScript only access to the main thread, and therefore this kidn of thing would halt further

input of while loop to come from output of `command`

為{幸葍}努か 提交于 2019-12-03 12:08:37
问题 #I used to have this, but I don't want to write to the disk # pcap="somefile.pcap" tcpdump -n -r $pcap > all.txt while read line; do ARRAY[$c]="$line" c=$((c+1)) done < all.txt The following fails to work. # I would prefer something like... # pcap="somefile.pcap" while read line; do ARRAY[$c]="$line" c=$((c+1)) done < $( tcpdump -n -r "$pcap" ) Too few results on Google (doesn't understand what I want to find :( ). I'd like to keep it Bourne-compatible (/bin/sh), but it doesn't have to be.

How to do a while loop with a string redirected into it

人走茶凉 提交于 2019-12-03 09:54:26
Im trying to loop though a string with HTTP links inside and newlines, I want to loop over a line at a time. At the moment I have echo -e "$HTTP_LINKS" | while read HTTP_S_LINK ; do TEST_STRING="test" done But this way I don't have access to the TEST_STRING out side the loop, which is what I want. I'm using the while loop so that it will loop though each newline in $HTTP_LINKS and not just the words in the string. (I don't want to use a for loop with IFS set to \n) I thought maybe I could just do something like this #!/bin/bash while read HTTP_S_LINKS do TEST_STRING="test2" done < $HTTP_LINKS