2 Replies - 19127 Views - Last Post: 30 January 2010 - 09:31 AM Rate Topic: -----

#1 Codebug  Icon User is offline

  • D.I.C Head

Reputation: 31
  • View blog
  • Posts: 244
  • Joined: 11-October 09

Using a boolean variable as an if statement parameter

Posted 29 January 2010 - 10:16 PM

I have a quick question regarding passing boolean variables to if statements. When you use a boolean variable and pass it to a method, you don't have to say if( boolean variable == true ). You can simply say if( boolean variable ). How does this work? Look at my code example for a better illustration of my question:


 boolean x = true;

if ( x )
{
   System.out.println( 2 + 2 ); // will print out 4
}

Now what if x was false? Nothing would be printed out. Why doesn't the method execute whether x is true or false?
 No matter what x is, it is being passed to the if statement. If x is false, that value is being passed to the if statement. 
In that case it appears that the statement reads:

// boolean value holds false
if( false )
{
	do something
}

boolean x = true;

if ( true )
{
	do something;
}




I hope I have made sense. I know that question was probably not very clear but it was the best I could do without being able to verbally voice my question .

Thanks for the help.

This post has been edited by Codebug: 29 January 2010 - 10:18 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Using a boolean variable as an if statement parameter

#2 athlon32  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 116
  • View blog
  • Posts: 363
  • Joined: 20-August 08

Re: Using a boolean variable as an if statement parameter

Posted 29 January 2010 - 10:37 PM

Well, you just need to think about if statements correctly. Remember, if the condition being checked by an if statement is true, then it will execute. The if statement doesn't care a lot about the values/conditions it's just trying to get a yes/no kind of answer.

So, when you have this:
x = 35; // x is interger

if(x == 35) // do something here 


the compiler sees x == 35 as true, and since it's true, it executes. Now, passing an if statement a Boolean basically saves the compiler a step. It just checks if its value( true or not), and acts upon that. Make sense? Passing an if statement a false value makes the statement false, and thus it doesn't execute.

Illustration :)
x = 35; // x is interger
b = false; // b is boolean

// this....
if(x == 55); // do something here

//is the equivilent of...
if( b ); // do stuff here


As you can see they're both false, one just came to that conclusion via a boolean :)

Hope I helped :)

This post has been edited by athlon32: 29 January 2010 - 10:38 PM

Was This Post Helpful? 1
  • +
  • -

#3 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10596
  • View blog
  • Posts: 39,258
  • Joined: 27-December 08

Re: Using a boolean variable as an if statement parameter

Posted 30 January 2010 - 09:31 AM

It has to do with boolean expressions. Like mathematical expressions which return a number, boolean expressions return true or false (boolean) values. Some examples of boolean expressions include x == 35, "123".equals("Hello, world"), "123" instanceof Object, etc. And since these expressions return boolean values, they can be assigned to boolean variables like so: boolean x = (y != 3);. So the boolean stored in x is true if y != 3, false otherwise. In conditionals like if statements, while loops, etc., they check the result of the value in the parentheses for a boolean- true or false. They execute if true, and terminate if false. So since they check boolean values, you can include boolean expressions, boolean literals (true or false), or boolean variables in the parentheses.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1