0
How to make switch statements for x<5, x>5, x==5
I need to make a switch statement for if something is equal, less than, it greater than in C++... HOW? Someone please help me.
2 ответов
+ 2
Don't think its possible. I once thought of it but didn't really work.
It seems you need to follow syntax which doesn't allow other than constant value to compare to.
Check this out-
https://www.tutorialspoint.com/cprogramming/switch_statement_in_c.htm
+ 1
Marvin Lee switch is worth if cases are less.. for x >5, there are n number of cases and in this scenario, switch case is not fruitful... You can go with if , elseif and else as below:
if (x==5)
{//your code
}
else if (x< 5)
{//your code
}
else
{//your code
}
If you still wish to use switch case, here it is :
switch (x)
{
case 5:
{
//your code
break;
}
case 1:
case 2:
case 3:
case 4:
{
//your code
break;
}
//write this way for all cases
// at the end
default:
{
//your code
}
}