while-loop

loop and a half controlled [closed]

懵懂的女人 提交于 2019-11-30 14:03:56
When do we use loop and a half? Also, should someone briefly elaborate how to write its code? You use loop-and-a-half to avoid repeating code from outside the loop to the inside. Example: read a; while a != b do stuff; read a; end becomes while true do read a if a == b then break stuff; end Now I only have the read in one place. As an aside, I'd like to add that the scope of the variable (assuming a is a local variable in this idiom) is minimized as compared to the alternative case, where a is still in scope even after the while loop terminates. Minimizing the scope of local variables is

Invalid token 'while' in class, struct, or interface member declaration in very simple code

泪湿孤枕 提交于 2019-11-30 14:02:11
I am not sure what the problem is but I keep receiving this error when I try to use a while statement in my code. Invalid token 'while' in class, struct, or interface member declaration I want to use a while loop to have something continuously update while a statement is true. The rest of my code is rather long but whenever I type in the syntax: while(a<b) { //do whatever i want it to do here } It gives me that compiler error right off the bat. Not quite sure what the problem is. I am doing this in a C# windows application under the Form1.cs file with all the other event handlers (for buttons

JavaScript: Asynchronous method in while loop

依然范特西╮ 提交于 2019-11-30 13:39:37
I'm tackling a project that requires me to use JavaScript with an API method call. I'm a Java programmer who has never done web development before so I'm having some trouble with it. This API method is asynchronous and it's in a while loop. If it returns an empty array, the while loop finishes. Otherwise, it loops. Code: var done = true; do { async_api_call( "method.name", { // Do stuff. }, function(result) { if(result.error()) { console.error(result.error()); } else { // Sets the boolean to true if the returned array is empty, or false otherwise. done = (result.data().length === 0) ? true :

How can I stop a While loop?

无人久伴 提交于 2019-11-30 12:37:34
I wrote a while loop in a function, but don't know how to stop it. When it doesn't meet its final condition, the loop just go for ever. How can I stop it? def determine_period(universe_array): period=0 tmp=universe_array while True: tmp=apply_rules(tmp)#aplly_rules is a another function period+=1 if numpy.array_equal(tmp,universe_array) is True: break #i want the loop to stop and return 0 if the #period is bigger than 12 if period>12: #i wrote this line to stop it..but seems it #doesnt work....help.. return 0 else: return period just indent your code correctly: def determine_period(universe

What's the most defensive way to loop through lines in a file with Perl?

↘锁芯ラ 提交于 2019-11-30 11:49:06
I usually loop through lines in a file using the following code: open my $fh, '<', $file or die "Could not open file $file for reading: $!\n"; while ( my $line = <$fh> ) { ... } However, in answering another question , Evan Carroll edited my answer, changing my while statement to: while ( defined( my $line = <$fh> ) ) { ... } His rationale was that if you have a line that's 0 (it'd have to be the last line, else it would have a carriage return) then your while would exit prematurely if you used my statement ( $line would be set to "0" , and the return value from the assignment would thus also

PHP json_encode() in while loop

只谈情不闲聊 提交于 2019-11-30 10:58:57
I am trying to use json_encode() in a while loop while getting database results. Here is my code: <? $database = sqlite_open("thenew.db", 0999, $error); if(!$database) die($error); $query = "SELECT * FROM users"; $results = sqlite_query($database, $query); if(!$results) die("Canot execute query"); while($row = sqlite_fetch_array($results)) { $data = $row['uid'] . " " . $row['username'] . " " . $row['xPos'] . " " . $row['yPos']; } echo json_encode(array("response"=>$data)); sqlite_close($database); ?> The output of this is {"response":"lastUserID lastUser lastXPos lastYPos"} I want it to be...

ORA-06502: PL/SQL: numeric or value error: character string buffer too small

倖福魔咒の 提交于 2019-11-30 10:50:34
I tried the following code different ways, like by taking out the while or the if, but when I put both together (if and while), I always get the error at the end... undefine numero set serveroutput on accept numero prompt 'Type # between 100 and 999: ' declare i number:=1; a char(25); b char(1); c varchar2(10); d number; begin c := &numero; d := length(c); b := substr(c, i, 1); while i <= d loop if b = '1' then a:= a||'one '; end if; i := i+1; end loop; dbms_output.put_line('The number is '||a); end; / ERROR: ORA-06502: PL/SQL: numeric or value error: character string buffer too small ORA

Using while loops to count elements in a list

拜拜、爱过 提交于 2019-11-30 09:56:56
问题 places = ["Jack", "John", "Sochi"] count=0 multi_word=0 place = places[count] while place != "Sochi" and count < len(places): if ' ' in place: multi_word += 1 count += 1 place = places[count] print ('Number of cities before Sochi:', count) My code should print the number of cities before Sochi excluding Sochi . I don't understand what this line (place = places[count]) does, nor do I understand why I need it twice. 回答1: foreach would neaten it up places = ["Jack", "John", "Sochi"] count = 0

How can I increment a number in a while-loop while preserving leading zeroes (BASH < V4)

一个人想着一个人 提交于 2019-11-30 09:44:19
问题 I am trying to write a BASH script that downloads some transcripts of a podcast with cURL. All transcript files have a name that only differs by three digits: filename[three-digits].txt from filename001.txt to.... filename440.txt . I store the three digits as a number in a variable and increment the variable in a while loop. How can I increment the number without it losing its leading zeroes? #!/bin/bash clear # [...] code for handling storage episode=001 last=440 secnow_transcript_url="https

Pause While loop by hotkey

a 夏天 提交于 2019-11-30 09:41:48
问题 I want to pause an AutoIt script containing a While loop and some functions. But I am only able to close the script on HotKeySet() . How can I pause it? The script checks for changes on a part of screen (x,y coordinates are set in a config file) and takes screenshots after playing an alert sound. It doesn't stop the While loop when pushing the pause button. But closing the program works. Here is my code: Global $Paused, $counter = 0 HotKeySet("{1}", "TogglePause") HotKeySet("{2}", "Terminate"