#9 Indentation incorrect in a switch statement

open
nobody
None
5
2008-07-02
2008-07-02
Anonymous
No

Hi,

I noticed that if I indent code that has a switch statement which has a for loop in one of the cases, the following cases are indented incorrectly. For example,

void fcn(int x)
{
switch (x)
{
case ABC:
for (int i = 0; i < 3; ++i)
do_first_thing();
break;

case DEF:
do_second_thing();
break;

case GHI:
do_third_thing();
break;
}
}

Note that if the for loop is removed, you get correct indentation:

void fcn(int x)
{
switch (x)
{
case ABC:
// for (int i = 0; i < 3; ++i)
do_first_thing();
break;

case DEF:
do_second_thing();
break;

case GHI:
do_third_thing();
break;
}
}

A workaround is to put a ";" after the ":",

void fcn(int x)
{
switch (x)
{
case ABC:;
for (int i = 0; i < 3; ++i)
do_first_thing();
break;

case DEF:;
do_second_thing();
break;

case GHI:;
do_third_thing();
break;
}
}

Discussion