How you refer to data in a range with switch statement in C#? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

How you refer to data in a range with switch statement in C#?

I'm writing a program on C# to grade students results but I can't use switch statement to refer to a range let's say like 80-100 which should get a grade A. Just give me the format of the code specifically on switch statement.

22nd Jan 2017, 12:03 PM
Tuchy
Tuchy - avatar
2 Answers
+ 4
Yes, use if instead. I don't know c#, but in all the other ones I know it would be much easier just to do: if(score>=80){grade = A};
22nd Jan 2017, 1:41 PM
J.G.
J.G. - avatar
0
It is not possible to specify a range in switch case statements because the main purpose of switch statements is to comparison between constants. Use if else ladder instead. And if u want to use switch only then write complete range in cases. Example: Case 100: Case 99: Case 98: Case 80: Break; This is a lengthy process, and useless. So use if else only.
22nd Jan 2017, 12:30 PM
Divesh Agarwal
Divesh Agarwal - avatar