python - While else statement equivalent for Java? -


what java equivalent of while/else in python? because doesn't work in java. first chunk python code , second portion attempt translate java. edit: tring 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 ", 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 ", count); 

the closest java equivalent explicitly keep track of whether exited loop break... don't have break in code, using while-else pointless in first place.

for java folks (and python folks) don't know python's while-else does, else clause on while loop executes if loop ends without break. way think executes if while condition false, if statement.

a while-else had break:

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

could translated to

boolean nobreak = true; while (whatever()) {     if (whateverelse()) {         nobreak = false;         break;     }     dostuff(); } if (nobreak) {     finishup(); } 

Comments

Popular posts from this blog

c# - Update a combobox from a presenter (MVP) -

How to understand 2 main() functions after using uftrace to profile the C++ program? -

How to put a lock and transaction on table using spring 4 or above using jdbcTemplate and annotations like @Transactional? -