+ 34
Whats a real world example where a switch statement would be used?
Im having a tough time comprehending how the switch statement works. The example in the app is too bland.
67 Answers
+ 37
In computer programming languages, a switch statement is a type of selection control mechanism used to allow the value of a variable or expression to change the control flow of program execution via a multiway branch.
Switch statements function somewhat similar to the if statement used in programming languages like C/C++, C#, Visual Basic .NET, Java and exists in most high-level imperative programming languages such as Pascal, Ada, C/C++, C#, Visual Basic .NET, Java, and in many other types of language, using such keywords as switch, case, select or inspect.
Switch statements come in two main variants: a structured switch, as in Pascal, which takes exactly one branch, and an unstructured switch, as in C, which functions as a type of goto. 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.
Switch statement in C
switch (age) {
case 1: printf("You're one."); break;
case 2: printf("You're two."); break;
case 3: printf("You're three.");
case 4: printf("You're three or four."); break;
default: printf("You're not 1,2,3 or 4!");
}
+ 18
You can use switch to check the "...day"
Let's take dayIndex as numerical representation of a day.
switch(dayIndex) {
case 0: cout << "Sunday";
break;
case 1: cout << "Monday";
break;
case 2: cout << "Tuesday";
break;
//... and so on till Saturday(6)
default: cout << "Non-Earth day";
}
Hope it gives a REAL feeling ^_^'
+ 15
@Garrett
i don't have first-hand knowledge on this topic .. i did search on Switch examples found in OpenSource applications.. it seems like implementation of Switches are commonly found in Networking applications.
i was looking at a short example of how they were testing circuits on a Systems Network using Switching Application.. using Python Language they mapped out ethernet ports with devices.. way over my head so i'm not summarizing effectively, but the general idea of sending packets of data over a network to endUser devices using Switching Application
You can probably find better examples.. seems like abundantly used in Network Communications and other Scientific environments
+ 12
Generally you will see a switch statement used when there are choices for a user to select or many possible outcomes can be returned from other code that feeds into it. Like in code that gets user input or like possible server or client replies in networking like niteOwlTwO said. Also when if else statements get too long and complicated they are often recoded as a switch if possible.
+ 9
usually switch is used whit a parameter, int or enum and different action are performed based on this value.
If instead is used to do or not to do a single action based on a bool value, yes or not, true or false.
it act more like a filter..
So nested if should means cascaded check conditions.
A switch is like a multi selector..
for example if you have a game whit many modes you'll ask the user to select a mode to play..
he digits 4 => switch(modeChosen){
...
case 4:
play captureTheFlagMod
+ 8
switch(dayIndex) {
case 1: cout << "Monday";
break;
case 2: cout << "Tuesday";
break;
case 3: cout << "Wednesday";
break;
//... and so on till Sunday(7)
default: cout << "Non-Earth day";
}
Hope it gives a REAL feeling ^_^'
+ 7
Great replacement for a large if/else if/else if/else if/else block that looks much cleaner and easier to read.
+ 7
See it works like if/else, when the compiler start analysing the switch part of the code it one by one matches the value entered by the user to the nos. Written in the switch case and when it match, compiler do the work that is written in that particular matched part that's it.
+ 7
voting machine is the best real world example of switch you select only one and nota show for default case
+ 6
Activating clients from a server .
+ 6
Consider this code...
https://code.sololearn.com/c13aM7NpMYbP/?ref=app
+ 5
cud be used to create an automatic grading system
+ 5
Hi, good night.
Everything in the real life can be compared with a switch statement. By example when you turn on the light you are using a switch.
When you take one adress, you must select one of two possibilities left or right.
I hope you have understood me.
+ 5
C++ Program :
#include <iostream>
using namespace std;
int main()
{
int input;
cout<<"1. Play game\n";
cout<<"2. Load game\n";
cout<<"3. Play multi-player\n";
cout<<"4. Exit\n";
cout<<"Selection: ";
cin>>input;
cout<<"\n";
switch ( input )
{
//Note the colon, not a semicolon
case 1:
cout<<"Play game called";
break;
case 2:
cout<<"Load game called";
break;
case 3:
cout<<"Play Multi-player game called";
break;
case 4:
cout<<"Thanks for playing!\n";
break;
default:
cout<<"Bad input, quitting!\n";
}
return 0;
}
+ 4
In real world we can switch statement in calculators
used in webpages for designing , apps im mobile phones
+ 3
Its a simple code in java using switch.
if you dont give the break statement after every case it results in fall through condition,i.e, all the other cases after the case value is matched
will be executed until a break statement is encountered.
https://code.sololearn.com/cs88KP1Y2Qh9/?ref=app
+ 3
let think about the functioning of calculator the switch statement works in calculator functioning. it works like check and draw if case matches then the statement will execute otherwise it draw and will jump on other case .
+ 3
So you're going to the office. The first thing you do is make coffee. How many people will be in today? Just you? It might not be best to make a whole pot, so you start considering a Keurig. 2 people? A half pot may last all day. 4 people? Maybe a full pot. 6? might want to grind extra so there will be some ready for making a second pot.
You can do this with IF statements of course, but a SWITCH/CASE statement will be much cleaner. It's basically IF shorthand.
The variable that you're IFfy about is assigned to the SWITCH. Maybe it would be better represented as a dial. Turn the switch-dial to the number of people coming in, and the output is how much coffee to make.
+ 3
for calculator
+
-
*
/
+ 3
If you using Ruby... try to avoid switch statement that switch on class, type or category.
Its best to abstract that kind of logic away with a Duck Type to keep dependencies to a minimum.