The syntax of an if
statement is either:
if (expression) statement
or
if (expression) statement else statement
In your code, the statement associated with the else
keyword is the entire switch
statement the goodMsg.push_back(msg);
(which happens to have a case label associated with it).
I can't tell from your question whether that's what you intended, though your indentation suggests that you meant the switch
statement to be independent of the if
/else
.
If that's what you intended, the else
is not useful. Your logic would be more clearly expressed as:
if (condition) break;
case ...
My suggestion: always use curly braces for conditional and loop statements. This lets you avoid having to ask questions like this in the first place:
if (condition) {
statement
}
else {
statement
}
An if
/ else if
/ ... / else
chain is a special case. Syntactically, it's a nested set of if
/else
statements, but it's convention to treat it as a linear chain, writing:
if (condition) {
statement
}
else if (condition) {
statement
}
else {
statement
}
rather than:
if (condition) {
statement
}
else {
if (condition) {
statement
}
else {
statement
}
}
Note that I'm using K&R-style brace placement, with each {
at the end of a line. Another very common style places both {
and }
on lines by themselves. Both choices are valid (I obviously have my own preference), as long as you're consistent.