Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / December 2005

Tip: Looking for answers? Try searching our database.

If statement - whats going on here?

Thread view: 
Jenski - 05 Dec 2005 18:01 GMT
Hiya

ok peeps, we all know how normal if statements work, but how does this
work? What should be expected from the outcome?

boolean brandNew = false

    if (brandNew)
        //condition 1
    else {
        //condition 2
    }
Roedy Green - 05 Dec 2005 18:25 GMT
>ok peeps, we all know how normal if statements work, but how does this
>work? What should be expected from the outcome?
[quoted text clipped - 6 lines]
>        //condition 2
>    }

1. you left off your semicolon after false

2. your conditions are empty

3. but other than that, if brandNew is true, you execute condition 1
else condition 2.  The {} are mandatory if your condition has more
than one statement

Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

fb - 05 Dec 2005 19:15 GMT
> Hiya
>
[quoted text clipped - 8 lines]
>         //condition 2
>     }

It will execute the 'else' statement.
Oliver Wong - 05 Dec 2005 19:20 GMT
> Hiya
>
[quoted text clipped - 8 lines]
> //condition 2
> }

   You need a statement where the comment "//condition 1" appears. At the
very least, you'd have to put the empty statement, like so:

if (brandNew)
; //condition 1
else {
 //condition 2
}

   In the revised example, what will happen is that since brandNew is
false, the else branch gets taken.

   - Oliver
Monique Y. Mudama - 05 Dec 2005 20:59 GMT
> Hiya
>
[quoted text clipped - 8 lines]
>         //condition 2
>     }

Maybe I'm dense, but what part of this are you considering "not
normal"?

I could take this a few different ways.

If I'm supposed to take this as pseudo-code and pretend there are
statements where the comments currently reside, then ....

if '//condition 1' is a single statement, it executes normally.

if '//condition 1' is empty or has multiple lines, you will get a
compiler complaint that there's an else with no if attached.

I typed all that without actually trying to compile the code.
Afterwards, I thought I'd check myself.  Here is the code, which you
could *easily* have written and played with yourself, rather than
asking the group.  I was right about the results, fwiw.

public class IfTest
{

  public static void main (String [] args)
  {
     boolean brandNew = false;

    if (brandNew)
        brandNew = true;
        //condition 1
    else {
        //condition 2
    }
  }
}

Signature

monique

Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html

George Cherry - 06 Dec 2005 00:50 GMT
> Hiya
>
[quoted text clipped - 8 lines]
> //condition 2
> }

"What should be expected from the outcome?"

A compilation error.
Noodles Jefferson - 06 Dec 2005 05:30 GMT
> Hiya
>
[quoted text clipped - 8 lines]
>         //condition 2
>     }

The same way.

if (brandNew) is the same as saying

if (brandNew == true)

It's just a shorthand way of doing it. That's just something you have to
get used to. I never think of it, so I use the "== true" one, usually.

Signature

Noodles Jefferson
mhm31x9 Smeeter#29 WSD#30
sTaRShInE_mOOnBeAm aT HoTmAil dOt CoM

"Our earth is degenerate in these latter days, bribery and corruption
are common, children no longer obey their parents and the end of the
world is evidently approaching."
--Assyrian clay tablet 2800 B.C.

Noodles Jefferson - 06 Dec 2005 05:49 GMT
> > Hiya
> >
[quoted text clipped - 17 lines]
> It's just a shorthand way of doing it. That's just something you have to
> get used to. I never think of it, so I use the "== true" one, usually.

<SCCCE>

public class BooleanTest {

   private boolean someWord = true;
   private String testFailed = "testFailed";

   public static void main(String[] args) {
   
       BooleanTest bt = new BooleanTest();
       bt.doTheTest();
       System.exit(0);
       
       
       }
       
     public void doTheTest() {
       
           if (someWord) {
       
               System.out.print(someWord);
           
           
           } else {
           
               System.out.print(testFailed);
               
           }
       
       
   }
 

}

</SCCCE>

Signature

Noodles Jefferson
mhm31x9 Smeeter#29 WSD#30
sTaRShInE_mOOnBeAm aT HoTmAil dOt CoM

