Questions tagged [logic]

Logic refers to the ultimate flow of your code and how you arrive your desired solution. Questions should relate to finding a coding solution (or improving existing coding logic) to a given problem. Please use with an appropriate language tag, a thorough description of your logic, and the relevant code you're working on. General logic questions are off-topic. If you simply need a code review, consider https://codereview.stackexchange.com

Note that the bulk of questions tagged concern the broad sense of program logic described in the excerpt above. Before using the tag, consider if a tag about a more specialised logic-related topic would be a better fit. Examples include:

Furthermore, if your question is about one of those specialised topics, consider whether it essentially involves programming in some way. If it doesn't, it will likely be more appropriately posted elsewhere in the network. Some sites in which such questions might be a good fit, depending on the circumstances of each concrete case, are Philosophy, Mathematics and Computer Science.

8192 questions
4722
votes
20 answers

Reference — What does this symbol mean in PHP?

What is this? This is a collection of questions that come up every now and then about syntax in PHP. This is also a Community Wiki, so everyone is invited to participate in maintaining this list. Why is this? It used to be hard to find questions…
Gordon
  • 296,205
  • 68
  • 508
  • 534
1969
votes
14 answers

What is the optimal algorithm for the game 2048?

I have recently stumbled upon the game 2048. You merge similar tiles by moving them in any of the four directions to make "bigger" tiles. After each move, a new tile appears at random empty position with a value of either 2 or 4. The game terminates…
nitish712
  • 18,496
  • 5
  • 23
  • 33
293
votes
6 answers

Are || and ! operators sufficient to make every possible logical expression?

The logical expression ( a && b ) (both a and b have boolean values) can be written like !(!a || !b), for example. Doesn't this mean that && is "unneccesary"? Does this mean that all logical expressions can be made only using || and !?
JakeTheSnake
  • 2,364
  • 3
  • 11
  • 24
129
votes
14 answers

Solving "Who owns the Zebra" programmatically?

Edit: this puzzle is also known as "Einstein's Riddle" The Who owns the Zebra (you can try the online version here) is an example of a classic set of puzzles and I bet that most people on Stack Overflow can solve it with pen and paper. But what…
117
votes
3 answers

Type-juggling and (strict) greater/lesser-than comparisons in PHP

PHP is famous for its type-juggling. I must admit it puzzles me, and I'm having a hard time to find out basic logical/fundamental things in comparisons. For example: If $a > $b is true and $b > $c is true, must it mean that $a > $c is always true…
hakre
  • 178,314
  • 47
  • 389
  • 754
111
votes
2 answers

Simple 'if' or logic statement in Python

How would you write the following in Python? if key < 1 or key > 34: I've tried every way I can think of and am finding it very frustrating.
Zak
  • 1,211
  • 2
  • 8
  • 5
105
votes
4 answers

Why does this if statement, with an assignment and equality check, evaluate to false?

How does a Java if statement work when it has an assignment and an equality check OR-d together?? public static void test() { boolean test1 = true; if (test1 = false || test1 == false) { System.out.println("TRUE"); } else { …
RoHaN
  • 1,296
  • 2
  • 11
  • 19
92
votes
9 answers

Is ((a + (b & 255)) & 255) the same as ((a + b) & 255)?

I was browsing some C++ code, and found something like this: (a + (b & 255)) & 255 The double AND annoyed me, so I thought of: (a + b) & 255 (a and b are 32-bit unsigned integers) I quickly wrote a test script (JS) to confirm my theory: for (var…
Martin
  • 1,065
  • 7
  • 13
88
votes
2 answers

What is the combinatory logic equivalent of intuitionistic type theory?

I recently completed a university course which featured Haskell and Agda (a dependent typed functional programming language), and was wondering if it was possible to replace lambda calculus in these with combinatory logic. With Haskell this seems…
grasevski
  • 2,429
  • 1
  • 19
  • 21
84
votes
7 answers

Why if (n & -n) == n then n is a power of 2?

Line 294 of java.util.Random source says if ((n & -n) == n) // i.e., n is a power of 2 // rest of the code Why is this?
David Weng
  • 3,985
  • 12
  • 38
  • 49
81
votes
10 answers

JQuery .hasClass for multiple values in an if statement

I have a simple if statement as such: if ($('html').hasClass('m320')) { // do stuff } This works as expected. However, I want to add more classes to the if statement to check if any of the classes are present in the tag. I need it so it's…
Danny Englander
  • 1,909
  • 4
  • 25
  • 37
73
votes
5 answers

Why does javascript accept commas in if statements?

I stumbled across some javascript syntax that seemed like it should produce a parse error of some kind but doesn't: if (true, true) {console.log('splendid')} else {console.log('horrid')} // splendid if (true, false) {console.log('splendid')} else…
Matt
  • 1,950
  • 3
  • 15
  • 18
69
votes
6 answers

In Java, what are the boolean "order of operations"?

Let's take a simple example of an object Cat. I want to be sure the "not null" cat is either orange or grey. if(cat != null && cat.getColor() == "orange" || cat.getColor() == "grey") { //do stuff } I believe AND comes first, then the OR. I'm…
Stephano
  • 5,368
  • 6
  • 36
  • 57
68
votes
15 answers

Looking for simple rules-engine library in .NET

Does anyone know of a good .NET library rules library (ideally open-source)? I need something that can do nested logic expressions, e.g., (A AND B) AND (B OR C OR D). I need to do comparisons of object properties, e.g., A.P1 AND B.P1. (Ideally, I…
Kurtz
  • 681
  • 1
  • 6
  • 5
68
votes
8 answers

General rules for simplifying SQL statements

I'm looking for some "inference rules" (similar to set operation rules or logic rules) which I can use to reduce a SQL query in complexity or size. Does there exist something like that? Any papers, any tools? Any equivalencies that you found on…
MicSim
  • 25,056
  • 15
  • 84
  • 124
1
2 3
99 100