logical-operators

relational operator expression order

This is probably a silly question, but curiosity has gotten the better of me. I've been seeing code lately that seems to "reverse" the order of expressions for relational operators e.g.: if (0 == someVariable) As opposed to what I normally see/write: if (someVariable == 0) To me, the second method seems more readable and intuitive,...

How to avoid short-circuit evaluation on

I'm working with Ruby on Rails and would like to validate two different models : if (model1.valid? && model2.valid?) ... end However, "&&" operator uses short-circuit evaluation (i.e. it evaluates "model2.valid?" only if "model1.valid?" is true), which prevents model2.valids to be executed if model1 is not valid. Is there an equivale...

How to do this query in mysql?

SELECT item_name from items WHERE item_id = $var; I tried: $var = 001 || 002 || 003 || 004; $var = 001 OR 002 OR 003 OR 004; $var = 001 or 002 or 003 or 004; But all do not work. Thanks, i try that, but the output only 1 result => 1. What I want is to output all, i.e. 1, 2 , 3 and 4.. Means, I want to select multiple records(rows...

A clear, layman's explanation of the difference between | and || in c# ?

Ok, so I've read about this a number of times, but I'm yet to hear a clear, easy to understand (and memorable) way to learn the difference between: if (x | y) and if (x || y) ..within the context of C#. Can anyone please help me learn this basic truth, and how C# specifically, treats them differently (because they seem to do the ...

OR Operator in c#

Can I achieve if(a == "b" || "c") instead of if(a == "b" || a== "c") ...

What is wrong with the short circuit logic in this Java code?

Why doesn't func3 get executed in the program below? After func1, func2 doesn't need to get evaluated but for func3, shouldn't it? if (func1() || func2() && func3()) { System.out.println("true"); } else { System.out.println("false"); } } public static boolean func1() { System.out.println("func1"); return...

C Preprocessor directives and boolean operators

Hello all, I searched the site but did not find the answer I was looking for so here is a really quick question. I am trying to do something like that : #ifdef _WIN32 || _WIN64 #include <conio.h> #endif How can I do such a thing? I know that _WIN32 is defined for both 32 and 64 bit windows so I would be okay with just it for win...

Performance difference in for loop condition?

Hello all, I have a simple question that I am posing mostly for my curiousity. What are the differences between these two lines of code? (in C++) for(int i = 0; i < N, N > 0; i++) for(int i = 0; i < N && N > 0; i++) The selection of the conditions is completely arbitrary, I'm just interested in the differences between , and &&. I'm...

In php, how do logical operators work _with non-booleans_?

In other programming languages (Python, Ruby, Scheme), I'm used to doing things like $foo = $cat && $dog; $bar = $fruit || $vegetable; I would expect that $foo would get assigned to $dog if $cat were null, and $bar to $fruit if $fruit were NOT null. I seem to recall getting burned for doing things like this in PHP, and I've never lear...

Explain the following from Accelerated C++ please

I don't understand the following excerpt from Accelerated C++: Starting at Because || is left-associative, and because of the relative precedence of ||,== ,and -, r == 0 || r == rows - 1 || c == 0 || c == cols - 1 means the same as it would if we were to place all of its subexpressions in parentheses: ((r == 0 |...

PHP: 'or' statement on instruction fail: how to throw a new exception?

Everyone here should know the 'or' statemens, usually glued to an die() command: $foo = bar() or die('Error: bar function return false.'); The most of the times we see something like: mysql_query('SELECT ...') or die('Error in during the query'); However, i cant understand how exactly that 'or' statement works. I would like to thr...

SQL Logic Operator Precedence: And and Or

Are the two statements below equivalent? SELECT [...] FROM [...] WHERE some_col in (1,2,3,4,5) AND some_other_expr and SELECT [...] FROM [...] WHERE some_col in (1,2,3) or some_col in (4,5) AND some_other_expr Is there some sort of truth table I could use to verify this? Thanks. ...

What's the difference between & and && in MATLAB?

What is the difference between the & and && logical operators in MATLAB? ...

Is there any wisdom behind "and", "or" operators in Ruby ?

I wonder why ruby give and, or less precedence than &&, || , and assign operator? Is there any reason? ...

Is there an Non-Short circuited logical "and" in C++?

tl;dr: Is there a non-short circuit logical AND in C++ (similar to &&)? I've got 2 functions that I want to call, and use the return values to figure out the return value of a 3rd composite function. The issue is that I always want both functions to evaluate (as they output log information about the state of the system) IE: bool Func...

Java short circuit evaluation

I thought Java had short circuit evaluation, yet this line is still throwing a null pointer exception: if( (perfectAgent != null) && (perfectAgent.getAddress().equals(entry.getKey())) ) { In this case perfectAgent is null, so I just want the whole expression to return false, but my app is still crashing on this line with a NullPointer...

Logical value of an assignment in C

while (curr_data[1] != (unsigned int)NULL && ((curr_ptr = (void*)curr_data[1]) || 1)) Two part question. What will (curr_ptr = (void*)curr_data[1]) evaluate to, logically. TRUE? Also, I know its rather hack-ish, but is the while statement legal C? I would have to go through great contortions to put the assignment elsewhere in the...

Implementating a logical parser in django-query

This is going to be a "long one". I'm including as much code and explanation as possible ... I'm not above throwing out code if necessary. I'm trying to implement a logical parser in a django query system. Where users can provide complex queries against tags that are applied to samples. This is essentially part of a scientific sample...

Javascript question

(myVar && foo()) what does the above code mean? what is it equivalent to? this is an inline code i think it runs on a single line ...

Which side (left or right) of && (and) operator evaluated in C++

Which order is the and && operator evaluated For example the following code if (int alpha = value1-value2 && alpha > 0.001) //do something threw an exception that alpha is being used without being initiated. I thought the expression left of the && would always initiate the value of alpha first, but it seems I may be wrong Any id...