8

What is the Java equivalent of the while/else in Python? Because it doesn't work in Java. The first chunk was my python code and the second portion is my attempt to translate it into Java. Edit: tring to replicate while-else

  while temp.frontIsClear():
    if temp.nextToABeeper():
       temp.pickBeeper()
       count += 1
    temp.move()
 else:
    if temp.nextToABeeper():
       temp.pickBeeper()
       count += 1
 print "The count is ", count

Java Attempt

  Robot temp = new Robot();
  int count = 0;
  while (temp.frontIsClear())
  {
     if (temp.nextToABeeper())
     {
        temp.pickBeeper();
        count += 1;
     }
     temp.move();
  }
  else
  {
     if (temp.nextToABeeper())
     {
        temp.pickBeeper();
        count += 1;
     }
  }
  print ("The count is ", count);
MissSprezzatura
  • 183
  • 2
  • 3
  • 13
  • 2
    there isn't such a thing in Java, you will just have to do if(!temp.frontIsClear()) instead of the else – RAZ_Muh_Taz Apr 07 '17 at 16:38
  • Your python code is broken anyway, since the while block doesn't contain a break. – Alex Hall Apr 07 '17 at 16:40
  • I didn't know there was such a thing in Python, but I don't see the point of it. How is `white(){..} else {code}` different from `while(){..}; code`? – Spencer Wieczorek Apr 07 '17 at 16:40
  • 1
    @SpencerWieczorek The top answer to [this SO question](https://stackoverflow.com/questions/3295938/else-clause-on-python-while-statement) gives an answer to your question. – AntonH Apr 07 '17 at 16:43
  • 1
    Possible duplicate of [while-else-loop](http://stackoverflow.com/questions/10791030/while-else-loop) – Bubletan Apr 07 '17 at 16:51
  • @Bubletan: Not a good dupe. People trying to replicate the behavior of Python's while-else won't find anything useful there. – user2357112 Apr 07 '17 at 17:12
  • It's worth noting that in the given example, the else statement on the `while` loop is pointless as nothing ever `break`s out of the `while` loop. The given python could equivalently be written with the content of the `else` block moved one level higher. – DRayX Nov 18 '20 at 16:55

5 Answers5

12

The closest Java equivalent is to explicitly keep track of whether you exited the loop with a break... but you don't actually have a break in your code, so using a while-else was pointless in the first place.

For Java folks (and Python folks) who don't know what Python's while-else does, an else clause on a while loop executes if the loop ends without a break. Another way to think about it is that it executes if the while condition is false, just like with an if statement.

A while-else that actually had a break:

while whatever():
    if whatever_else():
        break
    do_stuff()
else:
    finish_up()

could be translated to

boolean noBreak = true;
while (whatever()) {
    if (whateverElse()) {
        noBreak = false;
        break;
    }
    doStuff();
}
if (noBreak) {
    finishUp();
}
user2357112
  • 260,549
  • 28
  • 431
  • 505
4

Just use one more if statement:

if (temp.nextToABeeper())
    // pick beer
} else {
    while (temp.frontIsClear()) { /* your code */ }
}

Or:

if (temp.frontIsClear())
    while (temp.frontIsClear()) { /* your code */ }
} else if (temp.nextToABeeper()) {
    // pick beer
}
ghostprgmr
  • 488
  • 2
  • 11
2

If you look at the Java Backus–Naur form Grammar (Syntax Specification), else never follows a while.

Your solution needs to be modified accordingly. You can put the while in an else, that way you handle the if statement.

if temp.nextToBeeper() {
     //handle
 } else {
      while(temp.frontIsClear()) {
         //handle
      }
 }
Jud
  • 1,324
  • 3
  • 24
  • 47
2

Try this:

Robot temp = new Robot();
int count = 0;

if (temp.frontIsClear())
{
  while (temp.frontIsClear())
  {
     if (temp.nextToABeeper())
     {
        temp.pickBeeper();
        count += 1;
     }
     temp.move();
  }
} 
else if (temp.nextToABeeper())
{
   temp.pickBeeper();
   count += 1;
}
print ("The count is ", count);
Chris Hamilton
  • 555
  • 5
  • 22
0

In Java

if is a conditional statement .

But

while is loop that is iterate again an again and stop itself when falsecondition occurred .

Chetan Joshi
  • 5,582
  • 4
  • 30
  • 43