while-loop

How to exit a while-loop?

扶醉桌前 提交于 2019-12-05 17:58:55
#include <stdio.h> main(void) { char ch; while (1) { if ((ch = getchar()) != EOF) { break; } putchar(ch); } return 0; } How do I escape from this while ? I had tried with EOF but it didn't work. I think you mean: int ch; Because EOF won't fit in a char . Also: if ((ch=getchar()) == EOF) break; Your logic is backwards. This: char ch; is wrong, EOF doesn't fit in a char . The type of getchar() 's return value is int so this code should be: int ch; Also, as pointed out, your logic is backwards. It loop while ch is not EOF , so you can just put it in the while : while((ch = getchar()) != EOF)

why process substitution does not always work with while loop in bash?

放肆的年华 提交于 2019-12-05 17:44:37
问题 The process substitution works with filenames fine, e.g. both $ cat <FILENAME and $ while read i; do echo $i; done <FILENAME work. But if instead of FILENAME we use echo command (or any other, which generates output to stdout), cat continues to work $ cat <(echo XXX) XXX while the loop $ while read i; do echo $i; done <(echo XXX) bash: syntax error near unexpected token `<(echo XXX)' produces error. Any ideas why? 回答1: Note: < filename is not process substitution. It's a redirection. Process

Simulate while loop in Mule 3.4

半世苍凉 提交于 2019-12-05 17:37:34
I need a while loop in my Mule Flow to load data in chunks from a custom DAO (I use an Expression Transformer to access the DAO) until he does not provide any more items. (I don't know the total amount of items the DAO provides.) There is no build-in while loop in Mule 3.4. My first Idea was to use a recursive backreference in a SubFlow. The SubFlow calls itself until the work is done. But I ge a springframework exception for an "unresolvable circular reference". A Flow cannot call itself. My next Idea was to write a Custom Transformer and inject the SubFlow to call in a loop. I use some

Recursion inside while loop, How does it work?

北战南征 提交于 2019-12-05 15:23:38
Can you please tell me how does this java code work? : public class Main { public static void main (String[] args) { Strangemethod(5); } public static void Strangemethod(int len) { while(len > 1){ System.out.println(len-1); Strangemethod(len - 1); } } } I tried to debug it and follow the code step by step but I didn't understand it. update: sorry I didn't mention that I know the result of this code but just want to know the steps of the execution.. That'll print 4 3 2 1 1 1 1 1 1... And get stuck in a loop because nothing ever modifies len in the scope of the while loop. The first calls (with

TensorFlow while-loop with TensorArray

假如想象 提交于 2019-12-05 15:10:56
问题 import tensorflow as tf B = 3 D = 4 T = 5 tf.reset_default_graph() xs = tf.placeholder(shape=[T, B, D], dtype=tf.float32) with tf.variable_scope("RNN"): GRUcell = tf.contrib.rnn.GRUCell(num_units = D) cell = tf.contrib.rnn.MultiRNNCell([GRUcell]) output_ta = tf.TensorArray(size=T, dtype=tf.float32) input_ta = tf.TensorArray(size=T, dtype=tf.float32) input_ta.unstack(xs) def body(time, output_ta_t, state): xt = input_ta.read(time) new_output, new_state = cell(xt, state) output_ta_t.write(time,

Input string with getchar

末鹿安然 提交于 2019-12-05 13:23:22
I am trying to read a string into a char array with a length chosen by the user. The problem is that getchar() doesn't stop reading until the user manually enters a newline by pressing enter, based on my code. I have read through other's threads, and I understand why I'm not able to do it this way, it's just completely contradictory to my assignment handout. int chPrompt(int nchars); void strInput(char str[], int nchars); int main(void) { int nchars = chPrompt(nchars); char str[nchars]; strInput(str, nchars); return 0; } int chPrompt(int nchars) { printf("How many chars do you need to input? >

Running 'prop.test' multiple times in R

百般思念 提交于 2019-12-05 13:16:49
I have some data showing a long list of regions, the population of each region and the number of people in each region with a certain disease. I'm trying to show the confidence intervals for each proportion (but I'm not testing whether the proportions are statistically different). One approach is to manually calculate the standard errors and confidence intervals but I'd like to use a built-in tool like prop.test, because it has some useful options. However, when I use prop.test with vectors, it runs a chi-square test across all the proportions. I've solved this with a while loop (see dummy

Faster method than “while” loop to find chain of infection in R

这一生的挚爱 提交于 2019-12-05 13:01:56
I'm analyzing large tables (300 000 - 500 000 rows) that store data output by a disease simulation model. In the model, animals on a landscape infect other animals. For example, in the example pictured below, animal a1 infects every animal on the landscape, and the infection moves from animal to animal, branching off into "chains" of infection. In my example below, I want to take the table that stores information about each animal (in my example below, table = allanimals ) and slice out just the information about animal d2 's chain of infection (I've highlighted d2 's chain in green) so I can

How do you Make A Repeat-Until Loop in C++?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-05 12:05:40
问题 How do you Make A Repeat-Until Loop in C++? As opposed to a standard While or For loop. I need to check the condition at the end of each iteration, rather than at the beginning. 回答1: do { // whatever } while ( !condition ); 回答2: When you want to check the condition at the beginning of the loop, simply negate the condition on a standard while loop: while(!cond) { ... } If you need it at the end, use a do ... while loop and negate the condition: do { ... } while(!cond); 回答3: You could use

Variable initalisation in while loop

有些话、适合烂在心里 提交于 2019-12-05 11:06:48
I have a function that reads a file in chunks. public static DataObject ReadNextFile(){ ...} And dataobject looks like this: public DataObject { public string Category { get; set; } // And other members ... } What I want to do is the following basically List<DataObject> dataObjects = new List<DataObject>(); while(ReadNextFile().Category == "category") { dataObjects.Add(^^^^^ the thingy in the while); } I know it's probably not how it's done, because how do I access the object I've just read. I think what you're looking for is: List<DataObject> dataObjects = new List<DataObject>(); DataObject