Switch Statement Brackets

Generally I don't use brackets with the case's of a switch statement. I recently realized you have to if there is something inside the case part that does variable initializations. Is there a prettier or nicer way to get this functionality without the brackets..something that looks cleaner besides having to do brackets in the middle of case statement?

Does that make sense?
1) have the case call a function
or
2) put the variable initialzation outside of the switch statement


Both have performance reprocussions, whereas putting in harmless braces do not. I recommend you just throw braces in there.
Not really unless you want to make an initialize function or something similar.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
switch (var)
{
   case 1:
      commands1; commands2;
      commands3; commands4;
      break;
   case 2:
      {
         commands1; int i =0; int *pj;
         break;
      }
   default:
      break;
}


you mean this?
edit: beat me 2 it.
Last edited on
Yes. well if I am going to put them into just a few case statements, will it hurt if I put them into all by default, or is that bad code quality?
I wouldn't say it's bad, it could be good if the program is quite a large one and memory usage needs to be kept down to a minimum, it does make it somewhat hard to follow in design. But generally you could just do it outside the switch which is far more readable.
Topic archived. No new replies allowed.