I was curious if, in Java, you could create a piece of code that keeps iterating a piece of code without the use of a for
or while
loop, and if so, what methods could be used to solve this?

- 21,564
- 12
- 71
- 98

- 169
- 9
-
3The magic word is **recursion**. – laune Jul 13 '14 at 14:04
-
What would recursion do, like how would it be used? – NebulaeGuy Jul 13 '14 at 14:05
-
6Yeah, recursion. It will blow your mind, if not your stack. – Hot Licks Jul 13 '14 at 14:07
-
By `Stack`, are you referring to the stacking of factorials that was mentioned in ComputerPhile's video? https://www.youtube.com/watch?v=7ha78yWRDlE – NebulaeGuy Jul 13 '14 at 14:09
-
We're referring to the stack that keeps track of methods called but not yet returned from. – Hot Licks Jul 13 '14 at 23:22
5 Answers
Look at recursion. A recursive function is a function which calls itself until a base case is reached. An example is the factorial function:
int fact(int n)
{
int result;
if(n==1)
return 1;
result = fact(n-1) * n;
return result;
}

- 7,250
- 1
- 27
- 51
-
This is a brilliant answer! I will mark it as correct as soon as the 9 minutes are up. – NebulaeGuy Jul 13 '14 at 14:08
You could use the Java 8 Streams methods for iterating over the elements of a Collection. Among the methods you can use are filtering methods (get all the elements of a collection that satisfy some conditions), mapping methods (map a Collection of one type to a Collection of another type) and aggregation methods (like computing the sum of all the elements in a Collection, based on some integer member of the Element stored in the collection).
For example - Stream forEach :
List<Element> = new ArrayList<Element>();
...
list.stream().forEach (element -> System.out.println(element));
Or you can do it without a Stream :
List<Element> = new ArrayList<Element>();
...
list.forEach (element -> System.out.println(element));

- 387,369
- 54
- 702
- 768
-
-
I'm not going to mark this as correct, as I would like to see some more solutions, but if I do not receive anymore it will be given to you. – NebulaeGuy Jul 13 '14 at 14:06
Another variant of recursion:
public class LoopException extends Exception {
public LoopException(int i, int max) throws LoopException {
System.out.println( "Loop variable: "+i);
if (i < max)
throw new LoopException( i+1, max );
}
}
Of course this is just a bit of fun, don't ever do it for real.

- 48,926
- 12
- 77
- 104
Java does not have a goto
statement (that's a lie), so that way is a dead end.
But you could always make a piece of code endlessly iterate using recursion. Old factorial function seems to be the favorite, but since it is not an infinite loop, I will go for this simple function:
int blowMyStack(int a) {
return blowMyStack(a + 1);
}
There will be many ways to do this using various features of the language. But it always falls to an underlying recursion.

- 1
- 1

- 21,564
- 12
- 71
- 98
In case you're referring of something like C's goto, the answer is no.
In other cases, you can use recursive functions.

- 2,740
- 17
- 12