Avatar ·

Why can't you declare variables inside a switch block without curly braces in C++?

Hello everyone! I started learning C++ and encountered a very strange error when using switch. I needed to write a simple calculator that performs different operations depending on the entered character. Here is my code: ```cpp #include using namespace std; int main() { char op; int a = 10, b = 5; cout << "Enter operation (+, -, *, /): "; cin >> op; switch (op) { case '+': int result = a + b; // Error! cout << result; break; case '-': // ... break; } return 0; } ``` The compiler gives an error: "cannot jump from switch statement to this case label" or something like "initialization of 'result' is skipped by 'case' label". I understand that switch