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?

MattGotJava
  • 185
  • 3
  • 14

2 Answers2

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; 
   }
Roushan
  • 4,074
  • 3
  • 21
  • 38
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.

amk
  • 70
  • 7