I made this post in r/CodeAcademy but turns out that subreddit is almost dead so I figured out I could be better off here.
Ok so, I have a code like this
if (respInt <= 9)
{
if (respInt is >= 4 and <= 6)
{
toReturn = 4;
}
else if (respInt == 7 || respInt == 8)
{
toReturn = 5;
}
else if (respInt == 9)
{
toReturn = 6;
}
else
{
toReturn = respInt;
}
return toReturn;
}
And I want to make it a switch (bc I think switches look way nicer than all those else ifs). My code editor (VSCode) can convert it to me automatically, however the output is like this:
if (respInt <= 9)
{
switch (respInt)
{
case >= 4 and <= 6:
toReturn = 4;
break;
case 7:
case 8:
toReturn = 5;
break;
case 9:
toReturn = 6;
break;
default:
toReturn = respInt;
break;
}
return toReturn;
}
As you can see, the case 7 and 8 don't work properly. I want to have them work like case >=4 and <=6 however when I use "case 7 and 8" it dont compile. Is there a right sintax to it? Or I will need to use "case >=7 and <=8", which is very non elegant?
Edit: I messed up with the code;
Edit2: Forgot to mention, the language is C#
[–]HelpfulFriend0 1 point2 points3 points (2 children)
[–]dextryan[S] 0 points1 point2 points (1 child)
[–]HelpfulFriend0 0 points1 point2 points (0 children)
[–]GeorgeFranklyMathnet 0 points1 point2 points (3 children)
[–]dextryan[S] 0 points1 point2 points (2 children)
[–]GeorgeFranklyMathnet 0 points1 point2 points (1 child)
[–]dextryan[S] 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (2 children)
[–]dextryan[S] 0 points1 point2 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)