A double pipe is the logical OR operator
||
The OR operator is used to evaluate multiple expressions during decision making with if statements.
Here's the OR operator using a simple if statement format:
if (condition1 || condition2) {
doSomething();
}
Here's the OR operator using a if-then-else statement format:
if (condition1 || condition2) {
doSomething();
}
else {
doSomethingElse();
}
The method
doSomething() will only execute if
one or both of the conditions in the if statement evaluate to true.
Let's take a look at another, more concrete example:
if (maxDamage > 100 || ammoCount == 0) {
gameOver();
}
The method
gameOver() will only execute if either maxDamage is greater than 100 (and or)
OR ammoCount is equal to 0. If either condition does not evaluate to true, the gameOver() method will not execute.
In the following example, we use an if-then-else statement:
if (maxDamage > 100 || ammoCount == 0) {
gameOver();
}
else {
continue();
}
If
either or both of the conditions are true, the gameOver() method will execute. If
both of the conditions are false, the continue() method will execute.
Also see
&& Operator (AND)
! Operator (NOT)