8-bitButterfly
8-bitButterfly

Reputation: 203

Understanding the compiler - a statement that does nothing?

I am interested in understanding how the compiler is interpreting the statement…

Project type is C++ IDE is XCode 3.2 64bit Operating system is Mac OS X 10.6.8

Caution: The code sample is an infinite loop.

In the code sample below or in the attached image. What is the compiler doing if anything. No major error is raised and no minor errors either (given my current compiler settings). Mostly interested in understanding what is happening in case statement C:

    bool nrContinue;
    enum Task {

        A,
        B,
        C,
        D

    };

    nrContinue = 1;
    Task nrTask = A;

    while (nrContinue) {

        switch (nrTask) {

            case A:

                cout << endl << "Processing task: " << A;
                nrTask = B;

                break;
            case B:

                cout << endl << "Processing task: " << B;
                nrTask = C;

                break;
            case C:

                cout << endl << "Processing task: " << C;

                // --------------------------------------------------
                // Below is the statement of my misunderstanding

                D;      // What is going on at this statement

                break;
            case D:

                cout << endl << "Processing task: " << C;
                nrContinue = 0;

                break;
            default:

                cout << endl << "Default case was unexpected.";
                nrContinue = 0;

                break;

        }

    } // loop

Upvotes: 4

Views: 187

Answers (3)

Mike Seymour
Mike Seymour

Reputation: 254411

A line (or more) of code of the form

expression;

is known as an expression statement. The expression is evaluated, and the program's state changes according to the side-effects of the expression. So for example:

nrTask = C;

causes the program to evaluate the expression nrTask = C, which has the side-effect of changing the value of nrTask.

D; evaluates the expression D, which has no side-effects and therefore does nothing.

The language allows any expression to be used as the body of an expression statement; however, a good compiler should issue a warning if it has no effect, since it's almost always a mistake. If you enable warnings (-Wunused-value for just that warning, or better still -Wall -Wextra for many useful warnings), then the compiler should point out your mistake with a warning like "statement has no effect".

Upvotes: 2

Bart van Ingen Schenau
Bart van Ingen Schenau

Reputation: 15758

The statement

D;      // What is going on at this statement

does nothing. Most likely, the compiler will even throw it out with the most basic optimisation settings. And if you increase the warning level of your compiler, it will most likely start to complain that the statement does nothing.

The reason such statements are legal in the first place is probably because nobody has wanted to make the syntax of C++ more complicated than it already is by outlawing these useless statements without prohibiting similar looking, but useful statements.

Upvotes: 0

lurscher
lurscher

Reputation: 26943

in case C, you are missing a nrTask = D; statement so your state machine transitions to that case in the next loop, otherwise it stays indefinitely in state C. the single statement D; does nothing, it just evaluates the constant D as a temporal, which is never assigned to anything or produce any side effect

Upvotes: 1

Related Questions