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
1 answer

C - Switch-case prints case twice

I have written the following switch-case: char input; int run = 1; while(run){ printf("Would you like to update the student's name? Enter Y or N (Y=yes, N=no)\n"); input = getchar(); switch (input) { …
3
votes
2 answers

While Loop with a Nested Switch in Objective-C

I've just started learning Objective-C. I'm doing one of the standard calculator exercises. It's supposed to create an adding machine (e.g., input the operator and number, display the result each time). But I messed up something, and I think it has…
ctaggart
  • 166
  • 1
  • 10
3
votes
1 answer

Why can't I use enums for switch-case-statements?

I want to use enum-constants for switch-case-statements. I am using following enum/class: public enum Cons { ONE(1), TWO(2); private final int val; private Cons(final int newVal) { val = newVal; } public int getVal() { …
Naboki
  • 85
  • 1
  • 5
3
votes
1 answer

C switch case values cannot be modified within the switch (not constant)

I am trying to create bounce dynamics in a game that I'm programming on an arduino uno. I can create a series of nested ifs but I've heard that a switch is faster. I know that the case values are specified to be constants, but I'm curious if it's…
Mikeologist
  • 438
  • 4
  • 11
3
votes
3 answers

Multiple chars in SWITCH CASE in C

I have a school project and I'm working on a menu where the users chooses what he wants to do. I want the choice variable to be a char, not an int. Can I add multiple chars in a single switch case? I searched for a solution but I only found one when…
iyy0v
  • 51
  • 1
  • 5
3
votes
1 answer

esp8266 internet switch problems

I am trying to make a door relay switch system that i am able to operate from anywhere via port forwarding. I have found a very helpfull guide and code where i based my program…
3
votes
2 answers

Using enum as a variable (?)

I have a problem about some simple code in console. Precisely I created a public enum called Year which contains 4 seasons (obvious). I want the program to ask at the beginning what is the season of the year and then generate an answer to each…
3
votes
1 answer

How to properly use C# 8.0 switch case

I've tried to implemented c# 8.0 switch-case but it's not working unfortunatelly, I want to if case is satisfied to return a specific string for that satisfied case in switch expression. Here's my code: public static void GetErrMsg(Exception ex) => …
Roxy'Pro
  • 4,216
  • 9
  • 40
  • 102
3
votes
2 answers

Calculate hits per day

I need help to implement hits per day based on the user selected package. This is what I made so far but it's not working properly: Entity: @Entity @Table(name = "users") public class UsersModel implements Serializable { @Column(name = "plan") …
Peter Penzov
  • 1,126
  • 134
  • 430
  • 808
3
votes
5 answers

Match Enum Values With A Class Fields

I have an enum and POJO classes in JAVA. In enum class, each enum value matches with variables of POJO classes... And then I want to create a relationship between two classes. Enum Class: public enum MyEnum { FIELD1, FIELD2, FIELD3, …
Sha
  • 921
  • 17
  • 46
3
votes
2 answers

Can C# 8 new switch replace a code block containing multiple ? : ? : expressions?

Here's an example of what I am doing now: return shpc == 0 ? "Currently, based on your selection below, you have not yet identified any hidden cards in your card deck." : shpc == 1 ? "Currently, based on your selection below, you have one…
Samantha J T Star
  • 30,952
  • 84
  • 245
  • 427
3
votes
5 answers

How to switch between "possible" type of an object?

Possible Duplicate: C# - Is there a better alternative than this to ‘switch on type’? My company legacy code has something as follow public override Uri GetUri(object obj) { if ((obj is Entry) || (obj is EntryComment)) { // } …
Delta76
  • 13,931
  • 30
  • 95
  • 128
3
votes
1 answer

Best/Fastest way to NULL check multiple file pointers?

I was wondering what would be the best and/or fastest way to NULL check multiple file pointers and rule out the 'bad' (NULL) ones? Can this be achieved via the switch statement? My 'normal/basic' approach would be: FILE *fp1, *fp2, *fp3; fp1 =…
annoyingnoob
  • 63
  • 1
  • 5
3
votes
1 answer

render a switch button on in material table when i am editing

hi guys i am using material table : (the switch is from material-ui, a simple toggle button) columns: {[{ title: 'Name', field: 'name' }, { title: 'Status', field: 'status', type: 'boolean', render: rowData =>
3
votes
10 answers

cleaner way of mixing PHP switch case with html

i have this php block " alt="" />
Matt Elhotiby
  • 43,028
  • 85
  • 218
  • 321