• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

logic operation

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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


 
Bartender
Posts: 3648
16
Android Mac OS X Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 &&.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Marshal
Posts: 79177
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks guys morgan law and (&) did the trick
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic