condition

Why would you use an assignment in a condition?

In many languages assignments are legal in conditions. I never understood the reason behind this. Why would you write: if (var1 = var2) { ... } instead of: var1 = var2; if (var1) { ... } ...

Remove repetitive, hard coded loops and conditions in C#

I have a class that compares 2 instances of the same objects, and generates a list of their differences. This is done by looping through the key collections and filling a set of other collections with a list of what has changed (this may make more sense after viewing the code below). This works, and generates an object that lets me kno...

Java Concurrency Techniques

Here are two chunks of code that accomplish (what I think is) the same thing. I basically am trying to learn how to use Java 1.5's concurrency to get away from Thread.sleep(long). The first example uses ReentrantLock, and the second example uses CountDownLatch. The jist of what I am trying to do is put one thread to sleep until a cond...

Thinking Sphinx indexing non-expired records.

What would be the best approach to have Thinking Sphinx only index records where Today's date is less than or equal to the record's "expires_at" field? Thanks in advance. ...

Condition evaluation in loops ?

string strLine;//not constant int index = 0; while(index < strLine.length()){//strLine is not modified}; how many times strLine.length() is evaluated do we need to put use nLength with nLength assigned to strLine.length() just before loop ...

Which is the preferred condition in loop?

int n = 5; for(int i = 0;i!=n;i++)//condition != { //executing 5times } int n = 5; for(int i = 0;i<n;i++)//condition < { //executing 5times } Which one is preferred? This was example from "Accelerated C++ : practical programming by example / Andrew Koenig, Barbara E. Moo." Just wanted to know why the Author prefers the fi...

boost thread compiler error with GCC

Hello all, on linux, gcc 4.3, compiling a class with boost::thread implementation and mutexes / condition variables I get the following strange error, apparently due to type conflicts with the posix thread library: *Compiling: filter.cpp /usr/include/boost/thread/condition.hpp: In member function »void boost::condition::wait(L&) [with ...

Spurious unblocking in boost thread

I came across this interesting paragraph in the Boost thread documentation today: void wait(boost::unique_lock<boost::mutex>& lock) ... Effects: Atomically call lock.unlock() and blocks the current thread. The thread will unblock when notified by a call to this->notify_one() or this->notify_all(), or spuriously. When the...

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"); $...

How to write 'if' without using 'then' or 'end' in Ruby

I've found three ways to write the same condition in Ruby: #1 if 1==1 puts "true" end #2 puts "true" if 1==1 #3 if 1==1 then puts "true" end Why can't I do this? #4 if 1==1 puts "true" I don't understand: (1) Why then and end are needed in #3, and, (2) Why I need to change the order to get #2 to work. Statement #4 seems...

How to execute an Installshield Custom action while Rollback?

I have an InstallShield installer which does some stuff. In case the installation breaks the rollback sequence gets started. I do know that I can create conditions for my custom actions in order to make it run only during install or uninstall, but which condition do I set to make it run on rollback? To be precisely I need rollback and ...

Warning: Assignment in condition

One thing that has always bugged me is that when checking my php scripts for problems I get the warning "bool-assign : Assignment in condition" and I get them a lot. e.g.: $guests = array(); $sql = "SELECT * FROM `guestlist`"; $result = mysql_query($sql); while($row = mysql_fetch_assoc($result)) $guests[] = $row['name']; Is there ...

Speed of boolean expression (C#)

Hello I was thinking of what is better to write (in matter of speed and/or efficiency): bool Method(...) { ... } ... bool result = Method(...); if (result == false) { ... } // or if (!result) { ... } (or variation with true and if(result)) I'm asking because I use first one (result == false) but sometimes it gets very long, espec...

Order of condition checking?

(EDIT: Duplicate of "Why does one often see “null != variable” instead of “variable != null” in C#?". Even if C# isn't the language in question, the answers to that question give the reason one might want to do it in some languages.) This is a somewhat philosophical question. However,... maybe there is a technical answer. I don't know. ...

MySQL : selecting the X smallest values

Hi, Let be a table like this : CREATE TABLE `amoreAgentTST01` ( `moname` char(64) NOT NULL DEFAULT '', `updatetime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, `data` longblob, PRIMARY KEY (`moname`,`updatetime`) I have a query to find the oldest records for each distinct 'moname', but only if t...

Get a unique list/tuple element given a condition in python

Hi! How do I get a tuple/list element given a condition in python? This occurs pretty often and I am looking for a nice-few-lines-pythonic way of doing this. here could be an example: Consider a tuple containing 2D points coordinates like this: points = [[x1, y1],[x2, y2],[x3, y3], ...] And I would like to get the point that minimi...

Condition Synchronization

Please could someone explain condition synchronization to me? Having a hard time finding an example that explains itself well enough due to rubbish lecturing material. An example would be greatly appreciated also - preferably in C# Kind regards ...

Does ActiveRecord support dynamic conditions in sql statements?

I want to make a SQL query in a Rails app that adds a condition when a drop-down menu is added. Obviously I don't want to search with a blank condition if the drop down is not selected. How can I develop a dynamic condition in an SQL statement? untested example: When dropdown is not selected: Object.find("billy," :conditions => {}) Wh...

MySQL SELECT with a condition help

CREATE TABLE `comments` ( `comment_id` int(11) NOT NULL AUTO_INCREMENT, `comment_parent_id` int(11) NOT NULL DEFAULT '0', `user_id` int(11) NOT NULL DEFAULT '0', `comment_text` varchar(200) NOT NULL DEFAULT '', `comment_created` int(20) NOT NULL DEFAULT '0', `comment_updated` int(20) NOT NULL DEFAULT '0', `comment_replies_c...

unix condition checking!

what condition is being checked below? if [[ ! -s ${FILE} || -z ${FILE} ]] (here $FILE is a data file) ...