user3526469
user3526469

Reputation: 13

Performing a method() upon exit of a loop java

I have made a for loop with a nested if block. On exit of the loop I want the object to perform an action by calling a method. At the moment I am struggling to find how to do this. Could anyone make any suggestions?. Below is the code I have made.

public void goLoop()
   {
    for (int i = 1; i <= getAmountOfLoops(); i++ )
    {
      if (getPosition() => 25)
      {
         this.setPosition(4);
      }
      else
      {
        this.keepLooping();
        this.setTotalAmountOfLoops(getTotalAmountOfLoops() + 2);    
      }
      jump();   // Do I put the action to perform on exit of loop here?   
    }

Upvotes: 0

Views: 69

Answers (1)

Mohsen Kamrani
Mohsen Kamrani

Reputation: 7457

Just call it after the loop:

for (... ) {

}
action();

BTW change getPosition() => 25 to getPosition() >= 25.

Note: It's always better to be aware of the logic of your program before you start coding, however if you start coding and suddenly encounter a trivial situation like this, just give it a try! and check if it produces the desired result or not.

Upvotes: 2

Related Questions