Skip to main content

In computer programming, a switch, case, select or inspect statement is a type of selection control mechanism used to invoke specific blocks of code based on variable contents.

In computer programming, a switch, case, select or inspect statement is a type of selection control mechanism that exists in most imperative programming languages such as Pascal, Ada, C, C++, C#, Java, and so on. It is also included in several other programming languages. Its purpose is to allow the value of a variable or expression to control the flow of program execution via a multi-way branch (or "goto", one of several labels). The main reasons for using a switch include improving clarity, by reducing otherwise repetitive coding, and (if the heuristics permit) also offering the potential for faster execution through easier compiler optimization in many cases.

Example pseudo-code:

switch (response)
   when "Y"
      // code to execute when variable response="Y"
   when "N"
      // code to execute when variable response="N"          
   else
      // code to execute if no when clauses are true
end switch

Be sure to also include a tag specifying the programming language your question relates to, such as or .