+ 2

This might be annoying but i really don't understand the switch statement.

Can someone answer the switch simply?😵Cuz I really don't understand the meaning and how its used.Also pls give some examples.☺

30th Jan 2017, 5:22 PM
Julian Aung
Julian Aung - avatar
3 Antworten
+ 2
switch(var){//evaluate var case 2: //in case var equals 2 .....//do something break; //stop the switch statement case3: .....//do something else break;//same as above default: ....//do default action } if var is two the switch statement will execute the instructions in the "do sometimg" line and exit due to the "break;" folloing the instructions if var is 3 the switch statement will execute the instructions in the "do someting else" line and exit due to the "break;" folloing the instructions if var is 1 the switch statement will execute the instructions in the "do default action" line because case 1 and casr 2 are not met, and exit due to the end of the switch statement. if you dont use "break;" after the case, also the instructions of the next cases will be executed until next available "break;" or the end of the statement. Now, if you want to really understand it, start typing and do your experiments with switch statements.
30th Jan 2017, 9:01 PM
seamiki
seamiki - avatar
+ 6
Sorry for the late reply.Thank you for your explanation.Because of your explanation now I have a clear understanding for switch statements.And again thank you very much.
16th Mar 2017, 5:29 PM
Julian Aung
Julian Aung - avatar
+ 2
int a; a = Int.Parse(Console.ReadLine()); switch (a){ case 1: Console.WriteLine("one"); break; case 2: Console.WriteLine("two"); break; default: Console.WriteLine("???"); break; }
26th May 2017, 6:12 AM
Bakyt Bektashov
Bakyt Bektashov - avatar