• 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
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Please exlain this code

 
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can anyone telll me wts the function of (!b1) and (!b2) in the following code.what will be value of 'b1' and 'b2' [ i guess they are flase ..since they are instance variables]. Also how this is working in for loop.

Also can you tell me what this statement performs else if(b1 | b2). This seems to be basic operator questions..but i was a bit confused

public class Example1 {

static boolean b1,b2;
public static void main(String[] args) {
int x=0;
if(!b1){
if(!b2){
b1 = true;
x++;
if(5>6){
x++;
}
if(!b1) x= x+10;
else if(b2= true)x=x+100;
else if(b1 | b2)x=x+1000;
}
}
System.out.println("Value of X:" +x);
}
}
 
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Value of X:101
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The ! operator is a unary operator that changes the truth value of its operand.

Since b1 and b2 are not given explicit values, they are initialized to false.

When you use | between two boolean expressions, then both expressions are evaluated, and if either of them is true, the result is true.
 
Supriya Nimakuri
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hai keith...

Thnks for explanation...

I got a small doubt here...b1 is assigned as false..but with what value is this compared in if statement -- if (!b1) -- does this mean tht if b1 is not false
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if(b1)
{
// statements
}
here the statements will be executed only if b1 is true.
if(!b1)
{
//statements
}
here the statement will be executed only if b1 is false.
read if(!b1) as "if not b1" u can understand the meaning.
 
Supriya Nimakuri
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks to Sreeram Desigan , Keith and Wise for expllaining the code
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if (b2=true)
should be written
b2 = true; // now you can write, why you set b2 to true! aha!
if (b2)

coding can be nice and easy.
 
wise owen
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


It is a trick in Exam test.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic