+ 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.☺
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.
+ 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.
+ 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;
}