Case ranges for "switch" statements

General discussion on mikroC PRO for AVR.
Post Reply
Author
Message
rrsquez
Posts: 117
Joined: 23 Feb 2011 21:35

Case ranges for "switch" statements

#1 Post by rrsquez » 24 Sep 2015 19:32

Hello, I'm have a switch statement that has a lot of cases for 1 statement. Here is the code below. Can someone tell me if there is a better way to do this? In other compilers a sequential range of cases can be done using ellipses (...); For my example, I have cases 20 to 39 all going to the same switch statement. In the case of these "extended compilers" (because it's not ANSI), we can do this: case 20 ... 39: // my code here

Instead, I have to use this mess:
// Embedded switch
switch (Familygroup)
{
case 10 : Alarm1value = 10;
Alarm1range = 3;
Alarm2value = 25;
Alarm2range = 3;
break;
case 20 : case 21 : case 22 : case 23 : case 24 : case 25 :
case 26 : case 27 : case 28 : case 29 : case 30 : case 31 :
case 32 : case 33 : case 34 : case 35 : case 36 : case 37 :
case 38 : case 39 :
Alarm1value = 10;
Alarm1range = 2;
Alarm2value = 50;
Alarm2range = 2;
break;
default : Alarm1value = 10;
Alarm1range = 3;
Alarm2value = 25;
Alarm2range = 3;
break;
}


If anyone has a suggestion, or can point out the proper syntax, I would be very grateful.

Thank you, Richard V

aCkO
Posts: 1119
Joined: 14 Feb 2011 04:07
Location: Bar, Montenegro

Re: Case ranges for "switch" statements

#2 Post by aCkO » 24 Sep 2015 19:47

You should use:

Code: Select all

if (...) {
...
}
else if (...) {
...
}
else if (...) {
...
}
Regards

Post Reply

Return to “mikroC PRO for AVR General”