• 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

Explain me this Programm

 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class Beverage {
public static void main(String []args){
boolean b1= true;
boolean b2 = false;
boolean b3 = true;

if(b1 & b2 | b2& b3 | b2 )


System.out.println (" ok " );

if(b1 & b2 | b2& b3 | b2 |b1)
System.out.println("do kwy");


}

}
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

This Program illustrates the operators precedence, here the & oprator takes precedence over the | oprator.

so in the first if we can describe the expression as follows
((b1 & b2) | (b2 & b3) | b2)
((true & false) | (false & true) | false)
( (false) | (false) | false)
and as u can see it produces fals so the condition will not execute

in the second condition
((b1 & b2) | (b2 & b3) | b2 | b1)
((true & false) | (false & true) | false | true)
( (false) | (false) | false | true)
and as u can see it will evaluate to true so the condition will execute

and the program will print "do kwy" to the console
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"Akhilesh"

Please click on the My Profile link above and change your display name to match the JavaRanch Naming Policy of using your real first and real last names

Thanks

Mark
 
Ranch Hand
Posts: 584
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Akhilesh,

Keep in mind that the precedence for logical operators is as follows :

^ : xor comes 1st
& : comes 2nd
| : comes 3rd
&& : comes 4th
|| : comes 5th
 
reply
    Bookmark Topic Watch Topic
  • New Topic