notexOPENOverview
Notex/Languages/Code Review: switch and goto
02.17LESSON 17 OF 19 · 1 MIN READ

Code Review: switch and goto

Always free to read
  • A break keyword only breaks out of its immediate scope. If you have a switch statement inside a for loop, a break in one of the switch statement’s case clauses breaks out of the case, not the for loop. Use a labeled break if you want to exit the for loop from a case.

  • Prefer a blank switch over a series of if and else blocks when the cases are related comparisons. The switch signals the relationship, aligns the conditions, and makes the default explicit.

  • Avoid fallthrough. While it can be helpful in rare cases, reconsider your implementation if you find yourself using it.

  • Use goto only when the alternatives, like Boolean flags or code duplication, make the code’s logic harder to understand.

Progress is saved on this device.
KEEP EXPLORING

One note leads to another.

Back to the library →
⌕
Search across the entire library ⌘ K