Java if statements and boolean expressions [duplicate]
This question already has an answer here:
If statement executing all conditions
8 answers
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
java if-statement boolean-logic boolean-expression
marked as duplicate by nullpointer
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Nov 13 '18 at 0:57
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
If statement executing all conditions
8 answers
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
java if-statement boolean-logic boolean-expression
marked as duplicate by nullpointer
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Nov 13 '18 at 0:57
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
1
Why would you want to check all of them if it doesn't matter once one is false? Do theis...functions carry out side effects?
– Carcigenicate
Nov 13 '18 at 0: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 1: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 1:03
add a comment |
This question already has an answer here:
If statement executing all conditions
8 answers
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
java if-statement boolean-logic boolean-expression
This question already has an answer here:
If statement executing all conditions
8 answers
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
This question already has an answer here:
If statement executing all conditions
8 answers
java if-statement boolean-logic boolean-expression
java if-statement boolean-logic boolean-expression
asked Nov 13 '18 at 0:47
SalSal
1169
1169
marked as duplicate by nullpointer
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Nov 13 '18 at 0:57
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by nullpointer
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Nov 13 '18 at 0:57
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
1
Why would you want to check all of them if it doesn't matter once one is false? Do theis...functions carry out side effects?
– Carcigenicate
Nov 13 '18 at 0: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 1: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 1:03
add a comment |
1
Why would you want to check all of them if it doesn't matter once one is false? Do theis...functions carry out side effects?
– Carcigenicate
Nov 13 '18 at 0: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 1: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 1:03
1
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 0:49
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 0: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 1:02
@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 1: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 1:03
@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 1:03
add a comment |
1 Answer
1
active
oldest
votes
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?
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 1:00
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
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?
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 1:00
add a comment |
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?
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 1:00
add a comment |
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?
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?
answered Nov 13 '18 at 0:52
JakgJakg
405311
405311
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 1:00
add a comment |
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 1:00
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 1:00
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 1:00
add a comment |
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 0: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 1: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 1:03