Question about inverted IF statements

Discussion in 'Bukkit Help' started by uyscutix, Feb 13, 2017.

Thread Status:
Not open for further replies.
  1. Offline

    uyscutix

    Sometimes I get confused when to put ! for inverting an IF statement. I've seen several inverted IF statements like this:

    if (!<statement>)
    {
    // code here
    }

    and:

    if (!(<statement>))
    {
    // code here
    }

    So what's the difference between adding an extra bracket after the ! in an IF statement (which is !(<statement>)) and the difference between adding ! without brackets (which is !<statement>)?
     
  2. Offline

    timtower Administrator Administrator Moderator

    @uyscutix Some code requires brackets, instanceof for example.
     
  3. Offline

    Zombie_Striker

    @uyscutix
    If the statement is a boolean by itself (e.g. Player#isAlive() ), you do not need a bracket. But, if you are creating a new boolean (such as linking variables using || or && and using instanceof or ==)
     
  4. Offline

    Rayzr522

    And sometimes people just add extra brackets because they feel like it.
     
    AlvinB likes this.
  5. Offline

    uyscutix

    Forgot to mention: i'm assuming this example both do the same thing, correct?

    if (args.length != 1)
    {
    // code here
    }

    and

    if (!(args.length == 1))
    {
    // code here
    }
     
  6. Offline

    timtower Administrator Administrator Moderator

    @uyscutix No, if args.length = 2, then the first one will fire.
    Use >
     
Thread Status:
Not open for further replies.

Share This Page