educative.io

Charge codeis wrong

In the question it is mentioned or condition but in the code we are checking for and condition which is not correct is what I believe.
class ChargingRobotDemo {

public static void charge(SimpleRobot r, int maxDist) {

int i = 0;

while (i < maxDist && !r.blocked() ) {

  r.forward();

}

}

public static void main(String args[]) {

SimpleRobot r = new SimpleRobot();  

r.forward();

r.right();

charge(r, 3);

r.left();

charge(r, 20);

}

}

Hi @sanath_thantri

These lines of code are implementing the while loop,

while (i < maxDist && !r.blocked() ) {

  r.forward();

}

which says that run the loop till i is less than maxDist and r is not blocked, where blocked is a user-defined function. So nothing is wrong with the code.

If you still have any confusion, feel free to ask.