Is boolean expression && statement() the same as if(boolean expression) statement()?
Strictly speaking, they will produce the same results, but if you use the former case as a condition for something else, you will get dissimilar results. This is because in the case of x && doSomething()
, doSomething()
will return a value to signify its success.
&& (AND) and || (OR) in IF statements
No, it will not be evaluated. And this is very useful. For example, if you need to test whether a String is not null or empty, you can write:
if (str != null && !str.isEmpty()) {
doSomethingWith(str.charAt(0));
}
or, the other way around
if (str == null || str.isEmpty()) {
complainAboutUnusableString();
} else {
doSomethingWith(str.charAt(0));
}
If we didn't have 'short-circuits' in Java, we'd receive a lot of NullPointerExceptions in the above lines of code.
Difference between if (foo) bar(); and foo && bar();
The second form is known as short-circuit evaluation and results in exactly the same as the first form. However the first form is more readable and should be preferred for maintainability.
This type of short-cuircuit evaluation is often seen in if-statements, where the right hand is conditionally evaluated. See the example below; bar is only evaluated if foo evaluates to true.
if (foo && bar()) {
// ...
}
Javascript && operator as if statement?
Yes, you can do it. If a
is less than b
the other statements are going to execute.
That said, you probably shouldn't do it. Rather than trying to save yourself a small amount of typing you should instead go with a more readable piece of code whose purpose is immediately obvious. After all, if you needed to check that you weren't misinterpreting it, other programmers who may have to read or modify your code could have the same problem.
There is also a pitfall with this if you were trying to do multiple assignments, but one of the middle assignments was for a "falsey" value. Consider this example:
a < b && (a = 1) && (b = 0) && (c = 3);
Essentially, if a
is less than b
, set a
to 1, b
to 0 and c
to 3. However, the (b = 0)
returns false (0 is considered false when converted to a boolean), so the (c = 3)
part never executes. Obviously with that basic example anybody who is reasonably knowledgeable about JavaScript could work out that it will fail, but if the values for a
, b
and c
were coming from elsewhere there'd be no immediate indication that it wouldn't always work.
React showing 0 instead of nothing with short-circuit (&&) conditional component
Since your condition is falsy and so doesn't return the second argument (<GeneralLoader />
), it will return profileTypesLoading
, which is a number, so react will render it because React skips rendering for anything that is typeof
boolean
or undefined
and will render anything that is typeof
string
or number
:
To make it safe, you can either use a ternary expression {condition ? <Component /> : null}
or boolean cast your condition like {!!condition && <Component />}
What does expression && expression syntax mean?
It's shorthand for
if (parent) {
this.parent.next = this
}
What is the OR operator in an IF statement
||
is the conditional OR operator in C#
You probably had a hard time finding it because it's difficult to search for something whose name you don't know. Next time try doing a Google search for "C# Operators" and look at the logical operators.
Here is a list of C# operators.
My code is:
if (title == "User greeting" || "User name") {do stuff};
and my error is:
Error 1 Operator '||' cannot be
applied to operands of type 'bool' and
'string' C:\Documents and Settings\Sky
View Barns\My Documents\Visual Studio
2005\Projects\FOL Ministry\FOL
Ministry\Downloader.cs 63 21 FOL
Ministry
You need to do this instead:
if (title == "User greeting" || title == "User name") {do stuff};
The OR operator evaluates the expressions on both sides the same way. In your example, you are operating on the expression title == "User greeting"
(a bool) and the expression "User name"
(a string). These can't be combined directly without a cast or conversion, which is why you're getting the error.
In addition, it is worth noting that the ||
operator uses "short-circuit evaluation". This means that if the first expression evaluates to true
, the second expression is not evaluated because it doesn't have to be - the end result will always be true
. Sometimes you can take advantage of this during optimization.
One last quick note - I often write my conditionals with nested parentheses like this:
if ((title == "User greeting") || (title == "User name")) {do stuff};
This way I can control precedence and don't have to worry about the order of operations. It's probably overkill here, but it's especially useful when the logic gets complicated.
If with multiple &&, || conditions Evaluation in java
If I understand you correctly, in the first part of your question, you are asking whether a
and b
must both be true for the entire expression (a && b || c || d || e)
to evaluate as true.
The answer to that question is no. Java operators are such that &&
has higher precedence than ||
. So the equivalent expression is:
(a && b) || c || d || e
Therefore the expression as a whole will evaluate to true if any of a && b
or c
or d
or e
is true. For example, if a
was false and c
was true, the expression as a whole is true.
Note that Java conditional operators short-circuit. This means that once the end result of the expression is known, evaluation stops. For example, if a
and b
were both true, meaning the overall expression must evaluate to true, then c
, d
and e
are not evaluated.
For the second part of your question, we can apply the same logic. The equivalent expression is therefore:
(a && b) || (a && c) || (a && d) || (a && e)
This will evaluate to true if any of the sub-components, eg. a && c
evaluates to true. As others have noted, a
must be true for the expression to be true as it is part of every sub-component. Then if any of the other variables is true, the expression is true.
Once you understand that, you can see how the simplification of this expression suggested by @Arc676 is arrived at:
a && (b || c || d || e)
I should also add that the two expressions in your question are different logically. They are equivalent to:
(a && b) || c || d || e
and
a && (b || c || d || e)
The parentheses affect the order of evaluation. In the first, a
and b
are grouped together by an &&
, hence both must be true for that part of the expression (in the parentheses) to be true. In the second, b
, c
, d
and e
are grouped together by ||
. In this case, only one of b
, c
, d
and e
needs to be true for that part of the expression (in the parentheses) to be true.
Related Topics
Meteor: Proper Use of Meteor.Wrapasync on Server
Check If Jquery Has Been Loaded, Then Load It If False
Access Denied to Jquery Script on Ie
Find Out How Long an Ajax Request Took to Complete
Capture Value Out of Query String with Regex
Getting Key with the Highest Value from Object
Are Dashes Allowed in JavaScript Property Names
How to Filter Array When Object Key Value Is in Array
How to Highlight the Text of the Dom Range Object
How to Understand Usecapture Parameter in Addeventlistener
How to Add a Key Prop to a React Fragment
Limit Concurrency of Promise Being Run
JavaScript Change Date into Format of (Dd/Mm/Yyyy)
Why Does an Onclick Property Set with Setattribute Fail to Work in Ie
Calculate Date from Week Number in JavaScript
App.Settings - the Angular Way
Best Practice for Using Window.Onload
Jquery - Script Tags in the HTML Are Parsed Out by Jquery and Not Executed