A Loop with an Exit Condition

A Loop with an Exit Condition

Here is a pattern you will see quite often when structuring your code in loop constructs:

01    enum command {  start, stop, wait, quit };02    command instructions;03    int counter = 0;0405    cout < endl < "Enter Command > ";06    instructions = get_command();0708    while(instructions != quit)09    {10        counter += 1;11        cout < endl < "Enter Command > ";12        instructions = get_command();13    }

Notice that lines 5 and 6 are identical to lines 11 and 12.

During code modifications, it is not always clear that these two sets of lines need to be maintained in parallel. You might change line 5 but not realize that line 11 must also be modified to stay in sync with line 5. There is a better way to structure the code that removes redundancies:

01    command instructions;02    int counter = 0;0304    while(1)05    {06        cout < endl < "Enter Command > ";07        instructions = get_command();08        if(instructions == quit)09            break;10        counter += 1;11   }

Notice the redundant lines are no longer present. The code created an infinite loop and, in the middle of the loop, tests the exit condition to break out of the loop. The exit condition is the reverse of the condition coded in the original loop. Instead of instructions != quit it is coded as instructions == quit. The repeating code now goes above the exit condition and all other code from the original loop goes below the condition.

Here is the complete source code for the original and modified versions of the loop:

//*******************************************************************#include#include using namespace std;enum command {  start, stop, wait, quit };command get_command(){    int x = getch();    command cmd = command(x - '0');    switch(cmd)    {        case start:            cout < "START";            return start;        case stop:            cout < "STOP";            return stop;        case wait:            cout < "WAIT";            return wait;        default:            cout < "QUIT";            return quit;    }}// Original code before loop with exit 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;}// Modified code - loop with exit conditionint 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;}
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