do-while

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

Do while javascript issue

£可爱£侵袭症+ 提交于 2019-12-01 12:01:22
I'm trying to send multiple post within a do while loop but the result is not added <script type="text/javascript"> function action() { var initval = 1; var endval = 5; do { var action_string = 'txtuser=someone'; $.ajax({ type: "POST", url: "http://localhost/js.php", data: action_string, success: function(result){ $('div#append_result').append(initval + ',<br/>'); } }); initval++; } while (initval <= endval); } </script> The Output is: 5, 5, 5, 5, 5, and I need the output to be: 1, 2, 3, 4, 5, Due to the async nature of AJAX, by the time your success function runs for any of the resulting AJAX

Do-while endlessly looping cout, ignores cin

守給你的承諾、 提交于 2019-12-01 11:26:25
This program prints a specified amount of numbers within a specified range. However, when I enter a character, it just endlessly loops whichever do-while loop I do it in. E.g: If I enter a character in "Enter maximum number" cin, it just spams "Enter maximum number" endlessly, it just skips the cin and loops the cout (same goes for the other 2 do-whiles. Anyone know why? #include <iostream> #include <cmath> #include <cstdlib> #include <ctime> using namespace std; int roll(int mini, int maxi) { int v = maxi - mini; int x = mini + (rand() % (v+1)); return x; } void caller() { int a; int b; int c

Do-while endlessly looping cout, ignores cin

ε祈祈猫儿з 提交于 2019-12-01 09:28:00
问题 This program prints a specified amount of numbers within a specified range. However, when I enter a character, it just endlessly loops whichever do-while loop I do it in. E.g: If I enter a character in "Enter maximum number" cin, it just spams "Enter maximum number" endlessly, it just skips the cin and loops the cout (same goes for the other 2 do-whiles. Anyone know why? #include <iostream> #include <cmath> #include <cstdlib> #include <ctime> using namespace std; int roll(int mini, int maxi)

syntax error near unexpected token `<'

妖精的绣舞 提交于 2019-12-01 02:10:21
StudentAnwser=() inputScriptFile=001.sh while IFS= read -r line; do StudentAnwser+=( "$line" ) done < <( sh $inputScriptFile test.txt ) it returns a error foo.sh: line 22: syntax error near unexpected token `<' foo.sh: line 22: ` done < <( sh $inputScriptFile test.txt )' what's wrong with that? I follow the solution from other question for reading line from result You get the error because process substitution (the <(some command) part) is not a standard feature (defined in POSIX) in sh , which means it may work on some OS but may not in others or in the same OS with different configuration.

When would a do-while loop be the better than a while-loop?

老子叫甜甜 提交于 2019-11-30 21:47:03
This is a highly subjective question, so I'll be more specific. Is there any time that a do-while loop would be a better style of coding than a normal while-loop? e.g. int count = 0; do { System.out.println("Welcome to Java"); count++; } while (count < 10);` It doesn't seem to make sense to me to check the while condition after evaluating the do-statement (aka forcing the do statement to run at least once). For something simple like my above example, I would imagine that: int count = 0; while(count < 10) { System.out.println("Welcome to Java"); count++; } would be generally considered to have

Why do i get a NullPointerException at If statement in do while loop when reading a simple textfile in Java

百般思念 提交于 2019-11-30 21:41:14
问题 I'm new in Java, I have a lot of work with text and I've got an idea to make a simple program to do my work. I'm getting error Exception in thread "main" java.lang.NullPointerException at com.text.work.Main.main(Main.java:25) public class Main { public static void main(String[] args) throws IOException { int someNumber = 0; PrintWriter saveFileOne = new PrintWriter("save.txt"); PrintWriter saveFileTwo = new PrintWriter("otherThings.txt"); FileReader fileReader = new FileReader("read.txt");

do-while and while comparison

懵懂的女人 提交于 2019-11-30 21:07:26
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 The difference between a do-while and a while is when the comparison is done. With a do-while , you'll compare at the end and hence do at least one iteration. Equivalent code for your example do { i++; ++j;

How do I iterate over all bytes in an inputStream using Groovy, given that it lacks a do-while statement?

末鹿安然 提交于 2019-11-30 17:28:54
Given that Groovy does not have a do-while statement, how can I iterate over all bytes in an input stream? Per a previous version of the Groovy user guide : No 'do ... while()' syntax as yet. Due to ambiguity, we've not yet added support for do .. while to Groovy What would be the best way to do something like the following Java code in Groovy? def numRead = inputStream.read(fileBytes, 0, fileBytes.length); do{ } while(numRead > 0); (I know I can do that using a boolean, I just want to know if there's a "Groovy" way of doing it) The groovy (version 1.8+) way would be like this: inputStream

When would a do-while loop be the better than a while-loop?

帅比萌擦擦* 提交于 2019-11-30 17:22:04
问题 This is a highly subjective question, so I'll be more specific. Is there any time that a do-while loop would be a better style of coding than a normal while-loop? e.g. int count = 0; do { System.out.println("Welcome to Java"); count++; } while (count < 10);` It doesn't seem to make sense to me to check the while condition after evaluating the do-statement (aka forcing the do statement to run at least once). For something simple like my above example, I would imagine that: int count = 0; while