Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

logic operation  RSS feed

 
amr talaat
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how to make if not method (OR) not method in java ???
cz the way i do it doesnt return the response i want to see


and here how i do it


if(!method1()||!method2()) // doesnt do the excepected result


if(!(method1())||!(method2())) //neither this do the excepected result

it doesnt show bugs

but the excution logic isnt what i want


what i want is if anyone of this methods return (not true) excute the next code


 
K. Tsang
Bartender
Posts: 3648
16
Firefox Browser Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello there

It really depends on what your method1 and method2 does. If you return true say in method1, and check for !method1(), then this evaluates to false. Same goes for method2().
 
amr talaat
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
K. Tsang wrote:Hello there

It really depends on what your method1 and method2 does. If you return true say in method1, and check for !method1(), then this evaluates to false. Same goes for method2().



METHOD1 RETURN TRUE
METHOD2 RETURN TRUE


I WANT TO DO THIS


WHILE (!method1(N1,N2)||!method2(N1,N2))
{

//EXCUTE THIS UNTIL ONE OF THE METHODS RETURN FALSE


}
 
amr talaat
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
amr talaat wrote:
K. Tsang wrote:Hello there

It really depends on what your method1 and method2 does. If you return true say in method1, and check for !method1(), then this evaluates to false. Same goes for method2().



METHOD1 RETURN TRUE
METHOD2 RETURN TRUE


I WANT TO DO THIS


WHILE (!method1(N1,N2)||!method2(N1,N2))
{

//EXCUTE THIS UNTIL ONE OF THE METHODS RETURN FALSE


}


TO put you in the picture excuting the code in the body affects the parameters of method1 and method 2 which affect its return value

 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
amr talaat wrote:
I WANT TO DO THIS


WHILE (!method1(N1,N2)||!method2(N1,N2))
{
//EXCUTE THIS UNTIL ONE OF THE METHODS RETURN FALSE
}




The above says, "keep looping as long as m1() and m2() both return true." That's what "while" means in English and in Java. You seem to have been treating "while" as if it meant "until."

Note that when using &&, if m1() returns false, it won't bother evaluating m2(), wince it can't affect the outcome of the if condition. If you want m2() to execute even if m1() returns false, use & instead of &&.
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
amr talaat wrote:I WANT TO DO THIS
WHILE (!method1(N1,N2)||!method2(N1,N2))...

Amr, please KeepItDown (←click). Posting in all caps is akin to shouting, and it doesn't make pleasant reading.

I think Jeff has basically answered your question, but I'd also suggest that you try to avoid "negative" (!) logic if you can.
if (true)
is generally a lot easier to follow than
if (!false)
especially if the expression is a compound one.

Winston
 
fred rosenberger
lowercase baba
Bartender
Posts: 12542
48
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you want your body to execute when AT LEAST one method returns false, or when EXACTLY one method returns false?
 
Campbell Ritchie
Marshal
Posts: 55734
163
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is worth remembering de Morgan’s law for this sort of problem. If p and q are any boolean values:-
!p && !q ≡ !(p || q)
!p || !q ≡ !(p && q)
Remember ! has a higher precedence than && or ||.
 
amr talaat
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks guys morgan law and (&) did the trick
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!