do-while

Java do while loop making string testing act differently

懵懂的女人 提交于 2019-11-29 13:20:11
I am very confused at the behavior of using the || operator on the .equals function. Is there a reason I can not use it on strings or something? this works: do{ System.out.println("Play again? [Y/N]"); //input: Y play = in.nextLine(); play = play.toUpperCase(); } while(!"Y".equals(input) ); //breaks out of loop (as it should) why doesn't this work?! do{ System.out.println("Play again? [Y/N]"); //input: Y play = in.nextLine(); play = play.toUpperCase(); } while( !"Y".equals(input) || !"N".equals(input) ); //infinite loop Let's put it into words. "Eat all of this fruit as long as it's not an

Do .. While loop in C#?

佐手、 提交于 2019-11-29 09:06:12
How do I write a Do .. While loop in C#? (Edit: I am a VB.NET programmer trying to make the move to C#, so I do have experience with .NET / VB syntax. Thanks!) The general form is: do { // Body } while (condition); Where condition is some expression of type bool . Personally I rarely write do/while loops - for , foreach and straight while loops are much more common in my experience. The latter is: while (condition) { // body } The difference between while and do...while is that in the first case the body will never be executed if the condition is false to start with - whereas in the latter

Elegant way for do … while in groovy

帅比萌擦擦* 提交于 2019-11-29 04:27:40
问题 How to do code something like this in groovy ? do { x.doIt() } while (!x.isFinished()) Because there is no do ... while syntax in groovy. No 'do ... while()' syntax as yet. Due to ambiguity, we've not yet added support for do .. while to Groovy References: groovy - dev > do while Migration From Classic to JSR syntax Groovy Documentation > Control Structures > Looping Rosetta Code > Loops/Do-while Groovy 回答1: So many answers and not a single one without a redundant call, a shame ;) This is the

Do “nothing” while “condition”

半世苍凉 提交于 2019-11-29 04:19:42
问题 While browsing the code for the Java 8 version of ForkJoinPool(which has a few interesting changes from Java 7) I ran across this construct (here): do {} while (!blocker.isReleasable() && !blocker.block()); I'm struggling with why you would write it like this instead of just while (!blocker.isReleasable() && !blocker.block()); Is it just a semantics/readability choice, since you could read the first construct as do "nothing" while "conditions" ? Or is there some additional benefit I'm missing

Other ways to deal with “loop initialization” in C#

丶灬走出姿态 提交于 2019-11-28 20:13:24
问题 To start with I'll say that I agree that goto statements are largely made irrelevant by higher level constructs in modern programming languages and shouldn't be used when a suitable substitute is available. I was re-reading an original edition of Steve McConnell's Code Complete recently and had forgotten about his suggestion for a common coding problem. I had read it years ago when I was first getting started and don't think I realized how useful the recipe would be. The coding problem is the

Scanner input accepting Strings skipping every other input inside a while loop. [duplicate]

佐手、 提交于 2019-11-28 14:51:52
This question already has an answer here: How to use java.util.Scanner to correctly read user input from System.in and act on it? 1 answer Alright, so the code is pretty straight forward. Generic class ourSet, that takes in some elements, puts it in a LinkedList, and does some functions on the two sets. My problem is actually quite unrelated the general concept of the project, its more in the "user input interface" I've created. I want it to take in some Strings and add it to the set, then while receiving the string "EXIT" (all caps), to exit the loop, and do the same for the next set. What is

Accessing a variable from inside a do-while loop [closed]

青春壹個敷衍的年華 提交于 2019-11-28 13:56:52
问题 How can I access a variable from inside a do-while loop in Java? The code below writes out a value until the value entered is not is between 0 and 10. Here is my code : import java.util.Scanner; public class DoWhileRange { public static void main(String[] args) { do{ System.out.println("Enter a number between 0 an 10"); Scanner in = new Scanner(System.in); int a = in.nextInt(); int total +=0; }while (a>0 && a<10); System.out.println("Loop Terminated"); System.out.println("The total is : "

Why use a “do while” loop? [closed]

≡放荡痞女 提交于 2019-11-28 12:08:18
I've never understood why using a do while loops is necessary. I understand what they do, Which is to execute the code that the while loop contains without checking if the condition is true first. But isn't the below code: do{ document.write("ok"); } while(x == "10"); The exact same as: document.write("ok"); while(x == "10"){ document.write("ok"); } Maybe I'm being very stupid and missing something obvious out but I don't see the benefit of using do while over my above example. Your example code is wrong: do{ document.write("ok"); }while(x == "10"){ document.write("ok"); } This would be the

Java do while loop making string testing act differently

眉间皱痕 提交于 2019-11-28 06:54:36
问题 I am very confused at the behavior of using the || operator on the .equals function. Is there a reason I can not use it on strings or something? this works: do{ System.out.println("Play again? [Y/N]"); //input: Y play = in.nextLine(); play = play.toUpperCase(); } while(!"Y".equals(input) ); //breaks out of loop (as it should) why doesn't this work?! do{ System.out.println("Play again? [Y/N]"); //input: Y play = in.nextLine(); play = play.toUpperCase(); } while( !"Y".equals(input) || !"N"

do-while loop in R

时光毁灭记忆、已成空白 提交于 2019-11-28 05:11:47
I was wondering about how to write do-while-style loop? I found this post : you can use repeat{} and check conditions whereever using if() and exit the loop with the "break" control word. I am not sure what it exactly means. Can someone please elaborate if you understand it and/or if you have a different solution? Pretty self explanatory. repeat{ statements... if(condition){ break } } Or something like that I would think. To get the effect of the do while loop, simply check for your condition at the end of the group of statements. See ?Control or the R Language Definition: > y=0 > while(y <5){