logical-operators

PHP Logical Operators precedence affects variable assignment results strangely

北城余情 提交于 2019-12-02 16:16:25
问题 $var4 = 123; function fn1($p1) { return array('p1' => 1, 'p2' => 2); } if ($var1 = fn1(1) AND $var4 == 123) { print_r($var1); } if ($var2 = fn1(1) && $var4 == 123) { print_r($var2); } if (($var3 = fn1(1)) && $var4 == 123) { print_r($var3); } If you run this simple script it will output strange results, at least for me!! First output from first if expression will result in an array returned from the function & assigned to the $var1 variable, which is what I'm expecting, well? Second output

how to use NOT operator for integers in JAVA

我怕爱的太早我们不能终老 提交于 2019-12-02 13:35:06
how to use NOT operator for integers in JAVA when i put NOT operator (!) it shows an error package com.learnJava.first; public class LogicalOpTable { public static void main(String[] args) { int p,q; System.out.println("P\t Q\t AND\t OR\t XOR\t NOT\n"); p = 1; q = 1; System.out.println(p+ "\t " + q + "\t " + (p&q) + "\t " + (p|q) + "\t " + (p^q) + "\t " + !p ); p = 1; q = 0; System.out.println(p + "\t " + q + "\t " + (p&q) + "\t " + (p|q) + "\t " + (p^q) + "\t " + !p); p = 0; q = 1; System.out.println(p + "\t " + q + "\t " + (p&q) + "\t " + (p|q) + "\t " + (p^q) + "\t " + !p); p = 0; q = 0;

Logical Operators (AND Operators)

浪子不回头ぞ 提交于 2019-12-02 12:37:58
I came across the following logical operators workaround but could not comprehend the logic behind: console.log(1 && 2) will get you 2 console.log(false && X) will get you false console.log(true && X) will get you UncaughtReferenceError:X is not defined Anyone can explain the answer? stybl Look at the documentation for the && operator: && ; Returns expr1 if it can be converted to false; otherwise, returns expr2. Thus, when used with Boolean values, && returns true if both operands are true; otherwise, returns false. In the first example, you provide the numbers 1 and 2 as the operands. 1

IF statement with logical OR [duplicate]

寵の児 提交于 2019-12-02 11:35:08
This question already has an answer here: Can you use 2 or more OR conditions in an if statement? 6 answers if(1 == 2 || 4) { cout<<"True"; } else { cout<<"False"; } This is how I read the above. If 1 is equal to 2 or 4, then print true. Otherwise, print false. When this is executed though... true is printed. Obviously I'm misunderstanding something here. 1 is not equal to 2 or 4. Wouldn't that make it false? Yeah, I've made the same mistake. Read the sentence again: If 1 is equal to 2 or 4, then print true. The "2" and "4" both refer to the "If 1 is equal to [...]." That means, the sentence

Compare character with multiple characters in C

浪尽此生 提交于 2019-12-02 11:17:22
How can i compare a character in C with other characters without using an 'if' with tons of '||'? For example let's say I have a character named 'i' that I want to compare with 8 other characters that have no connection between them whatsoever, and if 'i' equals to at least one of those 8 characters then the expression is true. Something like this: if(i == c1 || i == c2 || i == c2 ........){ /* do stuff */} But on a big application these comparisons are a lot, not just 3 or 8. Is there a smart and fast way to achieve something like this and not end up with ugly looking code? Thank you in

What is the difference between bitwise and logical operators inside conditional statements in C?

こ雲淡風輕ζ 提交于 2019-12-02 10:06:02
There are many questions on the net that refer to the differences between bitwise and logical operators. Hoping that I have done a good search, none of them specialize to whether they are the same or not when used inside conditional statements nor refer exclusively to C Language. The majority referred to C++ and C# and I do not know if the same answers were applicable to C Language too. This is an example code I wrote to test what is going on: // Difference between logical && and bitwise & // #include <stdio.h> #define TRUE 123>45 #define FALSE 4>2342 void print_tt(int table[][4]); int main

PHP Logical Operators precedence affects variable assignment results strangely

北慕城南 提交于 2019-12-02 09:08:30
$var4 = 123; function fn1($p1) { return array('p1' => 1, 'p2' => 2); } if ($var1 = fn1(1) AND $var4 == 123) { print_r($var1); } if ($var2 = fn1(1) && $var4 == 123) { print_r($var2); } if (($var3 = fn1(1)) && $var4 == 123) { print_r($var3); } If you run this simple script it will output strange results, at least for me!! First output from first if expression will result in an array returned from the function & assigned to the $var1 variable, which is what I'm expecting, well? Second output from second if expression will result in an integer '1' assigned to the $var2 variable, which is NOT

Does C use short circuit evaluation even when arguments are function calls?

有些话、适合烂在心里 提交于 2019-12-02 07:52:03
问题 I know that logical operators do short-circuit checking. That is, if there is a statement like A && B && C , then if A is false, B and C are not evaluated. But is this also true in cases where B and C are function calls? For example, the return statement in this code: bool areIdentical(struct node * root1, struct node *root2) { /* base cases */ if(root1 == NULL && root2 == NULL) return true; if(root1 == NULL || root2 == NULL) return false; /* Check if the data of both roots is same and data

Does C use short circuit evaluation even when arguments are function calls?

走远了吗. 提交于 2019-12-02 06:35:22
I know that logical operators do short-circuit checking. That is, if there is a statement like A && B && C , then if A is false, B and C are not evaluated. But is this also true in cases where B and C are function calls? For example, the return statement in this code: bool areIdentical(struct node * root1, struct node *root2) { /* base cases */ if(root1 == NULL && root2 == NULL) return true; if(root1 == NULL || root2 == NULL) return false; /* Check if the data of both roots is same and data of left and right subtrees are also same */ return (root1->data == root2->data && //I am talking about

What's the value of short-circuit of Python?

ⅰ亾dé卋堺 提交于 2019-12-02 04:19:33
I'm learning the book named Data Structures & Algorithms in Python . On Page 12 that introduce the Logical Operators , it writes: The and and or operators short-circuit , (I thinks it should add is called ), in that they do not evaluate the second operand if the result can be determined based on the value of the first operand. This feature is useful when constructing Boolean expressions in which we first test that a certain condition holds (such as a reference not being None), and then test a condition that could have otherwise generated an error condition had the prior test not succeeded . I