switch

Navigation:  Programming language SQC > Language elements > Control elements >

switch

Previous pageReturn to chapter overviewNext page

The switch-case construct takes a integer variable, placed after switch, and compares it to the value following the case keyword.

If the variable argument to switch does not evaluate to a case constant, the code defined after the default constant is executed.

The whole switch-case construct is evaluated  once.

 

Example:

 

void main()

{

    int grade = 1;

 

    switch (grade) 

    {

     case 1:

       printf("grade 1\n");

       break;

     case 2:

       printf("grade 2\n");

       break;

     case 3:

       printf("grade 3\n");

       break;

     case 4:

       printf("grade 4\n");

       break;

     default:

       printf("grade 5\n");

       break;

    }

}

 

 

Output:

 

  grade 1