Will Switch Case code on same line run ?

The following compiles in the IDE without errors, but waiting for an Arduino to arrive to test if it will run.

Will these 2 code formats both run the same and produce the same result ?

switch (x) {
  case 0: Button1active = 1; break;
  case 1: Button2active = 1; break;
  case 2: Button3active = 1; break;
  case 3: Button4active = 1; break;
}

switch (x) {
  case 0: 
    Button1active = 1; 
  break;
  case 1: 
    Button2active = 1; 
  break;
  case 2: 
    Button3active = 1; 
  break;
  case 3: 
    Button4active = 1; 
  break;
}

The programming language (C++) does not care about line breaks (if they are not in the middle of a statement) nor the number of spaces and tabs. The result is exactly the same

Will these 2 code formats both run the same and produce the same result ?

Yes but it would be neater to put the ButtonXactive values in an array and use the value of x to set the appropriate one to 1. No need for a switch/case at all.

And to add to that, adding the break; on the same line is excepted by most users if it's indeed a bunch of one line statements. If not, stick to the "nothing follows a ;"-rule. I would never put the break on the same line just in case I want to extend the case and it becomes multiple lines.

And more about the code, If you do have a list of variables called Button1active, Button2active, Button3active and Button4active your better off with arrays. Actually, 99,9% of the time the moment you want to number variables it's easier to switch to an array. The hole piece of code simply become

isButtonActives[x] = 1; // or true because that would even make more sense

septillion:
And to add to that, adding the break; on the same line is excepted by most users if it's indeed a bunch of one line statements.

Hi septillion

are you saying the following 2 would do the same also ?

case 0: Button1active = 1; break;

case 0: Button1active = 1;

I am guessng that the break statement will cause the rest of the case conditions to be skipped, and without the break statement each of the remaining case conditions would be evaluated until the end of the switch.

DaveO:
are you saying the following 2 would do the same also ?

No I'm not. You're right, you need the break.

What I'm saying is that if the switch looks like

switch (x) {
  case 0: Button1active = 1; break;
  case 1: Button2active = 1; break;
  case 2: Button3active = 1; doStuff(); doMoreStuff(); break;
  case 3: Button4active = 1; break;
}

it will still work but is no longer exepted by users. Aka, that's ugly / bad readable code. Then switch to

switch (x) {
  case 0: 
    Button1active = 1; 
  break;
  case 1: 
    Button2active = 1; 
  break;
  case 2: 
    Button3active = 1; 
    doStuff(); 
    doMoreStuff(); 
  break;
  case 3: 
    Button4active = 1; 
  break;
}

To keep the whole thing readable.

But more important, did you get the huge tip of using arrays? Which reduces this all to a single line :wink:

septillion:
But more important, did you get the huge tip of using arrays? Which reduces this all to a single line :wink:

Hi

Yes, Thank You. I did get the pointers about the arrays and I am going to use arrays, but I still just wanted to know the answer to the question.

I understand your point about the readability and 'accepted norm' way of coding.

Thanks for your input. Much appreciated.