Skip to main content

Questions tagged [switch-statement]

Filter by
Sorted by
Tagged with
-1 votes
1 answer
20 views

The reason I wanted to ask is because of some code from Undertale that is responsible for choosing which dialogue set to use. It works something like this: switch (id) { case 0: msg[0] = &...
Bunabyte's user avatar
  • 641
2 votes
3 answers
977 views

I know there are already some questions about replacing if else with polymorphism, for example: Applying Replace Conditional with Composition in functional programming Is it wrong to use any type of ...
wcminipgasker2023's user avatar
0 votes
1 answer
86 views

I have the following code in C that reads data off a char array and makes decisions on what variables should some pointers point to. char preset1[20]; char preset1Name[20]; int preset1Temp; int ...
HFOrangefish's user avatar
3 votes
2 answers
861 views

It is said that visitor pattern is applicable to problems where objects rarely change but we add actions on those objects more frequently. What if the objects are changing too though? For example we ...
Ehsan Poursaeed's user avatar
1 vote
2 answers
1k views

I am practicing design patterns and OO concepts such as inheritance in java and I'm writing an application that represents a vending machine. I have two questions focused on ideal structure and design ...
Bob Jones's user avatar
28 votes
7 answers
8k views

This doubt is about Switch Statements from Chapter 3: Functions of the book named Clean Code Here we have a function: public Money calculatePay(Employee e) throws InvalidEmployeeType { switch (e....
The Chinky Sight's user avatar
2 votes
2 answers
1k views

Essentially I've got a bunch of formulas in two giant methods in a class designed to do math transformations and evaluations to multiple inputs. Where the inputs are actually lists of inputs (as there ...
Akumaburn's user avatar
  • 131
0 votes
1 answer
666 views

I have been using a pattern in a lot of places (mainly C#) that I would like to know the name of. Here is an example of it in C#: public enum ThingType { A, B, C } public interface ...
Romen's user avatar
  • 111
11 votes
5 answers
4k views

I sometimes end up with services encapsulating the responsibility of doing some sort of business process for which there are several possible outputs. Typically one of those output is success and the ...
Enrico Massone's user avatar
5 votes
4 answers
35k views

In my class I have a processor manager that contains a switch case. I have just changed the real name values to Ant, Parrot and Snake. switch (name) { case "ant": return new ...
Bastien Vandamme's user avatar
7 votes
6 answers
13k views

As a practice I'm working on a CPU simulator (runs at about 1.78MHz) and I'm using a switch statement to execute correct opcodes based on the value in the IR (instruction register) variable. This ...
NMITIMEN's user avatar
  • 119
2 votes
2 answers
550 views

Traditional switch blocks have one scope, so the following throws a compiler error "A local variable or function named 'message' is already defined in this scope": switch(value) { case 1: ...
rory.ap's user avatar
  • 929
1 vote
4 answers
1k views

Backstory (You can skip) I am writing a pronunciation library for irregular words. Take something like the following: T1E1s // tee one E one es | tee one E ones 1994-1995// 1994 (minus|dash|to|) ...
Anon's user avatar
  • 3,649
47 votes
12 answers
21k views

I have a switch structure that has several cases to handle. The switch operates over an enum which poses the issue of duplicate code through combined values: // All possible combinations of One - ...
Taco's user avatar
  • 1,175
-1 votes
2 answers
1k views

I was wondering if there is a way to get the values of every case in a switch statement? When you provide a not implemented case, I would like to throw some exception and provide a list of available ...
Lubomir Stoimchev's user avatar
9 votes
2 answers
286 views

My program needs to execute a sequence of steps from start to end. But based on different input the start point will vary, e.g, some will run from the first step to end, some will run from the 2nd ...
Qiulang 邱朗's user avatar
4 votes
2 answers
429 views

For example, it is common to see games with game loop and states: stateChanged(){ switch(state){ STATE.PLAYER_SELECT_CHARACTER: this.currentController=new PlayerSelectCharacterController()...
ocomfd's user avatar
  • 5,760
1 vote
1 answer
559 views

I have a situation that occurs 5 times in my code, and continues to grow. Basically I have the same switch statement structure every time I need to perform an operation that involves my matrix and ...
Krupip's user avatar
  • 1,347
1 vote
4 answers
3k views

When writing a switch statement that only ever has to deal with a known set of values (imagine an implicit enumeration), I find myself wondering what should be the last entry in the construct. I'm ...
Dev-iL's user avatar
  • 233
-1 votes
1 answer
724 views

Once in a while I’m stumbling on switch statements during a Code Review session. I would like to find a more elegant way of this code .... $istannceOfClass = $repository->loadFoo(); switch($...
BruceStackOverFlow's user avatar
0 votes
1 answer
1k views

Presume this situation: Max Number of 256 key slots. Key slots are defined by a struct, and a variable for each (256) has to exist. User defines which keys slots are active at initiation (in my ...
Anon's user avatar
  • 3,649
1 vote
1 answer
1k views

Some code I wrote was sent back to me during peer review, telling me to add logging to the start of each case in a switch statement in a servlet (Java, if that matters), so if something goes wrong, we ...
AlbeyAmakiir's user avatar
5 votes
2 answers
2k views

High level: I think my design pattern is flawed. Despite implementing Polymorphism, I find myself relying on large switch statements based on derived Type within my WPF application. As I'm adding more ...
Andrew Deacy's user avatar
11 votes
7 answers
2k views

I've been given some Java code to look at, which simulates a car race, of which includes an implementation of a basic state machine. This is not a classical computer science state machine, but merely ...
PythonNewb's user avatar
4 votes
3 answers
15k views

Let's say you have many paths that an application can take at a certain point based on the value of a specific input (for example a simple int). Is there a certain method that is most efficient for ...
thanby's user avatar
  • 149
6 votes
1 answer
1k views

Like: switch (value) { default: something_common() case 1: niche_case() ... } etcetera. if-else statements require an "if" first and an "else" last, but switch statements are more flexible ...
EMBLEM's user avatar
  • 425
1 vote
1 answer
1k views

I'm creating some kind of a "personal assistant" application which is basically a web service that receives a message and then does something according to it. For example I send "what time is it?" ...
das Keks's user avatar
  • 213
7 votes
2 answers
5k views

At work I bumped into a problem to see how enums are growing and storing business logics. As they grew, the constructors grew a lot. At one point I found out instead of putting in let's say the tenth ...
CsBalazsHungary's user avatar
-4 votes
4 answers
557 views

I could imagine the below code being somewhat useful. Is there a reason this pattern hasn't made it into programming languages? To be clear the string was just what I chose as an example you could ...
Jack Fraser's user avatar
10 votes
6 answers
10k views

So I'm making a method to create a salutation line based on two people from a database. There are four parameters: the two names (name1 and name2) and the two genders (gender and gender2). For every ...
moffeltje's user avatar
  • 261
4 votes
3 answers
14k views

I feel like I should be able to find an answer to this, but it turns out to be harder to search than expected... so: In C#, when we do something like: enum MyEnumClass { A, B }; static String Test(...
Hirle's user avatar
  • 300
1 vote
2 answers
2k views

There are several cases when I want to switch over a String input. I decided for implementing something like: public Object doStuff(String param) { switch (param.hashCode()) { case 1234546: ...
Mindwin Remember Monica's user avatar
7 votes
3 answers
43k views

When using the Switch statement, is using return instead of break or a combination of the two considered bad form? while (true) { var operation = Randomness.Next(0, 3); switch (operation) ...
tyh's user avatar
  • 253
4 votes
4 answers
12k views

I see most developers using switch statements with breaks in each and every case. Is this usage of switch statements inappropriate since a simple set of if-else statements would suffice? Is it OK for ...
user avatar
2 votes
2 answers
3k views

What is the searching algorithm used in switch statement in C language? If the cases are not in order still it searches proper case which means it is not a binary search algorithm, can anybody ...
Guruprasad K's user avatar
6 votes
2 answers
3k views

I'm seeing some code like this in our code base, and want to refactor it: (Typescript psuedocode follows): class EntityManager{ private findEntityForServerObject(entityType:string, serverObject:any)...
Taytay's user avatar
  • 211
0 votes
1 answer
660 views

I don't understand why the switch or equivalent is so popular in languages. To me, it seems like it had a place back in the days when the alternative was lots of nested if statements in the else part ...
neelsg's user avatar
  • 483
1 vote
3 answers
1k views

I'm refactoring code and have reached a horribly gigantic switch statement. Every single API method available to end users is represented as an enum and we have a switch statement iterating over the ...
dsollen's user avatar
  • 1,163
8 votes
3 answers
33k views

I am developing a game in JavaScript where you start with a user input, stored in the variable "controller". The options for the user consists of start to start the game or about to learn about the ...
Ben Watkins's user avatar
19 votes
8 answers
9k views

When I was starting to programme in Java, the fact that switch statements didn't take strings frustrated me. Then on using Enums, I realised the benefits that you get with them rather than passing ...
anotherdave's user avatar
12 votes
4 answers
2k views

I can't figure out a better solution to my problem. I have a view controller that presents a list of elements. Those elements are models that can be an instance of B, C, D, etc and inherit from A. So ...
Raphael Oliveira's user avatar
26 votes
5 answers
27k views

I have a function that takes in a set of parameters, then applies to them as conditions to an SQL query. However, while I favored a single argument array containing the conditions themselves: ...
xiankai's user avatar
  • 383
101 votes
3 answers
113k views

I am a bit puzzled on whenever or not to include break after the last case, often default. switch (type) { case 'product': // Do behavior break; default: // Do ...
Robin Castlin's user avatar
25 votes
6 answers
20k views

I'm working on a project that processes requests, and there are two components to the request: the command and the parameters. The handler for each command is very simple (< 10 lines, often < 5)....
beatgammit's user avatar
  • 1,907
13 votes
6 answers
95k views

Is there some syntax (other than a series of if statements) that allows for the use of a switch statement in Java to check if an object is an instanceof a class? I.e., something like this: switch (...
Cameron Fredman's user avatar
4 votes
2 answers
6k views

I'm writing a simple chess-related code with intention to write it clearly (performance doesn't matter at all). And this method I have doesn't look clean to me at all: public static Piece ...
vorou's user avatar
  • 153
43 votes
8 answers
67k views

During a recent code review I was asked to put default cases in all the files wherever switch block is used, even if there is nothing to do in default. That means I have to put the default case and ...
Ankit's user avatar
  • 939
40 votes
8 answers
27k views

Consider the following enum and switch statement: typedef enum { MaskValueUno, MaskValueDos } testingMask; void myFunction(testingMask theMask) { switch (theMask) { case ...
Swizzlr's user avatar
  • 503
8 votes
5 answers
4k views

I was wondering the if if-else statements, is like a switch statement that does have a break statement. if( boolean_expression_1 ) statement_1 else if( boolean_expression_2 ) statement_2 else ...
Chris Okyen's user avatar
20 votes
2 answers
4k views

I was reading Why do we have to use break in switch?, and it led me to wonder why implicit fall-through is allowed in some languages (such as PHP and JavaScript), while there is no support (AFAIK) for ...
zzzzBov's user avatar
  • 5,844