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
A line (or more) of code of the form
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:
causes the program to evaluate the expression
nrTask = C, which has the side-effect of changing the value ofnrTask.D;evaluates the expressionD, 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-valuefor just that warning, or better still-Wall -Wextrafor many useful warnings), then the compiler should point out your mistake with a warning like “statement has no effect”.