Questions tagged [switch-statement]

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 .

11704 questions
3
votes
3 answers

Is there any way to have switch that checks 2 parameters?

I am working on my school project and was wondering If I could make my code look cleaner by replacing: switch (screen) { case 0: switch (language) { case 0: std::cout << "1 - Start new game" << std::endl; …
3
votes
2 answers

strings.Contains in switch-case GoLang

Is it possible to use strings.Contains in switch case? something like: func function(str string){ switch str { case "str1": ... case strings.Contains("test"): ... default: ... } } Edit: its an example,…
S_Nissan
  • 69
  • 1
  • 9
3
votes
5 answers

Change img [src] specifically with ngSwitch

I want to change img tag src based on a condition. I am using ternary operator right now; I want to ask is there a way to use ngSwitch instead (WITHOUT REPEATING the img tag). Here is my code:
3
votes
5 answers

Best way to build object from delimited string (hopefully not looped case)

this question feels like it would have been asked already, but I've not found anything so here goes... I have constructor which is handed a string which is delimited. From that string I need to populate an object's instance variables. I can easily…
Tom Duckering
  • 2,727
  • 1
  • 23
  • 27
3
votes
1 answer

Switching custom views programmatically (pushing and popping)

In iOS, my views work individually but I can't switch between them. Now after a lot of google-ing around I've fond that the navigation based app would work great with the stack for views. The problem is all my views are nib/xib-less and custom…
Krunch
  • 85
  • 1
  • 9
3
votes
4 answers

Why Switch requires statement but accepts expressions

I'm studying switch expressions and I think I found a weird behaviour: public static boolean isTimeToParty(Day day) { switch (day) { case MONDAY -> dog(); //expression allowed case TUESDAY -> 2 + 2; //error: not a statement …
3
votes
2 answers

Match/Case not condition

How do I use a not in the new match/case structure? a = 5 match a: case not 10: print(a) This produces an error. How would I correctly syntax this?
3
votes
0 answers

How to know what are valid and missing cases in this switch assembly code

I have this homework from school and I do not get how to know which are valid and missing cases for this switch statement. I mean, I know that the first case is going to be 50 and I will have cases from 50 to 55 but how can I know what statement…
3
votes
2 answers

How do I render components with a switch in Svelte?

I would like to conditionally render components with a switch case statement in Svelte like so: // index.svelte