0

Got a basic if statement question, so I have an if statement in Java as shown here:

if (
    !isOmitted(word,map.get(word.length()+1))     &&
    !isInserted(word,map.get(word.length()-1))    &&
    !isTransposed(word,map.get(word.length()))    &&
    !isSubstituted(word,map.get(word.length()))   &&
    !isCapital(word,map.get(word.length())))
{
    noSuggestion=true;
}

where each individual method works perfectly as desired. Is there any way for java to check all conditions even when one is false? I know that the nature of the && operator is that as soon as a condition does not hold true, there is no point in checking the remaining conditions, as the entire condition is going to be set to false, but I was hoping I could do something like this in order to keep my code someone cleaner. I know I can use boolean variables, and assign the returned value to 5 different variables, but is there any other work around to force every condition to be checked? Thanks a lot in advanced

Sal
  • 1,471
  • 2
  • 15
  • 36
  • 1
    Why would you want to check all of them if it doesn't matter once one is false? Do the `is...` functions carry out side effects? – Carcigenicate Nov 13 '18 at 00:49
  • @Carcigenicate The is... methods all output possible corrections for a word. I need all of them to run, as they output their own data if applicable, and only if none of them output any data (resulting in a returned value of false), then noSuggestion would be set to true, and the remainder of the code would run – Sal Nov 13 '18 at 01:02
  • @ErwinBolwidt I should just let the Javadocs say it a lot better than I *For &, the result value is true if both operand values are true; otherwise, the result is false.* – Scary Wombat Nov 13 '18 at 01:03

1 Answers1

4

A single & is a non-short-circuit operand - ie both sides are evaluated irrespective of whether required.

More info here - https://docs.oracle.com/javase/specs/jls/se8/html/jls-15.html#jls-15.22.2

However, this sounds like a poor design. In fact, some code analysis tools would automatically flag these operators as suspicious for this reason. Typically you would want to call each method as they make some change to the state of your objects, which is not something you would expect in an if statement. But your method names do not suggest this. What are you trying to achieve?

Jakg
  • 922
  • 12
  • 39
  • So the overall program is essentially a Spell Check program. I want every possible correction method to run, and if no correct word could be found, the output to the console is that no correction was found for a particular word. Like I mentioned in the post, I was hoping to do all of this without creating several additional variables that are only going to be used as part of an if statement, and not after. When you say poor design, are you referencing the use of a single & operand, or the way that I have designed my program? – Sal Nov 13 '18 at 01:00