What you need to understand is how the comb2 method works. Basically, it calls itself. This is called recursion.
When you call comb2("1234"), the result is a call to comb2("","1234").
- comb2("","1234") prints "" (which has no effect), then starts a loop through the end of the string ("234"). The first thing it does in this loop is call comb2("1","234").
- comb2("1", "234") prints "1", then starts a loop through the end of the string ("234"). The first thing it does in this loop is call comb2("12","34").
- comb2("12", "34") prints "12", then starts a loop through the end of the string ("34"). The first thing it does in this loop is call comb2("123","4").
- comb2("123", "4") prints "123", then starts a loop through the end of the string ("4"). The first thing it does in this loop is call comb2("1234","").
- comb2("1234", "") prints "1234", then starts a loop through the end of the string (""). Since there is nothing to do, it returns immediately to its caller : comb2("123","4").
- comb2("123", "4") now goes to the next step in its loop. Since there is none, it returns.
- comb2("12", "34") now goes to the next step in its loop. It calls comb2("124","").
- comb2("124", "") prints "124", then starts a loop through the end of the string (""). There is nothing to do, so it returns immediately.
This is how you get from "1234" to "124". Hopefully, from there, you shoud be able to understand the whole execution.