"Our earth is degenerate in these latter days, bribery and corruption
are common, children no longer obey their parents and the end of the
world is evidently approaching."
--Assyrian clay tablet 2800 B.C.

Roedy Green - 06 Dec 2005 20:03 GMT
On Mon, 5 Dec 2005 23:30:49 -0600, Noodles Jefferson
<silverbells@tacoshells.com> wrote, quoted or indirectly quoted
someone who said :

>The same way.
>
[quoted text clipped - 3 lines]
>
>It's just a shorthand way of doing it

It is not shorthand.  IF wants a boolean expression.  brandNew is
already a boolean expression.

Adding a string of == true is just stuttering. When do you stop?

if ( brandnew == true == true == true == true )

What you are advocating is as goofy as suggesting that
i = j;
should be replaced by
i = j+0;

because you don't understand that j is a int expression already.

This is one of my pet peeves. See
http://mindprod.com/jgloss/boolean.html
and
http://mindprod.com/jgloss/newbie.html
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Noodles Jefferson - 06 Dec 2005 22:06 GMT
> On Mon, 5 Dec 2005 23:30:49 -0600, Noodles Jefferson
> <silverbells@tacoshells.com> wrote, quoted or indirectly quoted
[quoted text clipped - 10 lines]
> It is not shorthand.  IF wants a boolean expression.  brandNew is
> already a boolean expression.

Okay. Fair enough.

> Adding a string of == true is just stuttering. When do you stop?
>
> if ( brandnew == true == true == true == true )

Well, now you're just being a c**k. That will throw a compiler error
because you're not testing 4 expressions and you don't need to test it 4
times to determine if it's true or false. So you can't do it anyway.

> What you are advocating is as goofy as suggesting that
> i = j;
> should be replaced by
> i = j+0;

First, I'm not advocating it, she(?) asked how it works. People telling
OP that it'll throw an error are either wrong or lying to him.

> because you don't understand that j is a int expression already.

Secondly, when you adding nothing to something, you get the the same
number so why would you do that anyway?

> This is one of my pet peeves.

I'm sorry if your pets are peeved.

> See
> http://mindprod.com/jgloss/boolean.html
> and
> http://mindprod.com/jgloss/newbie.html

Signature

Noodles Jefferson
mhm31x9 Smeeter#29 WSD#30
sTaRShInE_mOOnBeAm aT HoTmAil dOt CoM

"Our earth is degenerate in these latter days, bribery and corruption
are common, children no longer obey their parents and the end of the
world is evidently approaching."
--Assyrian clay tablet 2800 B.C.

Eric Sosman - 06 Dec 2005 22:21 GMT
Noodles Jefferson wrote On 12/06/05 17:06,:

>>On Mon, 5 Dec 2005 23:30:49 -0600, Noodles Jefferson
>><silverbells@tacoshells.com> wrote, quoted or indirectly quoted
[quoted text clipped - 20 lines]
> because you're not testing 4 expressions and you don't need to test it 4
> times to determine if it's true or false. So you can't do it anyway.

   Of the three assertions in this paragraph, two are
easily disproved by experiment.  But if you're Really
Sure -- well, would you like to place a small wager?

   As to the first assertion, Usenet is not a good
medium for assessing someone's skills in food preparation.

Signature

Eric.Sosman@sun.com

Noodles Jefferson - 07 Dec 2005 22:44 GMT
> Noodles Jefferson wrote On 12/06/05 17:06,:
> >
[quoted text clipped - 26 lines]
> easily disproved by experiment.  But if you're Really
> Sure -- well, would you like to place a small wager?

Nah. People that want to put money think they're gonna keep it. I'm not
so sure. In blackjack terms, I'll stand at 16.

>     As to the first assertion, Usenet is not a good
> medium for assessing someone's skills in food preparation.

I make the best french toast in the universe. Tell no one.

Signature

Noodles Jefferson
mhm31x9 Smeeter#29 WSD#30
sTaRShInE_mOOnBeAm aT HoTmAil dOt CoM

"Our earth is degenerate in these latter days, bribery and corruption
are common, children no longer obey their parents and the end of the
world is evidently approaching."
--Assyrian clay tablet 2800 B.C.

