Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

To introduce loop inside c++ switch- case

I am trying to use this switch - case statement. I was wondering if there is any other efficient way of writing this code. The "function" prototype is: int function(int a,int b, int c,int d)

switch (u) {
case 1:
  t = t + function(0,2,1,0);   // 1
  break;
case 2:
  t = t + function(0,2,1,0);  // 1
  t = t + function(1,2,2,0);  // 2
  break;
case 3:
  t = t + function(0,2,1,0) ; // 1
  t = t + function(1,2,2,0) ; // 2
  t = t + function(0,3,3,1) ; // 3
  break;
case 4:
  t = t + function(0,2,1,0) ; // 1
  t = t + function(1,2,2,0) ; // 2
  t = t + function(0,3,3,1) ; // 3
  t = t + function(1,3,4,1) ; // 4 
  t = t + function(3,2,4,0) ; // 6
  break;
case 5:
  t = t + function(0,2,1,0) ; // 1
  t = t + function(1,2,2,0) ; // 2 
  t = t + function(0,3,3,1) ; // 3
  t = t + function(1,3,4,1) ; // 4
  t = t + function(2,3,5,1) ; // 5
  t = t + function(3,2,4,0) ; // 6
  t = t + function(4,2,5,0) ; // 7
  break;
case 6:
  t = t + function(0,2,1,0) ; // 1
  t = t + function(1,2,2,0) ; // 2
  t = t + function(0,3,3,1) ; // 3
  t = t + function(1,3,4,1) ; // 4
  t = t + function(2,3,5,1) ; // 5
  t = t + function(3,2,4,0) ; // 6
  t = t + function(4,2,5,0) ; // 7
  t = t + function(3,3,6,1) ; // 8
  break;
case 7:
  t = t + function(0,2,1,0) ; // 1
  t = t + function(1,2,2,0) ; // 2
  t = t + function(0,3,3,1) ; // 3
  t = t + function(1,3,4,1) ; // 4
  t = t + function(2,3,5,1) ; // 5
  t = t + function(3,2,4,0) ; // 6
  t = t + function(4,2,5,0) ; // 7
  t = t + function(3,3,6,1) ; // 8
  t = t + function(4,3,7,1) ; // 9
  t = t + function(6,2,7,0) ; // 11
  break;
case 8:
  t = t + function(0,2,1,0) ; // 1
  t = t + function(1,2,2,0) ; // 2
  t = t + function(0,3,3,1) ; // 3
  t = t + function(1,3,4,1) ; // 4
  t = t + function(2,3,5,1) ; // 5
  t = t + function(3,2,4,0) ; // 6
  t = t + function(4,2,5,0) ; // 7
  t = t + function(3,3,6,1) ; // 8
  t = t + function(4,3,7,1) ; // 9
  t = t + function(5,3,8,1) ; // 10
  t = t + function(6,2,7,0) ; // 11
  t = t + function(7,2,8,0) ; // 12
  break;
}

Is there any way to shorten this code?

Every new case has the same function till previous case plus one or two new function.

Ultimate Goal :The goal is to have less code and less manual entry inside the code.

Please do post an answer to decrease the length of code .

All the answers posted till now do not think about automagically creating the numbers themselves as even these numbers have a pattern with them.

like image 504
user2725924 Avatar asked Oct 03 '13 06:10

user2725924


People also ask

Can we use for loop inside switch case in C?

Can we use for loop inside switch case in C? No, a switch statement is not a loop. And neither are if, if-else, if-else-if, etc.

How do I loop back a switch case?

A switch-case construct isn't an iteration construct. The flow of control cannot go back up due to it. So, you can't use it to loop. Wrap it in a while or for looping construct instead. Just turn condition to false when you want to stop looping.

What is the use of switch loop in C?

In C++, the switch statement is used for executing one condition from multiple conditions. It is similar to an if-else-if ladder. Switch statement consists of conditional based cases and a default case. In a switch statement, the “case value” can be of “char” and “int” type.

How do you use a switch statement inside a while loop?

identify the points you want to break OUT of the while loop within your switch body, set a flag indicating this, and make the while(expression) evaluate the flag for the break-state (i.e. while(true) becomes while(stay_in_while) , and have stay_in_while cleared by the switch code you want to break.


2 Answers

Reverse the cases and remove all break. Then remove common +=:

switch (u)
{
case 8:
    t += function(5, 3, 8, 1); // 11
    t += function(7, 2, 8, 0); // 12
case 7:
    t += function(4, 3, 7, 1); // 9
    t += function(6, 2, 7, 0); // 10
case 6:
    t += function(4, 2, 5, 0); // 7
    t += function(3, 3, 6, 1); // 8
case 5:
    t += function(2, 3, 5, 1); // 5
    t += function(4, 2, 5, 0); // 6
case 4:
    t += function(1, 3, 4, 1); // 4 
    t += function(3, 2, 4, 0); // 5
case 3:
    t += function(0, 3, 3, 1); // 3
case 2:
    t += function(1, 2, 2, 0); // 2
case 1:
    t += function(0, 2, 1, 0); // 1
}
like image 73
masoud Avatar answered Nov 03 '22 15:11

masoud


One option here would be to create an array of the four possible options to the function; and a mapping of which of these parameters to use for any value of u.

Then you can do those calls with the mapped parameters in a loop.

Like this:

int params[12][4] = {
    {0,2,1,0}, // 1
    {1,2,2,0}, // 2
    {0,3,3,1}, // 3
    // ...
};
vector<vector<int> > paramMapping;
paramMapping.push_back({1});
paramMapping.push_back({{1, 2});
paramMapping.push_back({{1, 2, 3});
paramMapping.push_back({{1, 2, 3, 4, 6});
paramMapping.push_back({{1, 2, 3, 4, 5, 6, 7});
// .. 

vector<int>::iterator it = paramMapping[u-1].begin();
while (it != paramMapping[u-1].end())
{
     int i = (*it) - 1;
     t += function(params[i][0], params[i][1], params[i][2], params[i][3]);
     ++it;
}

This solution, in contrast to the fallthrough-switch in M M.'s answer, would keep the calling order of the function calls the same as in your original code (which could be important in case function has side-effects, as mentioned in the comments).

The initialisation of the params and paramMapping should best be done outside of the actual computing function (somewhere in the initialisation of the class that contains the function for example).

Conclusion: With the addition of the mapping between number and parameters, this implementation has actually become quite complex, and its debatable whether its simpler than the original switch.

like image 30
codeling Avatar answered Nov 03 '22 15:11

codeling