10.2.1 Notes for the C Programmer

The switch statement is also available in the widely used C programming language. There are, however, some differences between the statement in Octave and C

  • Cases are exclusive, so they don’t ‘fall through’ as do the cases in the switch statement of the C language.
  • The command_list elements are not optional. Making the list optional would have meant requiring a separator between the label and the command list. Otherwise, things like
    switch (foo)
      case (1) -2
      …

    would produce surprising results, as would

    switch (foo)
      case (1)
      case (2)
        doit ();
      …

    particularly for C programmers. If doit() should be executed if foo is either 1 or 2, the above code should be written with a cell array like this

    switch (foo)
      case { 1, 2 }
        doit ();
      …

© 1996–2020 John W. Eaton
Permission is granted to make and distribute verbatim copies of this manual provided the copyright notice and this permission notice are preserved on all copies.
Permission is granted to copy and distribute modified versions of this manual under the conditions for verbatim copying, provided that the entire resulting derived work is distributed under the terms of a permission notice identical to this one.
Permission is granted to copy and distribute translations of this manual into another language, under the above conditions for modified versions.
https://octave.org/doc/v5.2.0/Notes-for-the-C-Programmer.html