cpp Case value is not a constant expression

#include <iostream>
using namespace std;

int main() {
    int caseVal = 2;
    switch (caseVal) {
        case 1:
            cout << "Case 1" << endl;
            break;
        case caseVal + 1:
            cout << "Case 3" << endl;
            break;
        default:
            cout << "Default case" << endl;
            break;
    }
    return 0;
}

Explanation:

  1. Include necessary header files (iostream in this case) for input/output operations.
  2. Define the main() function, the entry point of the program.
  3. Declare an integer variable caseVal and assign it the value 2.
  4. Use a switch statement to check the value of caseVal.
  5. In the switch statement:
  6. There's a case labeled 1 that prints "Case 1" if caseVal is 1. However, this case will be skipped as caseVal is 2.
  7. There's another case labeled caseVal + 1 which would evaluate to 3 in this instance (2 + 1 = 3). This case will execute and print "Case 3".
  8. The default case is included for any value that doesn't match the defined cases. In this scenario, it won't be executed as one of the defined cases will match.
  9. The program ends with a return 0;, indicating successful completion. ```