Explore BrainMass
Share

Explore BrainMass

    How to eliminate a break statement from a for loop

    This content was COPIED from BrainMass.com - View the original, and get the already-completed solution here!

    Using the following code as an example, explain how, in general, one can remove the break statement and replace it with "better" code:
    int count; for (count = 1; count <= 10 ; ++count) { if (count == 5) break; // LINE 5 };
    Suppose we replace the line labeled "LINE 5" with a continue statement. How would you replace the early termination code with "better" structured code?

    © BrainMass Inc. brainmass.com October 10, 2019, 7:30 am ad1c9bdddf
    https://brainmass.com/computer-science/c-sharp/how-eliminate-break-statement-loop-584234

    Solution Preview

    There are multiple ways to eliminate the "break" from this loop. The
    purpose of the "break" statement here is to cause the loop to exit
    when the condition "count == 5" is met. ...

    Solution Summary

    This solution shows how to remove a break statement from a for loop in two different ways. It also provides a short justification for why we would want to do this.

    $2.19