Results 1 to 5 of 5
  1. #1
    NewbieDan is offline Member
    Join Date
    Oct 2011
    Posts
    7
    Rep Power
    0

    Default IF statement - && vs nested statements?

    Hello, newbie here :D

    I'm just wondering if there is any difference in whether I use && compared to using nested if statements.


    int count = 2;
    int total = 4;

    //---------------
    // Using &&
    //---------------
    if (count == 2 && total == 4)
    {
    System.out.println ("2 + X = 4");
    }

    //------------------------------
    // Compared to using nested statements
    //-------------------------------
    if (count == 2)
    {
    if (total == 4)
    {
    System.out.println ("2+X=4");
    }
    }



    my textbook says, "This type of processing should be used carefully" in regards to using && and having the process "short circuit". So is using nested if statements better for some reason?
    Last edited by NewbieDan; 10-23-2011 at 01:58 AM.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,772
    Blog Entries
    7
    Rep Power
    21

    Default Re: IF statement - && vs nested statements?

    You have a nice tool in your JDK: javap.exe; it can disassemble the class code in a .class file. Try it and see what code is produced for your example and judge for yourself. (look in your JDK/bin directory, it should be in your PATH environment variable).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    NewbieDan is offline Member
    Join Date
    Oct 2011
    Posts
    7
    Rep Power
    0

    Default Re: IF statement - && vs nested statements?

    Could you point me in the right direction on how to use it in Eclipse?

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,772
    Blog Entries
    7
    Rep Power
    21

    Default Re: IF statement - && vs nested statements?

    Quote Originally Posted by NewbieDan View Post
    Could you point me in the right direction on how to use it in Eclipse?
    Not everything needs to be done with Eclipse; fire up a shell/command prompt; go to the directory where your .class file is stored and type this:

    Java Code:
    javap -c YourClassFile
    javap produces a listing of the disassembled code in YourClassFile.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    Iron Lion is offline Senior Member
    Join Date
    Nov 2010
    Posts
    210
    Rep Power
    5

    Default Re: IF statement - && vs nested statements?

    Quote Originally Posted by NewbieDan View Post
    my textbook says, "This type of processing should be used carefully" in regards to using && and having the process "short circuit". So is using nested if statements better for some reason?
    The only thing you need to be careful with regards to short-circuiting is that if the left-hand operand of && evaluates to false, or the left-hand operand of || evaluates to true, the right-hand operand won't be evaluated at all. So if you have something like:

    Java Code:
    if (a-- > 0 && b++ <= 100) { ... }
    then, if a <= 0, a will be decremented but b will not be incremented. If you need both operands to be evaluated, you can use the & operator.

Similar Threads

  1. Replies: 7
    Last Post: 05-03-2011, 07:41 PM
  2. Using IF statement to control return statements.
    By Teclis in forum New To Java
    Replies: 21
    Last Post: 04-16-2011, 09:42 AM
  3. Replies: 20
    Last Post: 03-22-2011, 05:02 AM
  4. nested for loops
    By Implode in forum New To Java
    Replies: 4
    Last Post: 09-01-2009, 09:47 AM
  5. Statement or Prepared Statement ?
    By paty in forum JDBC
    Replies: 3
    Last Post: 08-01-2007, 05:45 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •