Why Not to Use an Exit Within a Loop

This recent tip gives advice on how to include an exit within a loop to avoid repeated code. While avoiding repeated code is prudent, using an exit from a loop is not. Using the exit can result in ill-defined variable status.

One of the first things students are told about creating “well-structured code” is not to use an exit or break to jump out of a loop. This is equivalent to using a GOTO statement and leads to code with unplanned side effects.

You can avoid both duplicated lines of code and the use of exit by using the apropriate loop control, as illustrated in the following code:

int main(){    command instructions;    int counter = 0;    cout < endl < "Enter Command > ";    instructions = get_command();    while(instructions != quit)    {        counter += 1;        cout < endl < "Enter Command > ";        instructions = get_command();    }    cout < endl < "entered " < counter + 1 < " commands" < endl;    return 0;}

The problem is that the lines:

    cout < endl < "Enter Command > ";    instructions = get_command();

occurr before and within the loop. This is a valid concern. However, this pattern should be taken as a very strong indication that a do .. while( ) loop and not a while () { }; is needed.

The original tip gave the following code:

int main(){    command instructions;    int counter = 0;     while(1)     {         cout < endl < "Enter Command > ";         instructions = get_command();                          if(instructions == quit)             break;         counter += 1;     }     cout < endl < "entered " < counter + 1 < " commands" < endl;}

This should be changed as shown below:

int main(void){    command instructions;    int counter = 0;     do     {         cout < endl < "Enter Command > ";         instructions = get_command();                          if(instructions == quit)             break;         counter += 1;     }while (command != quit);     counter -= 1;     cout < endl < "entered " < counter + 1 < " commands" < endl;}

Note also that the correct definition of main uses void in the argument list. This makes the code portable.

Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

The Latest

microsoft careers

Top Careers at Microsoft

Microsoft has gained its position as one of the top companies in the world, and Microsoft careers are flourishing. This multinational company is efficiently developing popular software and computers with other consumer electronics. It is a dream come true for so many people to acquire a high paid, high-prestige job

your company's audio

4 Areas of Your Company Where Your Audio Really Matters

Your company probably relies on audio more than you realize. Whether you’re creating a spoken text message to a colleague or giving a speech, you want your audio to shine. Otherwise, you could cause avoidable friction points and potentially hurt your brand reputation. For example, let’s say you create a

chrome os developer mode

How to Turn on Chrome OS Developer Mode

Google’s Chrome OS is a popular operating system that is widely used on Chromebooks and other devices. While it is designed to be simple and user-friendly, there are times when users may want to access additional features and functionality. One way to do this is by turning on Chrome OS