while-loops

C#: trying a simple project

Hello, I am new to C#. I wanted to do a simple program with some type of loops. I wanted my program to loop through the numbers that the user enters and if it is less than a number then write keep guessing,but once they enter the number 25 i wanted it to say Merry Christmas.. Please Help int number; do { Console.WriteLine("Guess...

Writing a while loop in the C preprocessor

I am asking this question from an educational/hacking point of view, (I wouldn't really want to code like this). Is it possible to implement a while loop only using C preprocessor directives. I understand that macros cannot be expanded recursively, so how would this be accomplished? Thanks ...

The correct way of waiting for strings to become equal

In a Swing app a method should continue only after user enters a correct answer. The correct answer is stored in a String with user answer being set by a listener to another String. So, the code is while (!correctAnswer.equals(currentAnswer)) { // wait for user to click the button with the correct answer typed into the textfiel...

is while(true) bad programming practice?

I often use this code pattern: while(true) { //do something if(<some condition>) { break; } } Another programmer told me that this was bad practice and that I should replace it with the more standard: while(!<some condition>) { //do something } His reasoning was that you could "forget the break" too easily...

Is using a while block to do nothing a bad thing?

I'm currently working through the excercises in 'The C Programming Language'. Here's one of my solutions: int c; while ((c=getchar()) != EOF) { if (c == ' ') { while ((c = getchar()) == ' ') {} // do nothing? putchar(' '); } putchar(c); } I found some solutions here that are quite different to mine and use an ext...

T-SQL While Loop and concatenation

I have a SQL query that is supposed to pull out a record and concat each to a string, then output that string. The important part of the query is below. DECLARE @counter int; SET @counter = 1; DECLARE @tempID varchar(50); SET @tempID = ''; DECLARE @tempCat varchar(255); SET @tempCat = ''; DECLARE @tempCatString varchar(5000); SET @t...

get pointer position in a php while mysql_fetch_array function

I've got a fairly standard while mysql_fetch_array statement in php, and I'm trying to figure out which row in the result set is being printed. I figured this should be pretty simple, but I've put a fairly standard $i=0; $count=mysql_num_rows($getResults); while($resultArray=mysql_fetch_array($getResults)){ $i++ if($i==$count){ ech...

How to do a multiple keyword search?

I have 2 tables to search. Searching photos for keywords, title and description. The keywords have been split off into a separate table. My advanced search will allow searching on all 3 but the basic will just be the keyword table. Basic table setup: PHOTO Table PhotoID Name Title Description WORD2PHOTO Table WordID PhotoID Word ...

What's the difference between iterating over a file with foreach or while in Perl?

I have a filehandle FILE in Perl, and I want to iterate over all the lines in the file. Is there a difference between the following? while (<FILE>) { # do something } and foreach (<FILE>) { # do something } ...

Which is better practice - for loop with break or conditional loop?

I'm just curious what peoples' thoughts are on this topic. Let's say I have an Array of Objects, and I want to loop through them to see if the Objects contain certain values, and if so, I want to stop the loop. Which is better practice - a for loop with a break, or a conditional loop? The pseudo-code in the example I have provided is fo...

How do you loop through a string in Ruby?

Pretty simple question from a first-time Ruby programmer. How do you loop through a slab of text in Ruby? Everytime a newline is met, I want to re-start the inner-loop. def parse(input) ... end ...

Zend Studio reports warning: Assignment in condition. Is this so bad?

I have recently started using Zend Studio which has reported as warning the following type of code: $q = query("select * from some_table where some_condition"); while ($f = fetch($q)) { // some inner workings } To stop the warning the code needs to be written like this: $q = query("select * from some_table where some_condition"); $...

Ruby: undefined method `>'

I just started learning Ruby and I ran into a problem today. numResults = /\d+/.match(ie.div(:id, 'results_label').text) puts "Results found: "+numResults.to_s while(numResults > 0) . . some more code . I get this error in my output: Exception: undefined method `>' for #<MatchData:0x424c6d4> Which is really strange because I mad...

do-while loop in Python?

I need to emulate a do-while loop in a python. But, unfortunately, following straightforward code does not work: l = [ 1, 2, 3 ] i = l.__iter__() s = None while True : if s : print s try : s = i.next() except StopIteration : break print "done" Instead of "1,2,3,done" I have the following output: [stdout:]1 [stdout:]...

Possible to use a while loop within a loop?

Howdy Guys, What I am trying to do is the following: I have an array of values, these values will eventually be used to generate a random unique string but that's a little later. First I would like to loop through all the values in the array (foreach loop) then I would like to limit this (while loop) Is this a correct method for doing t...

Simple iterator looping gives unexpected result

I am sure it's an obvious error with my logic, but I can't seem to work out what I am doing wrong. Quite simply I have an array list of security codes, I want to compute the correlation between each combination of security codes. My code is as follows: private void crossCorr(ArrayList<String> codes, HashMap<String, Stock> stockMap){ /...

Is there any performance difference between for() and while()?

Or is it all about semantics? ...

C pointer arithmetic snippet

I have a program that I'm trying to decode. It is translated to C from another language (whose name is not spoken here), and as I want to understand how it works, I am slowly rewriting the code and simplifying it to use all the nice logical constructs C has to offer. The following little bit keeps popping up in my code, with varying val...

Mysql too many while loops inside one stored procedure

Hi! I have one stored procedure in while I am getting comma separated values in parameters. I have three parameters which has comma separated values. and i need to put them in table's columns so I am using while loop. but i am scared when too many(say lakhs of users) users will connect to my website then my procedure will have performanc...

creating a unique key - most efficient way

I have two functions, makeKey() and keyExists(). makeKey() simply generates a 5 digit random alphanumeric key, keyExists() accepts this key as its only argument, and looks up in a table, returning true/false depending on whether it exists. I need to do something very simple but I cannot figure out the quickest way of doing it. I just...