• 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

Explanation of Program

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,
i need a explanation of the following code. anybody know please explain me.



class SSBool {
public static void main(String [] args) {
boolean b1 = true;
boolean b2 = false;
boolean b3 = true;
if ( b1 & b2 | b2 & b3 | b2 )
System.out.print("ok ");
if ( b1 & b2 | b2 & b3 | b2 | b1 )
System.out.println("dokey");
}
}

Thanks.
 
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The code should print out 'dokey'.

None of the first conditions in the if statement are true, and only the last one in the second is true which is enough for the if statement to evaluate to true.

A little more:
if ( b1 & b2 | b2 & b3 | b2 ) means if any of those conditions are true and the values are:
if (true and false) or (false and true) or (false)

None of those are true, so 'ok' isn't printed.

if ( b1 & b2 | b2 & b3 | b2 | b1 ) means if any of those conditions are true and the values are:
if (true and false) or (false and true) or (true)

The only one of those that is true is the last condition - (true), so the IF statement evaluates to true and dokey is printed.

Hope that clears it up a bit.
[ September 13, 2006: Message edited by: Andy Morris ]
 
kavi ram
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Morris.

So if statement check true condition only. if its true means it will print "dokey". i'm i right?
 
Andy Morris
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not entirely sure I understand that question, but..yes, IF conditions need to evaluate to true in order for the code in their body to be executed.

E.g

if(!false) {
System.out.println("bla");
}

That evaluates to true and bla would be printed.

I don't mean to cause offense but this is a fundamental aspect of programming so attempting the SCJP exam might be a little inappropriate right now. That's up to you though. Good luck!
 
Greenhorn
Posts: 18
VI Editor Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If the Java operator precedence rules work the same way as C/C++, then the boolean expression as written (i.e. with no grouping parentheses) would simply evaluate from left to right. So ( b1 & b2 | b2 & b3 | b2 ) would be evaluated as ((( ( b1 & b2 ) | b2 ) & b3 ) | b2 ).

The second expression is evaluated similarly, but it is interesting to note that since it ends in "| b1" and you know b1 is true, therefore you know right away that the whole expression evaluates to true.

(BTW if I'm mistaken, I hope someone will jump in and correct me...)

b.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

If the Java operator precedence rules work the same way as C/C++, then the boolean expression as written (i.e. with no grouping parentheses) would simply evaluate from left to right. So ( b1 & b2 | b2 & b3 | b2 ) would be evaluated as ((( ( b1 & b2 ) | b2 ) & b3 ) | b2 ).



In Java, the precedence of the bitwise AND is slightly higher than the bitwise OR -- so evaluation should be...

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

Henry
 
E McKenney
Greenhorn
Posts: 18
VI Editor Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Henry Wong:

In Java, the precedence of the bitwise AND is slightly higher than the bitwise OR -- so evaluation should be... ((( b1 & b2 ) | ( b2 & b3 )) | b2 )
Henry




Thanks! Scratch my earlier comment then....
b.
 
reply
    Bookmark Topic Watch Topic
  • New Topic