while-loop

How do I convert this for loop into a while loop? [closed]

点点圈 提交于 2019-12-06 13:40:16
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 3 years ago . I am creating an array program. I am practicing how to convert for loops to while loops, but I cannot grasp the concept. If I have the for loop: int [] list = new int [5]; for (int i = 0; i < 5; i++) { list [i] = i + 2; } How would I make it a while loop? Here's my attempt int [] list = new int [5]; int i = 0;

Comparing char in a while loop

廉价感情. 提交于 2019-12-06 12:41:39
问题 As long as the input is not x, the loop will continue to ask for the input and it prints out either A or B. int main (void){ char input; while( input != 'x'){ printf("Enter Input:"); scanf("%c", &input); if (input == 'a'){ printf("A \n"); } else{ printf("B\n"); } } return (0); } The problem is that everytime after i entered the input, it prints the output and it also prints out "Enter Input:B" in a new line no matter i entered a or b or anything else as input. Can anyone tell me how can i

BufferedReader readLine used in while loop

久未见 提交于 2019-12-06 12:02:53
I have seen BufferedReader using while loops to iterate through the contents of a file, with code more or less like: try { FileReader fr = new FileReader(file); Buffered Reader br = new BufferedReader(fr); String line; while ( (line = br.readLine()) != null ) { // do something } } catch () {} what I don't understand is how the while loop is internally incrementing its counter until it has read all lines in the document. To me, the while loop above means "if the first line (line[0]) is not null, do something (presumably an infinite number of times)", and never advancing past the first line of

How to run a while loop as long as there is no error [duplicate]

社会主义新天地 提交于 2019-12-06 11:36:13
问题 This question already has answers here : Asking the user for input until they give a valid response (18 answers) Closed 4 years ago . I am running a code that might get an error, but I want to run it as long as there is no error. I thought about something like that: while ValueError: try: x = int(input("Write a number: ")) except ValueError: x = int(input("You must write a number: "))` 回答1: You were quite near while True: try: x = int(input("Write a number: ")) break except ValueError: print(

sql while loop with date counter

喜夏-厌秋 提交于 2019-12-06 11:09:26
问题 I need to do something like this in sql: declare @StartDate varchar(10) declare @EndDate varchar(10) set @StartDate='12/31/2008' set @EndDate='1/11/2009' Declare @date varchar = @StartDate while (@date <= @EndDate) begin -- some statements set @date += 1 -- basically increment by 1 day end How can I do the above correctly in SQL? Basically, my startdate and enddate are strings and not datetimes because my business logic is referencing string column in another table with a date as the name of

The difference between while and do while C++? [duplicate]

≯℡__Kan透↙ 提交于 2019-12-06 10:53:08
问题 This question already has answers here : 'do…while' vs. 'while' (27 answers) Closed 5 years ago . I would like someone to explain the difference between a while and a do while in C++ I just started learning C++ and with this code I seem to get the same output: int number =0; while (number<10) { cout << number << endl; number++ } and this code: int number=0; do { cout << number << endl; number++ } while (number<10); The output is both the same in these both calculations. So there seem to be no

php echo table with while loop

一笑奈何 提交于 2019-12-06 10:06:36
问题 I am creating a table and want it laid out a certain way and have to retrieve the data from the DB. I am trying to get it set up where it has the usernames across the top example... Jim Chris Allen Rick 7 8 4 5 my code looks like this and I have been messing around with it for hours and cant figure out why I cant get it set up how I want. Any help would be appreciated. Its a while loop. while ($pickresults= mysql_fetch_assoc($picksquery)) { //first row echo '<th> '.$pickresults['username'].'

Running multiple while true loops independently in python

[亡魂溺海] 提交于 2019-12-06 10:00:29
问题 Essentially I have 2 " while True: " loops in my code. Both of the loops are right at the end. However when I run the code, only the first while True: loop gets run, and the second one gets ignored. For example: while True: print "hi" while True: print "bye" Here, it will continuously print hi, but wont print bye at all (the actual code has a tracer.execute() for one loop, and the other is listening to a port, and they both work on their own). Is there any way to get both loops to work at the

How to concatenate all lines from a file in Bash? [duplicate]

狂风中的少年 提交于 2019-12-06 08:57:32
This question already has answers here : How to concatenate multiple lines of output to one line? (7 answers) Closed last year . I have a file csv : data1,data2,data2 data3,data4,data5 data6,data7,data8 I want to convert it to (Contained in a variable): variable = data1,data2,data2%0D%0Adata3,data4,data5%0D%0Adata6,data7,data8 My attempt : data='' cat csv | while read line do data="${data}%0D%0A${line}" done echo $data # Fails, since data remains empty (loop emulates a sub-shell and looses data) Please help.. In bash, data=$( while read line do echo -n "%0D%0A${line}" done < csv) In non-bash

Making pairs of words based on one column

两盒软妹~` 提交于 2019-12-06 08:44:43
I want to make pairs of words based on the third column (identifier). My file is similar to this example: A ID.1 B ID.2 C ID.1 D ID.1 E ID.2 F ID.3 The result I want is: A C ID.1 A D ID.1 B E ID.2 C D ID.1 Note that I don't want to obtain the same word pair in the opposite order. In my real file some words appear more than one time with different identifiers. I tried this code which works well but requires a lot of time (and I don't know if there are redundancies): counter=2 cat filtered_go_annotation.txt | while read f1 f2; do tail -n +$counter go_annotation.txt | grep $f2 | awk '{print "'$f1