Oliver Wong - 06 Dec 2005 23:06 GMT
>> if ( brandnew == true == true == true == true )
>
> Well, now you're just being a c**k. That will throw a compiler error
> because you're not testing 4 expressions and you don't need to test it 4
> times to determine if it's true or false. So you can't do it anyway.

   I'm relatively sure that you CAN do this, and that it WON'T throw a
compiler error (assuming that after the if statement, there is a statement).
In other words, yes the above will not compile, but it's not because of the
fact that there are "4 expressions" in a row.

>> What you are advocating is as goofy as suggesting that
>> i = j;
[quoted text clipped - 3 lines]
> First, I'm not advocating it, she(?) asked how it works. People telling
> OP that it'll throw an error are either wrong or lying to him.

   I'm one of the people who told the OP that the code (s)he posted will
not compile. The reason it won't compile is that if-statements have to
contain a statement. So the following:

<code>
if (someCondition)
else
 statementB;
</code>

won't compile, but this:

<code>
if (someCondition)
 statementA;
else
 statementB;
</code>

will compile (assuming some suitable replacement for "statementA" and
"statementB").

The actual code that the OP posted was:

<code>
if (brandNew)
//condition 1
else {
//condition 2
}
</code>

And here, the else branch does contain a statement ("{}" is a legal
statement in Java), but the if branch did not contain a statement, which is
why I said it would not compile.

   - Oliver
Monique Y. Mudama - 06 Dec 2005 23:19 GMT
> will compile (assuming some suitable replacement for "statementA" and
> "statementB").
[quoted text clipped - 12 lines]
> statement in Java), but the if branch did not contain a statement, which is
> why I said it would not compile.

Which is exactly why I expressed reservations when posting *my*
response.  It all hinges on whether '//condition 1' is really code,
and if so, how many lines of code that block might be.

Signature

monique

Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html

Noodles Jefferson - 07 Dec 2005 22:49 GMT
> >> if ( brandnew == true == true == true == true )
> >
[quoted text clipped - 6 lines]
> In other words, yes the above will not compile, but it's not because of the
> fact that there are "4 expressions" in a row.

Ok, what about something like this

if (someBoolean == true == false == true == false)

How does something like that work out then?

> >> What you are advocating is as goofy as suggesting that
> >> i = j;
[quoted text clipped - 39 lines]
> statement in Java), but the if branch did not contain a statement, which is
> why I said it would not compile.

I have nothing to add to this. I don't know why I'm telling you that I'm
not adding anything here.

Signature

Noodles Jefferson
mhm31x9 Smeeter#29 WSD#30
sTaRShInE_mOOnBeAm aT HoTmAil dOt CoM

"Our earth is degenerate in these latter days, bribery and corruption
are common, children no longer obey their parents and the end of the
world is evidently approaching."
--Assyrian clay tablet 2800 B.C.

Oliver Wong - 07 Dec 2005 23:55 GMT
> Ok, what about something like this
>
> if (someBoolean == true == false == true == false)
>
> How does something like that work out then?

   Not sure. Why don't you try it and see? What happens is well defined in
the language spec (google for "Java Language Specification"), in the sense
that the order of evaluation is left to right, and so on, but I'm too lazy
to try it myself, and I don't want to risk giving an incorrect prediction
here.

   - Oliver
Noodles Jefferson - 08 Dec 2005 02:47 GMT
> > Ok, what about something like this
> >
[quoted text clipped - 9 lines]
>
>     - Oliver

deesBoolean tested using == true == false == true
deesBoolean started true - tested  false

datBoolean tested using == true == false == true
datBoolean started false - tested true.

Ooookay. Well, that was pretty worthless. Lost was a rerun anyway so
whatever.

Signature

Noodles Jefferson
mhm31x9 Smeeter#29 WSD#30
sTaRShInE_mOOnBeAm aT HoTmAil dOt CoM

"Our earth is degenerate in these latter days, bribery and corruption
are common, children no longer obey their parents and the end of the
world is evidently approaching."
--Assyrian clay tablet 2800 B.C.

Chris Smith - 08 Dec 2005 03:16 GMT
> deesBoolean tested using == true == false == true
> deesBoolean started true - tested  false

Yep, you had (parens added for clarity):

   ((((deesBoolean) == true) == false) == true)

