Ad
  • Custom User Avatar

    Yes, you can use a break clause in order to stop the loop.

    But, in my opinion, breaks and gotos are not a "good practice".

    You could also place the return statement after the "missingLetter+=1" (this is the line that you use for find the correct number of the letter).

    Another option, you can use booleans. You can declare a boolean found = false; at the beginnig, use it in the loop (for(int i = 0; i < array.length-1 && !found; i++)) and, when you found the letter, write found = true;

    So, the next time you return to the loop condition, !found will be false, so yo won't enter again, and you save a lot of time.

    I hope was helpful :D

  • Custom User Avatar

    Do you think it's efficient to use a 'return' clause inside a loop?
    Would not it be better to use boolean conditions?

  • Custom User Avatar

    You are watching all array's elements, but you only need to see elements until you find the missing one.
    If the array had had 1000 elements and the missing one was found in the 1st iteration, you would be doing an extra effort.
    Use a boolean or similar :D.