How do you make the case statment act of a range of numbers like.
if ( var >= 100 && var < 200 ) instead of guess one value .
switch (var) {
case 1:
//do something when var == 1
break;
// break is optional
case 2:
//do something when var == 2
break;
default:
// if nothing else matches, do the default
// default is optional
}
How do you make the case statment act of a range of numbers
You don't - it works on discrete values only. You'll need an if-else block to do ranges.
the switch statement enters the code block at the particular case that matches, and executes the remaining code in all cases unless it hits a break. As a result of this behavior, if there are only a few values in the range, you can list them all like this:
switch(x)
{
case 1:
case 3:
case 5:
// code for 1,3,5 case
break;
case 2:
case 4:
case 6:
// code for 2,4,6 case
break;
default;
}