You said deesBoolean was true, so it goes like this.  Turn off any
proportional fonts.  The underlines above each line show what section
will be simplified in the next line.

       ___________
   ((((deesBoolean) == true) == false) == true)
      _____________
   ((((true       ) == true) == false) == true)
       ____________________
   ((( true         == true) == false) == true)
     _______________________
   (((        true         ) == false) == true)
              _______________________
   ((         true           == false) == true)
    __________________________________
   ((             false              ) == true)
                  ____________________________
   (              false                == true)
   ____________________________________________
   (                  false                   )
                      _____
                      false

> datBoolean tested using == true == false == true
> datBoolean started false - tested true.

You can work this one out for practice.

Signature

www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation

Noodles Jefferson - 11 Dec 2005 22:18 GMT
> > deesBoolean tested using == true == false == true
> > deesBoolean started true - tested  false
[quoted text clipped - 25 lines]
>                        _____
>                        false

Sweet explanation. Thanks.

> > datBoolean tested using == true == false == true
> > datBoolean started false - tested true.
>
> You can work this one out for practice.

Signature

Noodles Jefferson
mhm31x9 Smeeter#29 WSD#30
sTaRShInE_mOOnBeAm aT HoTmAil dOt CoM

NP: "The Road to Chicago" -- Thomas Newman (Road to Perdition
Soundtrack)

"Our earth is degenerate in these latter days, bribery and corruption
are common, children no longer obey their parents and the end of the
world is evidently approaching."
--Assyrian clay tablet 2800 B.C.

Mark Haase - 07 Dec 2005 07:14 GMT
> Well, now you're just being a c**k. That will throw a compiler error
> because you're not testing 4 expressions and you don't need to test it 4
> times to determine if it's true or false. So you can't do it anyway.

What compiler are you using? Its valid java, and in fact I don't know
how a compiler would even detect that its nonsense code.

marks:~ mehaase$ cat Test.java
class Test {
 public static void main(String args[]) {
   boolean b = false;
   if (b == true == true == true)
     System.out.println("FOO");
   else
     System.out.println("BAR");
 }
}

marks:~ mehaase$ javac Test.java
marks:~ mehaase$ java Test
BAR

See? Works fine here

|\/|  /|  |2  |<
mehaase(at)gmail(dot)com
Noodles Jefferson - 07 Dec 2005 22:14 GMT
In article <mehaase-286CCB.02142407122005@news1.east.earthlink.net>,
Mark Haase took the hamburger, threw it on the grill, and I said "Oh
wow"...

> > Well, now you're just being a c**k. That will throw a compiler error
> > because you're not testing 4 expressions and you don't need to test it 4
[quoted text clipped - 19 lines]
>
> See? Works fine here

New to me.

Why in the hell would someone do something like that?

Signature

Noodles Jefferson
mhm31x9 Smeeter#29 WSD#30
sTaRShInE_mOOnBeAm aT HoTmAil dOt CoM

"Our earth is degenerate in these latter days, bribery and corruption
are common, children no longer obey their parents and the end of the
world is evidently approaching."
--Assyrian clay tablet 2800 B.C.

Roedy Green - 07 Dec 2005 22:39 GMT
On Wed, 7 Dec 2005 16:14:16 -0600, Noodles Jefferson
<silverbells@tacoshells.com> wrote, quoted or indirectly quoted
someone who said :

>Why in the hell would someone do something like that?

you were the one defending it, adding == true on the end of a
perfectly valid boolean expression.
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Noodles Jefferson - 07 Dec 2005 22:50 GMT
> On Wed, 7 Dec 2005 16:14:16 -0600, Noodles Jefferson
> <silverbells@tacoshells.com> wrote, quoted or indirectly quoted
[quoted text clipped - 4 lines]
> you were the one defending it, adding == true on the end of a
> perfectly valid boolean expression.

Not 4 of them. 1, I can see. Not 4.

Signature

Noodles Jefferson
mhm31x9 Smeeter#29 WSD#30
sTaRShInE_mOOnBeAm aT HoTmAil dOt CoM

"Our earth is degenerate in these latter days, bribery and corruption
are common, children no longer obey their parents and the end of the
world is evidently approaching."
--Assyrian clay tablet 2800 B.C.



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.