0

I have a boolean variable collision initialized to false.

Now in an if statement, suppose my if condition is the following:

if(!collision)

does this mean that the if statement will execute if collision is the opposite of what is initialized? Meaning, will it execute when !collision returns true?

Just a bit confused since I initialized it to false, and I want this if statement to run when collision is false, but don't know if !collision is the right way to do it or not.

Code as requested. Still confused on what the condition would be. I have collision initialized to false. As a result, I want the statement to be executed when it is false. Should I write if(collision) or if(!collision)?

    boolean collision = false;
    boolean winner = false;

    while(!winner){
        //Main loop where user will be able to move the ships and try to destroy the ship
        //boolean shipAlive = true/false; YET TO ADD!
        //if(!shipAlive) -> winner = true; YET TO ADD!

        //movement of ships
        if(!collision){
            System.out.println("Player 1: L, R, or S? (L = Left, R = Right, S = Stay)");
            String p1Input = IO.readString();
            int k = movement(p1Input, player1);
            while (k == 1){
                System.out.print("Error! Enter either L, R or S: ");
                p1Input = IO.readString();
                k = movement(p1Input, player1);
            }
        }

        collision = fireProjectileUp();
        if(collision){
            winner = true;
        }
Harshil
  • 33
  • 4

6 Answers6

2

Yes, it is the right way to do it:

In if (!someBoolExpr) { ... }, the "then-clause" will run if someBoolExpr == false.

See the JLS §15.15.6: Logical Complement Operator ! for more information:

The value of the unary logical complement expression is true if the (possibly converted) operand value is false, and false if the (possibly converted) operand value is true.

Martijn Courteaux
  • 67,591
  • 47
  • 198
  • 287
1

! represents NOT in Java. So if you have something like

if(!true) {
//doSomething
} else {
//Something else  --- This is executed.
}

if(!false) {
//doSomething --- This is executed.
} else {
//Something else 
}

true and false are the final result of your comparison operations

Pratik
  • 1,122
  • 8
  • 26
0

! means not, so your expression will be translated to if(NOcollision).

Check documentation for further info.

Jordi Castilla
  • 26,609
  • 8
  • 70
  • 109
0

Yes, your if statement will be executed when collision is false.
!false returns true

Shruti
  • 1
0

You've asked a question, but I suspect this is a bit of an XY problem for you. You've asked about clarification of the ! operator, then proceeded to explain exactly what a not operation is.

When constructing your conditions, you want to manipulate the logic so that it evaluates to true when you want it too. For example, I want something to run if there has been a collision.

if(collision)

Simple. This is because this will evaluate to true when you want it too. Now you want something to run if their hasn't been a collision.

if(!collision)

Once again, it evaluates to true if collision is false. Or, when you want it too. The trick here is working out how to express what you want in a way that the compiler understands, and this is done through logical expressions that resolve to some boolean true or boolean false.

EDIT

Just incase, ! operator is simply the opposite of the value. !true is false, !false is true. Going by what I explained above, you can combine this knowledge to create your solution.

christopher
  • 26,815
  • 5
  • 55
  • 89
0

When if(!collision) is executed then first of all it checks the value of collision. Let's assume its is 'false'. Then ! operator converts it to true. opposite of false. Then the value is 'true ' so, if statement will execute its code block.