Switch/Case Question
-
Good day all! I believe I have a rather simple question but I can not seem to work it out using a Switch statement. I have a small function that checks the time and if the time is one of the parameters set (a total of 6 time points), then I wish to peform a duty. Instead of using an if statement for all of the time points I wanted to add 3 time points to one case and 3 to another and do nothing in the default. I cannot compile my function while I have the case statements setup in this manner: case "10:00", "11:20", "13:30": Do something break; case "17:00", "19:20", "23:30": Do something else break; default break; Can anyone pinpoint my syntax error with the cases I have defined above. When I used VB, this syntax worked fine with the multiple points but it doesn't now. Any pointers would be greatly appreciated.
-
Good day all! I believe I have a rather simple question but I can not seem to work it out using a Switch statement. I have a small function that checks the time and if the time is one of the parameters set (a total of 6 time points), then I wish to peform a duty. Instead of using an if statement for all of the time points I wanted to add 3 time points to one case and 3 to another and do nothing in the default. I cannot compile my function while I have the case statements setup in this manner: case "10:00", "11:20", "13:30": Do something break; case "17:00", "19:20", "23:30": Do something else break; default break; Can anyone pinpoint my syntax error with the cases I have defined above. When I used VB, this syntax worked fine with the multiple points but it doesn't now. Any pointers would be greatly appreciated.
-
switch(time)
{
case "10:00":
case "11:20":
case "13:30":
// Do Something
break;//etc
}No multiple params in a switch case, fallthrough only allowed when no code is defined in that particular case.
-
Just to be clear on my end: From your snippet, at 10:00 // Do Something will be performed? Sorry, just want to make sure my Alzheimer's is not starting too early.
-
Just to be clear on my end: From your snippet, at 10:00 // Do Something will be performed? Sorry, just want to make sure my Alzheimer's is not starting too early.
-
Just to be clear on my end: From your snippet, at 10:00 // Do Something will be performed? Sorry, just want to make sure my Alzheimer's is not starting too early.
Yes - because switch statements like this fall through to the next case. Note that they only work if there's nothing betweent the case statements. As soon as you introduce an operation, you have to use break;.
Deja View - the feeling that you've seen this post before.