GuruKulki
GuruKulki

Reputation: 26428

Is there any way of executing the already executed code in java

Is there any way of executing the already executed code in java.

for example in the main method i have int variable a = 10; and later point in time say suppose at the end of execution i will check some conditions on its(a's) value and if the condition fails then i want it to execute from the beginning.

there may be some other scenarios also, right now i have this.

Edit: without looping and methods. which i know. like goto statements in c++. and dont think only for the above stated example there might be some other example which might need the way i wanted.

Upvotes: 0

Views: 264

Answers (4)

Skip Head
Skip Head

Reputation: 7760

The correct answer is: No, there is no way to do that in Java. You would have to use a loop or recursive method.

Upvotes: 1

Evan Teran
Evan Teran

Reputation: 90543

why can't you just do something like this?

   do {
       a = processSomething();
   } while(a != 10);

EDIT: if you can't use a proper loop construct, then recursion is your only real choice.. something like this:

int rFunction(int a) {
    // if we found our needed value, return it
    if(a == 10) {
        return a;
    }

    a = whatever();

    rFunction(a); // call again with new value   
}

Upvotes: 4

Nicholas
Nicholas

Reputation: 7521

Put it in a function, or create a loop.

Upvotes: 2

user177800
user177800

Reputation:

while(some_check_of_some_sort)
{
   // do stuff
}

Upvotes: 0

Related Questions