Whats a real world example where a switch statement would be used? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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.

22nd Feb 2018, 5:02 AM
Garrett
Garrett - avatar
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!"); }
22nd Feb 2018, 5:14 AM
Sudarshan Rai
Sudarshan Rai - avatar
+ 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 ^_^'
24th Feb 2018, 2:48 PM
777
777 - avatar
+ 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
2nd Mar 2018, 1:25 AM
niteOwLTwO
niteOwLTwO - avatar
+ 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.
2nd Mar 2018, 1:35 AM
Louis
+ 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
2nd Mar 2018, 3:05 AM
AZTECCO
AZTECCO - avatar
+ 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 ^_^'
25th Feb 2018, 4:54 AM
Adarsh Singh
Adarsh Singh - avatar
+ 7
Great replacement for a large if/else if/else if/else if/else block that looks much cleaner and easier to read.
23rd Feb 2018, 10:24 AM
Tom Shaver
Tom Shaver - avatar
+ 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.
24th Feb 2018, 9:30 AM
Ananya Srivastava
Ananya Srivastava - avatar
+ 7
voting machine is the best real world example of switch you select only one and nota show for default case
25th Feb 2018, 12:02 PM
Yash Rajput
Yash Rajput - avatar
+ 6
Activating clients from a server .
22nd Feb 2018, 9:31 AM
Waseem Siddiqi
Waseem Siddiqi - avatar
28th Feb 2018, 6:31 PM
Pravin Pandey
Pravin Pandey - avatar
+ 5
cud be used to create an automatic grading system
25th Feb 2018, 3:05 PM
#Allan_Scar
#Allan_Scar - avatar
+ 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.
25th Feb 2018, 10:02 PM
David Rueda 🇪🇸
David Rueda  🇪🇸 - avatar
+ 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; }
2nd Mar 2018, 9:16 AM
Ankit
Ankit - avatar
+ 4
In real world we can switch statement in calculators used in webpages for designing , apps im mobile phones
3rd Mar 2018, 3:49 PM
அபிஷேக் அபி
+ 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
25th Feb 2018, 10:16 AM
Nafisa Sardar
Nafisa Sardar - avatar
+ 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 .
25th Feb 2018, 11:03 AM
Rupali Patel
Rupali Patel - avatar
+ 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.
25th Feb 2018, 8:45 PM
Alan Tenore
Alan Tenore - avatar
+ 3
for calculator + - * /
26th Feb 2018, 1:57 PM
Nadeem Raza
Nadeem Raza - avatar
+ 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.
28th Feb 2018, 12:37 AM
Khalil Gharbaoui
Khalil Gharbaoui - avatar