Pre Exam Test Practice (SSC | Banking | IAS | GATE | State Level | CLAT | Railways | Insurance | Teaching Exams | NDA | Defence | IES |State Exam )

Organizational Research By

Surprising Reserch Topic

is there any better implementation for this switch case construct

is there any better implementation for this switch case construct  using -'c,embedded'

I'm working on an embedded system where a register hast to be accessed and after that to be incremented to achieve the result I'm looking for, since the machine is notifying and configured to react on my access and changing or not changing the flag. So the switch's argument has to be kept as it is, since it would otherwise change the behaving of the embedded system.

But there may occur a situation where I don't want to get any of the cases get invoked. But I still need to acces and increment the argument of the switch.

(More indepth I'm converting a sequence of analog values to digital values conversions step by step. The index is used to stay synchronized with the current conversion and relating it with the corresponding case to handle the figure correct. There may occur a state in which the index desynchronisizes to the current conversion so the sequence of conversions must be run through without any of the cases getting invoked (to prevent setting wrong data) untill the sequence is finished and the resynchroinisation can get performed)

The way I'm currently doing this is this:

switch (RunIndex++)/*RunIndex may only be accessed one time per execution
of this construct and has to be incremented in the same step. thats given.*/
   if (RunIndexSyncedWithADCensured == false)
   case 0:
     Case0RelatedData = SomeOperationsForCase0(RawData);
   case 1:
     Case1RelatedData = SomeOperationsForCase1(RawData);
   case 2:
     Case2RelatedData = SomeOperationsForCase2(RawData);
     RunIndex = 0;

This construct does the job but it looks like it is a bit controversial and I don't feel well by considering about committing this into productinal code.

So is there a better looking way to achieve the same, without the need of additional variables or assignements?


Also it may be relevant, that this is in the first part of a interupt function consisting of 2 parts.
The first part handles what has to happen if() a conversion is finished. The second part, what has additional to be done if() this conversion also ended the sequence. So it is no option to simply return from the function without getting into the second part. and there is currently no loop structure where an if(...)break; may break out. (What is also the reason why I'm putting the if inside the switch scope, as it is at least by standard a valid way to break out.)

asked Sep 9, 2015 by Ale8109
0 votes

Related Hot Questions

Government Jobs Opening