Why the jump crosses initialization
A switch evaluates its controlling expression and transfers execution to the matching label. When no case matches, it can jump to default. These transfers bypass statements between the opening brace and the selected label. If one of those statements initializes a local variable, jumping over the declaration would leave control in a scope where the variable exists without having initialized it.
This program declares score after case 1. A jump to case 2 could skip that initialization while remaining in the switch block. MSVC identifies the crossed initialization with C2360. The exact source location and note can vary with the code around the label.
#include <iostream>
int main() {
int choice = 2;
switch (choice) {
case 1:
int score = 90;
std::cout << score << '\n';
break;
case 2:
std::cout << "second case\n";
break;
default:
break;
}
return 0;
}
Build with cl /std:c++17 /EHsc /W4 /permissive- main.cpp. MSVC reports:
error C2360: initialization of 'score' is skipped by 'case' label
note: see declaration of 'score'
error C2361: initialization of 'score' is skipped by 'default' label
note: see declaration of 'score'GCC diagnostic core for the same mistake. GCC reports it once for case 2 and once for default, and a note names the variable whose initialization the jump skips:
error: jump to case label
note: crosses initialization of 'int score'
Give a case its own scope
Put braces around the statements and local variables for one case. The declaration then belongs to that nested block. A switch jump can enter another case without entering the block whose local variable needs initialization. Keep each case's break inside its braces so control exits the switch as intended.
This version gives score a scope that belongs only to case one:
#include <iostream>
int main() {
int choice = 1;
switch (choice) {
case 1: {
int score = 90;
std::cout << score << '\n';
break;
}
case 2:
std::cout << "second case\n";
break;
default:
break;
}
return 0;
}
The block begins after the label and ends before the next one. The local variable does not remain in scope across other labels. This is the usual fix when a value is used by only one case. It also lets you reuse a short variable name in another case because each block has its own scope.
90
Move shared declarations before the switch
If several cases need the same variable, declare and initialize it before the switch. Every possible label then starts after the initialization has run. Choose a value that makes sense for all cases, and avoid a declaration that hides which branches actually use it.
Here score is initialized before control can jump to any label:
#include <iostream>
int main() {
int choice = 1;
int score = 90;
switch (choice) {
case 1:
std::cout << score << '\n';
break;
case 2:
std::cout << score / 2 << '\n';
break;
default:
break;
}
return 0;
}
This form keeps one shared value available across branches. Use braces instead when the variable belongs to one branch. Both approaches prevent a jump from bypassing initialization, but the scope should reflect which cases use the value.
Do not try to fix this diagnostic by adding a semicolon after a case label or by moving only the label. Labels mark possible entry points; they do not provide declaration scope. The compiler checks every path to each label, including paths that do not follow the source's visual order. A valid fix changes the scope or places initialization before those paths.
Exercise
Update the switch so the variable message is local to the case that prints it. The program should print a ready message when choice is one. Build it with C++17. Expected output:
Ready
Show one possible solution
#include <iostream>
int main() {
int choice = 1;
switch (choice) {
case 1: {
const char* message = "Ready";
std::cout << message << '\n';
break;
}
default:
break;
}
return 0;
}
Related guides
- Find a missing semicolon
- Fix missing include files
- Understand type declarations
- Search compiler diagnostics
Keep learning with CppPath
Continue with the switch Statements lesson. The full lesson includes a quiz and a checked coding challenge with a free account.