1

I came across these if statements while watching a Java pong game tutorial video:

boolean upAccel, downAccel; double y, velY; public HumanPaddle() { upAccel = false; downAccel = false; } public void setUpAccel(boolean input) { upAccel = input; } public void setDownAccel(boolean input) { downAccel = input; } // moves the paddle public void move() { /* What does the 'if(upAccel){ }' expression do..? */ if(upAccel) { velY -= 1; } if(downAccel) { velY += 1; } y = y + velY; } 

So I understand that the setUpAccel and setDownAccel methods accept a boolean input which can either be true or false. However, I experimented with the if statements - and changed if(upAccel) to if(upAccel = true). Java didn't see the expression as the same thing, so I realized that the two expressions were different!

My question is, what does the if(upAccel) expression test out?

3
  • 1
    if (x == true) is equivalent to if (x) when x is a boolean. Commented Apr 15, 2018 at 15:07
  • If statements evaluate a boolean expression. Boolean variables can be used as boolean expressions. Commented Apr 15, 2018 at 15:07
  • 1
    = is assignment. == is comparison. So if (upAccel = true)... is the equivalent of upAccel = true; if (upAccel)... Commented Apr 15, 2018 at 15:13

2 Answers 2

1
/* What does the 'if(upAccel){ }' expression do..? */ if(upAccel) { velY -= 1; } 

it will evaluate to true can be rewritten as

/* What does the 'if(upAccel){ }' expression do..? */ if(upAccel==true) { velY -= 1; } 
Sign up to request clarification or add additional context in comments.

Comments

0

The statement (upAccel) checks if the condition is true then it continues to perform velY -=1. (upAccel) is the same as (upAccel == true). The same goes with downAccel.

2 Comments

My bad, I was using one equal sign - which is used to ASSIGN variables, not set them. Thanks for the clarification.
You're welcome, good luck.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.