It’s possible. You’ve accidentally written an infinite loop because you’re not incrementing x
, which you probably want to do after the switch.
But if you just want to run those 3 blocks of code in that order, why not just have those 3 blocks of code in that order? You gain absolutely nothing by wrapping them up in the loop and the switch. Let’s say you wrote 3 functions. This block of code:
while(x<4){
switch(x)
{
case 1:
f1();
break;
case 2:
f2();
break;
case 3:
f3();
break;
}
x++;
}
is exactly the same as:
f1();
f2();
f3();
If there is a lot of similarity between the stuff you would put in each of these 3 functions, perhaps write one function and call it 3 times, with slightly different parameters.
1
solved iterating over cases in c++ switch statement? [closed]