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?
if (x == true)is equivalent toif (x)whenxis a boolean.=is assignment.==is comparison. Soif (upAccel = true)...is the equivalent ofupAccel = true; if (upAccel)...