I am trying to figure out recursive backtracking, i have good understanding of recursion and till some extent the concept of backtracking too but i am having difficulty understand the chronological order of how things are working when for loop is being used in following code.
public static void diceRolls(int dice) {
List<Integer> chosen = new ArrayList<Integer>();
diceRolls(dice, chosen);
}
// private recursive helper to implement diceRolls logic
private static void diceRolls(int dice,List<Integer> chosen) {
if (dice == 0) {
System.out.println(chosen); // base case
} else {
for (int i = 1; i <= 6; i++) {
chosen.add(i); // choose
diceRolls(dice - 1, chosen); // explore
chosen.remove(chosen.size() - 1); // un-choose
}
}
}
Now i am having problem understand that for example we pass 3 in diceRolls function, it calls the helper method, inside the for loop we add all the value of i (ie 1) after that it calls the method again so does the for loop complete itself before recursing or the method diceRolls(2,chosen) is passed now ? Because if 2 is passed then also the loop would only run once before recursing itself.