Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does this syntax of switch case mean?

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?

like image 509
user2131316 Avatar asked Jul 17 '13 12:07

user2131316


People also ask

What is the syntax of switch case?

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.

What is the use of switch syntax?

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.

What is the meaning of switch case?

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.

What does switch case mean in C?

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.


1 Answers

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;
like image 112
ouah Avatar answered Sep 28 '22 08:09

ouah