while-loop

Assign variable in Java while-loop conditional?

為{幸葍}努か 提交于 2019-12-30 07:55:35
问题 I have a method that does a lot of checking and computation and returns a custom class, returnMessage. returnMessage has 2 booleans and a String. What I want to do is run this method in a while loop from my main class and have access to the returnMessage object after the while loop terminates for the last time. In php this would be a case of while ( $returned = myObject->myMethod()->finished ) { } if( $returned -> finished == FALSE) { ... } However trying to assign like this gives me a

Accepting any number of inputs from scanf function

那年仲夏 提交于 2019-12-30 06:57:28
问题 I am trying to read an unknown number of inputs using scanf function. int a[100]; int i = 0; while((scanf("%d", &a[i])) != '\n') i++; // Next part of the code But this function is not going to next part of the code, seems like there is an infinite while loop. How Do I solve this logical error? Is there any other alternatives to scanf like sscanf to read integers into an array? 回答1: scanf returns the number of input items that have been successfully matched and assigned, thus it is reasonable

C++ performance, for versus while

给你一囗甜甜゛ 提交于 2019-12-30 05:58:53
问题 In general (or from your experience), is there difference in performance between for and while loops? What if they are doubly/triply nested? Is vectorization (SSE) affected by loop variant in g++ or Intel compilers? Thank you 回答1: Here is a nice paper on the subject. 回答2: Any intelligent compiler won't really show a difference between them. A for loop is really just syntactic sugar for a certain form of while loop, anyways. 回答3: This is something easily ascertained by looking at disassembly.

How do I ensure that a Python while-loop takes a particular amount of time to run?

流过昼夜 提交于 2019-12-30 03:28:24
问题 I'm reading serial data with a while loop. However, I have no control over the sample rate. The code itself seems to take 0.2s to run, so I know I won't be able to go any faster than that. But I would like to be able to control precisely how much slower I sample. I feel like I could do it using 'sleep', but the problem is that there is potential that at different points the loop itself will take longer to read(depending on precisely what is being transmitted over serial data), so the code

How do I ensure that a Python while-loop takes a particular amount of time to run?

♀尐吖头ヾ 提交于 2019-12-30 03:28:03
问题 I'm reading serial data with a while loop. However, I have no control over the sample rate. The code itself seems to take 0.2s to run, so I know I won't be able to go any faster than that. But I would like to be able to control precisely how much slower I sample. I feel like I could do it using 'sleep', but the problem is that there is potential that at different points the loop itself will take longer to read(depending on precisely what is being transmitted over serial data), so the code

Why is this statement printed twice in while loop?

亡梦爱人 提交于 2019-12-30 02:26:10
问题 I wrote this simple program for practise: #include <stdio.h> #include <stdlib.h> #include <string.h> #define CLASSES 3 #define STUDENTS 4 int grades[CLASSES][STUDENTS]; int main(void) { int i = 1; char t,k; while(i == 1) { printf("\n\n\nMENU:\nEnter the grades(E)\nReport Grades(R)\nQuit(Q)\nYour choice: "); k = toupper(getchar()); printf("Input entered... %c\n", k); switch(k) { case 'E' : printf("Entering the grades..\n"); break; case 'R' : printf("Reporting the grades...\n"); break; case 'Q'

How to tell CasperJS to loop through a series of pages

不问归期 提交于 2019-12-30 00:36:08
问题 I try to make CasperJS achieve the following: Go through a series of pages that are named sequentially by date. On each page, locate a PDF link. Download the PDF. I got some working code, but I don't understand how CasperJS is going through the sequence of events. For instance, in the code sample below, CasperJS tries to process step 2, and throws a "ReferenceError: Can't find variable: formDate", while step 1 isn't executed at all for some reason. What's wrong with my reasoning? It seems to

Break statement inside two while loops

青春壹個敷衍的年華 提交于 2019-12-29 18:42:42
问题 Let's say I have this: while(a){ while(b){ if(b == 10) break; } } Question: Will the break statement take me out of both loops or only from the inner one? Thank you. 回答1: In your example break statement will take you out of while(b) loop while(a) { while(b) { if(b == 10) { break; } } // break will take you here. } 回答2: It will break only the most immediate while loop. Using a label you can break out of both loops: take a look at this example taken from here public class Test { public static

Best Loop Idiom for special casing the last element

我只是一个虾纸丫 提交于 2019-12-29 10:36:09
问题 I run into this case a lot of times when doing simple text processing and print statements where I am looping over a collection and I want to special case the last element (for example every normal element will be comma separated except for the last case). Is there some best practice idiom or elegant form that doesn't require duplicating code or shoving in an if, else in the loop. For example I have a list of strings that I want to print in a comma separated list. (the do while solution

Error catching with try-catch and while loop [duplicate]

梦想与她 提交于 2019-12-29 09:50:10
问题 This question already has answers here : How to handle infinite loop caused by invalid input (InputMismatchException) using Scanner (5 answers) Closed 4 years ago . I'm trying to make sure that the users input is an integer but when I use the below code I just get an infinite loop of the print statement. Any advice of how to improve? boolean valid = false; System.out.println("What block are you gathering? (use minecraft block ids)"); while(valid == false){ try{ block = input.nextInt(); valid