Why Not to Use an Exit Within a Loop

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:
Heading photo, Metadata.

What is Metadata?

What is metadata? Well, It’s an odd concept to wrap your head around. Metadata is essentially the secondary layer of data that tracks details about the “regular” data. The regular

XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of

kubernetes logging

Kubernetes Logging: What You Need to Know

Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes

ransomware cyber attack

Why Is Ransomware Such a Major Threat?

One of the most significant cyber threats faced by modern organizations is a ransomware attack. Ransomware attacks have grown in both sophistication and frequency over the past few years, forcing