I saw some C code like this:
int check = 10:
switch(check) {
case 1...9: printf("It is 2 to 9");break;
case 10: printf("It is 10");break;
}
What does this case 1...9:
mean? Is it standard?
Switch Case Syntaxswitch( expression ) { case value-1: Block-1; Break; case value-2: Block-2; Break; case value-n: Block-n; Break; default: Block-1; Break; } Statement-x; The expression can be integer expression or a character expression.
The switch statement evaluates an expression, matching the expression's value against a series of case clauses, and executes statements after the first case clause with a matching value, until a break statement is encountered.
The switch case in java executes one statement from multiple ones. Thus, it is like an if-else-if ladder statement. It works with a lot of data types. The switch statement is used to test the equality of a variable against several values specified in the test cases.
The switch statement in C is an alternate to if-else-if ladder statement which allows us to execute multiple operations for the different possibles values of a single variable called switch variable. Here, We can define various statements in the multiple cases for the different values of a single variable.
It's a GNU C extension called case range.
http://gcc.gnu.org/onlinedocs/gcc/Case-Ranges.html
As noted in the document, you have to put spaces between the low and high value of the range.
case 1 ... 9:
statement;
is equivalent to:
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
case 7:
case 8:
case 9:
statement;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With