# Break inside curly brackets?

**URL:** <https://forum.arduino.cc/t/break-inside-curly-brackets/1359193>\
**Category:** Programming\
**Created:** [March 1, 2025, 12:24pm UTC](https://forum.arduino.cc/t/break-inside-curly-brackets/1359193 "2025-03-01T12:24:12Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![dick-san](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/dick-san/32/1013044_2.png) [@dick-san](https://forum.arduino.cc/u/dick-san)\
**Post date:** [March 1, 2025, 12:24pm UTC](https://forum.arduino.cc/t/break-inside-curly-brackets/1359193/1 "2025-03-01T12:24:12Z")

</div>

Hi,  
I have a bug in this piece of code, i.e. it executes once then hangs.  
It's part of a CNC sketch for my bench drill.  
I use a TV remote for a keypad.

My question: is it OK to put a _break_ (line 5) inside curly backets, & not uniquely at the end of the case?  
Is a better alternative to use _endif_ instead of _if_ on line 7?

TIA

Diksan

```arduino
    case 2192: //Ch- key
      if (simul == HIGH) {
        Serial.println(F("Simul!"));
        blinkBL(3);
        break;
      }
      if (drill_state == 3) { //drill 1 hole if unloaded
        lcd.setCursor(0, 2);
        lcd.print(F(" "));
        lcd.setCursor(0, 1);
        lcd.print(F(" **Drill 1 hole**"));
        Serial.println("Drill 1 hole");
        while (Zposition <= Zdownposition) Zstep_down();
        showXYZ();
        while (Zposition > Zupposition) Zstep_up();
        showXYZ();
      } else {
        Serial.println(F("Can't drill yet! Unload, set Z zero, drill up (C) & down (D)"));
        blinkBL(3);
      }
      break;

```

---

<div class="post-metadata">

**Author:** ![bobcousins](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/bobcousins/32/221530_2.png) [@bobcousins](https://forum.arduino.cc/u/bobcousins)\
**Post date:** [March 1, 2025, 12:34pm UTC](https://forum.arduino.cc/t/break-inside-curly-brackets/1359193/2 "2025-03-01T12:34:30Z")

</div>

> [@dick-san](#):
>
> My question: is it OK to put a _break_ (line 5) inside curly backets, & not uniquely at the end of the case?

It's ok, yes, if a little non-conventional. If I saw this I might wonder if a "case xxx" line had been accidentally deleted.

> [@dick-san](#):
>
> Is a better alternative to use _endif_ instead of _if_ on line 7?

Not sure what you mean here, endif is not a C keyword.

Using an if structure would be equally clear and more conventional IMO.

---

<div class="post-metadata">

**Author:** ![dick-san](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/dick-san/32/1013044_2.png) [@dick-san](https://forum.arduino.cc/u/dick-san)\
**Post date:** [March 1, 2025, 1:39pm UTC](https://forum.arduino.cc/t/break-inside-curly-brackets/1359193/3 "2025-03-01T13:39:58Z")

</div>

erratum: _else if_, not _endif_. My bad!

---

<div class="post-metadata">

**Author:** ![sterretje](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/sterretje/32/391294_2.png) [@sterretje](https://forum.arduino.cc/u/sterretje)\
**Post date:** [March 1, 2025, 1:40pm UTC](https://forum.arduino.cc/t/break-inside-curly-brackets/1359193/4 "2025-03-01T13:40:00Z")

</div>

> [@dick-san](#):
>
> Is a better alternative to use _endif_ instead of _if_ on line 7?

_#endif_ is related to _#ifdef_ and _#if_, not with _if_.

_#endif_. _#ifdef_ and _#if_ are preprocessor statements and are applied at compile time influencing what part of a code will be compiled, not at runtime.

---

<div class="post-metadata">

**Author:** ![sterretje](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/sterretje/32/391294_2.png) [@sterretje](https://forum.arduino.cc/u/sterretje)\
**Post date:** [March 1, 2025, 1:41pm UTC](https://forum.arduino.cc/t/break-inside-curly-brackets/1359193/5 "2025-03-01T13:41:54Z")

</div>

> [@dick-san](#):
>
> erratum:

That will work if it suites your needs.

---

<div class="post-metadata">

**Author:** ![alto777](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/alto777/32/317385_2.png) [@alto777](https://forum.arduino.cc/u/alto777)\
**Post date:** [March 1, 2025, 1:44pm UTC](https://forum.arduino.cc/t/break-inside-curly-brackets/1359193/6 "2025-03-01T13:44:22Z")

</div>

> [@dick-san](#):
>
> is a better alternative to use [_else if_] instead of _if_ on line 7?

There's a style thing that says _return_ should appear once in a function, at the end.

Sometimes doing this makes thing less clear or necessitates code convolutions.

I think the same might be said about _break_. Here _else if_ is easy enough to do, and allows staying with the same convention.

I've not seen that hammered on like is done to seeing _return_ all over the place.

a7

---

<div class="post-metadata">

**Author:** ![PaulRB](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/paulrb/32/216330_2.png) [@PaulRB](https://forum.arduino.cc/u/PaulRB)\
**Post date:** [March 1, 2025, 2:11pm UTC](https://forum.arduino.cc/t/break-inside-curly-brackets/1359193/7 "2025-03-01T14:11:15Z")

</div>

> [@dick-san](#):
>
> Is a better alternative to use _endif_ _else if_ instead of _if_ on line 7?

That depends on what you want the code to do! It will make it behave differently. If `simul` is not `HIGH`, do you still want the code to check `drill_state` ? If not, is that what the extra `break` is for? In that case, removing the extra `break` and using `else if` would be a better way to structure the code in my opinion.

You can't avoid using `break` in `switch-case` statements in C. But apart from that, I would recommend keeping use of `break` to an absolute minimum. Same goes for `return`.

---

<div class="post-metadata">

**Author:** ![dick-san](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/dick-san/32/1013044_2.png) [@dick-san](https://forum.arduino.cc/u/dick-san)\
**Post date:** [March 1, 2025, 2:37pm UTC](https://forum.arduino.cc/t/break-inside-curly-brackets/1359193/8 "2025-03-01T14:37:54Z")

</div>

I changed the code to use _else if_. No more bug!

I will now check to see if there are any more of these in the sketch 😉

Dick-san

---

<div class="post-metadata">

**Author:** ![gfvalvo](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/gfvalvo/32/1013044_2.png) [@gfvalvo](https://forum.arduino.cc/u/gfvalvo)\
**Post date:** [March 1, 2025, 2:43pm UTC](https://forum.arduino.cc/t/break-inside-curly-brackets/1359193/9 "2025-03-01T14:43:41Z")

</div>

> [@dick-san](#):
>
> I changed the code to use _else if_. No more bug!

There was nothing wrong with the original snippet you posted. The "bug" was elsewhere in the code that you didn't post. So, what ever changes you made either fixed it somewhere else or simply masked it to reappear later.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [August 28, 2025, 2:44pm UTC](https://forum.arduino.cc/t/break-inside-curly-brackets/1359193/10 "2025-08-28T14:44:37Z")

</div>

Closed on August 28, 2025, 2:44pm UTC

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