while-loop

Javascript, while loop return

眉间皱痕 提交于 2019-11-30 09:24:51
问题 var i = 0; while(i < 100){ return "The number is " + i; i++; } What is wrong with my return statement? Why can I return a string plus a variable? 回答1: return means end of function and return some value. Any statements after return statement will not be executed and the execution of a function will terminate at return statement. So, return in your case will make the loop to execute only one and terminate it. 回答2: First of all your code should be inside a function. Secondly the return statement

do-while with Java8-Optional

血红的双手。 提交于 2019-11-30 08:58:11
I'm frequently using the do-while-checkNextForNull-getNext looping pattern (don't know if there is an official name for it) in some of my projects. But in Java8, the use of Optional is considered as cleaner code than checking for null references in client-code. But when using Optional in this looping pattern, the code gets a bit verbose and ugly, but because Optional has some handy methodS, I would expect that there must exist a cleaner way than the one I came up with below. Example: Given the following class. class Item { int nr; Item(nr) { this.nr = nr; // an expensive operation } Item next(

Using scanf in a while loop

荒凉一梦 提交于 2019-11-30 08:20:50
问题 Probably an extremely simple answer to this extremely simple question: I'm reading "C Primer Plus" by Pratta and he keeps using the example while (scanf("%d", &num) == 1)... Is the == 1 really necessary? It seems like one could just write: while (scanf("%d", &num)) It seems like the equality test is unnecessary since scanf returns the number of objects read and 1 would make the while loop true. Is the reason to make sure that the number of elements read is exactly 1 or is this totally

What's the difference between “while 1” and “while True”?

房东的猫 提交于 2019-11-30 08:05:46
I've seen two ways to create an infinite loop in Python: while 1: do_something() while True: do_something() Is there any difference between these? Is one more pythonic than the other? Fundamentally it doesn't matter, such minutiae doesn't really affect whether something is 'pythonic' or not. If you're interested in trivia however, there are some differences. The builtin boolean type didn't exist till Python 2.3 so code that was intended to run on ancient versions tends to use the while 1: form. You'll see it in the standard library, for instance. The True and False builtins are not reserved

Which is faster in SQL, While loop, Recursive Stored proc, or Cursor?

不羁的心 提交于 2019-11-30 07:49:53
问题 Which is faster in SQL, While loop, Recursive Stored proc, or Cursor? I want to optimize the performance in a couple of spots in a stored procedure. The code I'm optimizing formats some strings for output to a file. 回答1: I'll assume you are using SQL Server. First of all, as someone said in the statements, recursive stored procs, while possible, are not a good idea in SQL Server because of the stack size. So, any deeply recursive logic will break. However, if you have 2-3 levels of nesting at

While loop with try catch fails at bad cin input

徘徊边缘 提交于 2019-11-30 05:59:55
问题 I can't seem to figure out why this falls into a loop after getting non-int input. I've tried cin.flush(), which doesn't seem to exist, cin.clear(), which seems like it should work, even cin.sync() after reading someone else post about it working, but didn't seem to make much sense. Also tried cin.bad(). Thank you very much for any help Please enter the first number: f Sorry, I don't think that's a number? Please enter the first number: Sorry, I don't think that's a number? Please enter the

Is the code “while(condition);” valid and what does it mean?

落花浮王杯 提交于 2019-11-30 05:54:58
Can we put semicolon like while(condition); in a C Programming? If while(condition); is valid, what does it mean? while (condition); is the same as while (condition) { } It can be used for waiting loops, e.g.: while (!isLightGreen()); // waits until isLightGreen() returns true go(); It means the body of the loop is empty. Typically this pattern is used when the condition itself does some work. For example, this loop can copy bytes within the condition: while ( '\0' != (*pt++ = *ps++)) ; If you're using a semicolon, it's a good idea to put it on a separate line so that it looks less like a

while(true); loop throws Unreachable code when isn't in a void

半世苍凉 提交于 2019-11-30 05:38:06
I was doing some small programs in java. I know that if I write while(true); the program will freeze in this loop . If the code is like that: Test 1: public class While { public static void main(String[] args) { System.out.println("start"); while (true); System.out.println("end"); } } The compiler throws me the error: Exception in thread "main" java.lang.Error: Unresolved compilation problem: Unreachable code at While.main(While.java:6) I didn't know that this error exists. But I got why it is thrown. Of course, line 6 was unreachable , causing a compilation problem. Then I tested this: Test 2

How to use CSV data set config inside a while loop in JMeter?

六月ゝ 毕业季﹏ 提交于 2019-11-30 05:05:00
问题 I need to test the same set of urls against 5 to 10 servers. URLs are defined in the CSV file. Server names are defined in User Defined Variables config. I'm using While Controller based on the number of servers to iterate and execute the url requests. My current logic is defined as below: Thread group While controller Counter (defines number of servers) While controller (inner check "${URL}" != "<EOF>") CSV Data Set Config (stop EOF is true) HTTP Sampler (with url data) As per the logic my

How to close a stage after a certain amount of time JavaFX

爱⌒轻易说出口 提交于 2019-11-30 03:51:54
问题 I'm currently working with two controller classes. In Controller1 it creates a new stage that opens on top of the main one. Stage stage = new Stage(); Parent root = FXMLLoader.load(getClass().getResource("Controller2.fxml")); Scene scene = new Scene(root); stage.setScene(scene); stage.show(); Now once that stage is open, I want it to stay open for about 5 seconds before closing itself. Within Controller2, I've tried implementing something like long mTime = System.currentTimeMillis(); long end