Most voted "switch" questions
In programming, a switch instruction is a type of selection control mechanism used to allow the value of a variable or expression to change the program execution control flow
Learn more…134 questions
Sort by count of
-
29
votes2
answers463
viewsWhat’s a loose comparison?
In the documentation of PHP, on the switch says: Note: Note that the switch/case does loose comparison. What is a loose comparison? And what is the difference between a loose comparison and a rigid…
-
24
votes2
answers24948
viewsWhat is the difference between Switch, Case and If, Else?
I’d like to know the difference between switch .. case and if .. else. Which offers a better "performance"?
-
15
votes1
answer311
viewsHow does the switch work behind the scenes?
Seeing those comments on the use of switch is the doubt how it really works and why it is different from the if when only buy for the equality of a single variable against a sequence of values. How…
-
14
votes1
answer276
viewsWhy is using String in a switch block more efficient than in an if-Else block?
According to the java documentation: The Java Compiler generates generally more Efficient bytecode from switch statements that use String Objects than from chained if-then-Else statements. Not to…
-
12
votes1
answer1550
viewsWhy can’t you declare a variable within a case?
Why don’t you compile? #include <stdio.h> int main(void) { int valor = 0; scanf("%d", &valor); switch (valor) { case 0: int variavel = 1; printf("%d", variavel); break; default: int…
-
12
votes1
answer25845
viewsIn Python we have the switch function?
In Python we have the function switch? wanted to create a program but didn’t want to use many if, and that’s why the function switch help, but I’ve seen several videos Python lessons and no talk of…
-
12
votes3
answers1357
viewsIs it wrong to use multiple cases for the same action on the switch?
It’s wrong to do that with the switch? I programmed like this with him, it’s wrong? switch(estado){ case "AL": case "BA": case "CE": case "MA": case "PB": case "PE": case "PI": case "RN": case "SE":…
-
12
votes3
answers1299
viewsDefine whether the letter is vowel or consonant
I was trying to create a code with Javascript that after reading the letter entered showed if it was a vowel or consonant but with any letter I put the code answers me that and a consonant even if…
-
10
votes3
answers1001
viewsUse OR operator in a PHP CASE
How to use the operator or in a control structure switch? Example switch ($options) { case 1 || case 2: echo "Valor de opção 1 e 2"; break; case 3: echo "Valor de opção 3"; break; }…
-
10
votes3
answers6900
viewsWhat’s the difference between Return and break in a switch case?
In some situations it is necessary to practice the switch case for optimization and improvement of code. I have an application developed for Android using Java, which in case is used of both…
-
9
votes1
answer2214
viewsJavascript performance: switch or if nested?
Which of the two alternatives provides the best performance in Javascript: nested switch or if? if { ... } else { if { ... } else { if { ... } else { ... } } } or switch(expression) { case n: code…
-
8
votes3
answers8606
viewsSwitch case Kotlin
In Java I use the switch in various situations, as in the example below: public class SwitchDemo { public static void main(String[] args) { int month = 8; String monthString; switch (month) { case…
-
7
votes2
answers305
viewsI know the variable value, but the switch only goes to the default
After a search, I get the following result: // O resultado desta linha é I ou II ou III; echo $subsecao; // O resultado desta linha é string; echo gettype($subsecao); // Faço um switch para atribuir…
-
6
votes2
answers256
viewsOptimization of custom search tool
Guys, I’m creating a search page that gets, via POST, the information on a form containing several checkbox. See below that there are several sequences of results and is marked in orange one of them…
-
6
votes2
answers1839
viewsSwitch Alternative Inside Switch
I’m making a decision structure that depends on more than one variable, but readability and maintenance using if or switch are terrible. What alternatives do I have for this case? Example of switch…
-
6
votes2
answers562
viewsSwitch case Java ends without reason
I have an example of switch-case with 8 options. But from the case 7 he finishes the program. He was to return to the menu option after execution. The case has limit of options? Follows the code:…
-
6
votes3
answers820
viewsHow to exit the FOR loop within the Switch structure?
I’m in a noose FOR and I want to lock him inside the Switch, but when I give break within the Switch he comes out of Switch and continues the loop FOR, someone can tell me how to get out of the loop…
-
6
votes3
answers142
viewsMultiple cases on switch with C# 8
On seeing this question about multiple conditions in the switch, reminded me of a problem I had and I couldn’t solve. In C# 8, a new syntax for switch: int num = ObterUmNumero(); var descricao = num…
-
5
votes1
answer281
viewsIs it possible to use the "break" argument on a "switch" to interrupt a loop?
When I want to interrupt a loop of one for, while or foreach always use the break; within a if with the following structure: $teste = array(1,2,3,4,5); foreach($teste as $t){ if($t == 3){ echo…
-
5
votes2
answers1728
viewsHow to receive a string and switch to check in C?
I have to develop an algorithm that gets the name of a place, for example, "School", and based on this, do a check of the string in the switch, and if it is "School", then it sends a message to the…
-
5
votes1
answer2819
viewsUsing Switch Case for Ranges
My teacher passed an exercise in which we should use the cases of a switch to treat intervals, he strictly said that we should use the switch and we cannot use if and neither while/do. I’ve tried…
-
5
votes2
answers195
viewsWhat is the logic behind the switch case in Javascript?
I started my studies in Javascript and had contact with the switch, following example: let permissao; switch (permissao){ default: console.log('sem acesso'); break; case 'estagiário':…
-
4
votes2
answers294
viewsWhat is the correct way to return a "switch" value?
How is the correct way to return the value of switch? HTML: <h3 id="txtOcorrenciaStatus">Status</h3> Javascript: switch(ocoStatus) { case C: $("#OcorrenciaStatus").html("Concluído");…
-
4
votes1
answer592
viewsLarge switch case in C
I am developing a system for the college and I would like to know what is the best option, in the sense of optimization of the code, of improving even. I have a switch Menu case, where I have 88…
-
4
votes2
answers253
viewsIs it OK to use a switch inside a for?
Came to me a question related to the use of a switch within a for. I saw that it works for my purpose but this is correct to do in the world of programming? There is a better way to get the result…
-
3
votes1
answer237
viewsDoes the last instruction of a switch need 'break'?
I was watching some tutorials on Youtube and noticed that some programmers leave the last instruction of a block switch without the word break. For example: switch(frutas){ case "abacaxi":…
-
3
votes3
answers492
viewsUsing an operator on a switch case
If I do, good wheel: #include <stdio.h> int op1; int main(){ scanf("%d", &op1); switch(op1) { case 1: puts("Hello World"); } } I wanted to make that by introducing a special character, for…
-
3
votes2
answers467
viewsIterate a list inside a switch
good guys I would like to know how I do to iterate a list on a switch in python, type I have a list with ten values lista = [1,2,3,4,5,6,7,8,9,11] now I want to access this list value by value and…
-
3
votes2
answers163
viewsConvert Switch to IF
Hello everyone is the following I have this code and I needed to change it to if instead of switch, how can I do this? switch (theOperator) { case "+": total2 = total1 +…
-
3
votes2
answers2767
viewsDetermining a string within a C switch
I have doubts about how to define a string within a switch in C, without having to make a table of constants for this. The variable nome_lanche is the type char, but this with the number of…
-
3
votes1
answer760
viewsAccess object array within a switch
Good evening, I’m having trouble accessing and modifying an array of main class objects inside a switch in the view class. The array is declared like this in main: public class Controle { public…
-
3
votes1
answer12863
viewsReturn to code start after a C if or switch case
I have a question regarding if. I’m developing a program for the end of college semester and every time I need to use one if and at the end of if, I need you to go back to the beginning of the…
-
3
votes1
answer1903
viewsBreak and Continue on Switch
What’s the difference of using the break and the continue within the switch?
-
3
votes2
answers157
viewsCode error using switch and new date
I tried to do a function that would return the day of the week that we are, through a switch in JS, but somehow the result does not appear. I will debug all the code, but did not find the error.…
-
3
votes2
answers293
viewsHow to make a switch in C#?
I see a lot of people saying it’s wrong to do switch in the code, because it ends up weighing and is ugly. Is it correct the way I am using and what would be an alternative to using it? Fill the…
-
3
votes1
answer83
viewsCode works with a messagebox but no no?
Good afternoon, I’m doing a little school work to learn how to control radio Buttons with a switch in winforms public Form1() { InitializeComponent(); Form1.CheckForIllegalCrossThreadCalls = false;…
-
3
votes1
answer69
viewsComparison of index (get) in Array List not working properly
I’ve been trying to compare a string (right answer) for an alternative question as an example in a project I’m developing, but I can’t succeed in all the different ways I try. The goal is that every…
-
3
votes2
answers201
viewsReact Switch Route Default
I’m creating an app in Nodejs, React & Redux here at the company. And I have a page that is Content, in it I have two navigation bars and a common area for content. In this common area I’m using…
-
2
votes1
answer261
viewsRegex in a switch case
About I have a switch that takes care of the activation of my menus from their address on the site. switch($location.path()){ case '/perfil': $scope.menuAtivo.perfil = 'active'; break; case…
-
2
votes3
answers972
viewsOperation of the switch case
It is possible to make comparisons with variable values in the instruction switch case or the case just checks the value of a variable and makes an action? It is better to make this kind of…
-
2
votes1
answer215
viewsHow many "cases" are possible in a "switch"?
Case 1 Informação.Text = "Olá Mundo!" Case 2 Informação.Text = "Tudo bem com você?" Case 3 Informação.Text = "Que horas são?" Notice that I added 3 (three) cases. Now a question; is there a limit to…
-
2
votes1
answer1148
viewsHow to call different methods in different Switch Case conditions?
I have this code, and I’d like to know how to call a method from the switch...case. public static void opcoes(){ System.out.println("Selecione o Algoritmo de Substituicao Desejado");…
-
2
votes1
answer273
viewsUsing the switch function in C++
The function switch and I don’t know what I’m doing wrong marcos there appear "landmarks and cool" and put murilo "Nice and cool". I’m doing it like this: #include <stdio.h> #include…
-
2
votes1
answer279
views -
2
votes1
answer697
viewsFor inside a Java Switch
Good night, I wonder if there is a way to declare a 'FOR' within a SWITCH CASE in Java because Netbeans is making an error right here. I’m trying to make an application to perform a guessing game…
-
2
votes1
answer368
viewsOpen an Edittext when a Switch component is turned off
I have a Adapter custom rendering list, in this list has a switch that by default comes on and when the user shuts down, the system validates a rule and if that rule was true, he has to show a…
-
2
votes3
answers368
viewsHow do I switch to switch to css?
I have two CSS files for the same HTML and would like to use a switch button to switch styles. The idea is to assemble a keying "standard layout" "new layout". I tried it, but I didn’t succeed.…
-
2
votes1
answer159
views -
2
votes1
answer193
viewsHow to present only even and odd values with three variables in C?
My variables are : int codigo; --> Sendo esta variável para o switch int numero1,numero2,numero3; User will enter with three variables : printf("Digite o primeiro numero inteiro: "); scanf("%d",…
-
2
votes3
answers92
viewsVB.NET to C#
I found this line of code: Try Dim WebReq As HttpWebRequest = HttpWebRequest.Create(GET_Data) Using WebRes As HttpWebResponse = WebReq.GetResponse Using Reader As New…