Short circuit evaluation in mcu world

Hey guys, let say i have following code:

if((somebool==true && somefunction()==true) || anotherfunction()==false) {
// do something
}

just wanted to confirm, as i know if the variable "somebool" is false, is the following function "somefunction" not being triggered but "anotherfunction" going to executed in cpp and javascript. Is it also works in world of mcu's?


another question

switch(x) {
case 1:
{} // <--- notice the brackets, code compiles without them but doesn't work.
 break;
}

cpp and javascript executing that switch statement without brackets, arduino won't. why?

Yes, C++ will short-circuit on the first failed condition, even on Arduinos.

Never once had to use brackets inside a case for a switch in C++.

Try it...

boolean somebool = false;

void setup()
{
  Serial.begin(115200);

  if ((somebool == true && somefunction() == true) || anotherfunction() == false)
  {}
}


void loop()
{}

boolean somefunction()
{
  Serial.println("somefunction");
  return true;
}

boolean anotherfunction()
{
  Serial.println("anotherfunction");
  return true;
}

The brackets aren't required, although if you are declaring variables within the case block then you will need them, or you will run into variable scope issues.

This code compiles and works on my Arduino UNO:

void setup()
{
  Serial.begin(115200);
  delay(200);
  
  int x = 1;
  switch (x)
  {
    case 1:
      Serial.println("The case is executed.");
      break;
  }
}

void loop() {}

It's a snippet out of context. The problem is elsewhere.

That case does nothing. How are you knowing it doesn't get execute?

a7

Thank you guys, very helpful. about the brackets, as @red_car indicated it's a scope issue i guess.

for anyone who interested, that's the actual code which i having problems: osiris/packager.cpp at master · PsyChip/osiris · GitHub

line 525, switch statement works only when encapsulated with brackets. environment: arduino uno r3, arduino ide 1.8.19

maybe it's a platform bug, going to test with atmel studio

This is not what you said before. Do you mean some one case has to be in braces (these [ ] are brackets, BTW) or the entire switch statement?

If one case, which? All?

Very odd no matter what. @red_car was calling your attention to the problem of declaring a variable in case code, you have not done that, so no scope issue obtains here.

a7

void Packager::SetConfig(int id, unsigned int value) {
  switch (id) {
    case conf_boy:
      c.boy = value;
      break;
    case conf_gramaj:
      c.gramaj = value;
      break;
    case conf_tarih:
      c.tarihPush = value;
      break;
    case conf_ribbon:
      c.tarihPull = value;
      break;
  }
  c.updated = millis();
  c.changed = true;
}

This is the code to which you refer. It does not have brackets inside of the case for the switch.

This does not apply to that code.

Nor braces. :wink:

There are some parentheses however.

a7

In UK English these ( ) [ ] { } all are different types of brackets.

Is US programming courses:
( ) - parentheses
[ ] - square brackets
{ } - curly braces

but any can be referred to as brackets as they all bracket things

Not where I learned it. From one of those ppl I guess I have become, who went up the wall when she heard "square brackets" or anything like that. :expressionless:

( ) - parentheses
[ ] - brackets
{ } - braces

square and curly are redundant.

a7

Soda, pop, or cola?

Tonic.

a7

Curly braces

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.