While we loop through questPartyMembers, let's see whether we can identify when a certain element is iterated over and add a special debug log just for that case:
- Add an if statement below the debug log in the for loop.
- In the if statement's condition, check whether the current questPartyMember list matches "Merlin the Wise".
- If it does, add a debug log of your choice:
The console output should look almost the same, except that there is now an extra debug log—one that only printed once when it was Merlin's turn to go through the loop. More specifically, when i was equal to 1 on the second loop, the if statement fired and two logs were printed out instead of just one:
Using a standard for loop can be highly useful in the right situation, but there's seldom just one way to do things in programming, which is where the foreach statement comes into play.