dimanche 15 avril 2018

If statement with a variable and no expression

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?

Aucun commentaire:

Enregistrer un commentaire