### Is it possible to execute the contents of a switch statement from the bottom-up?

 ``123456`` ``````switch (i) { case 1: printf("1"); case 2: printf("2"); case 3: printf("3"); }``````

Is there a way to execute the above switch statement such as when

i = 1, the output is `1`
i = 2, the output is `12`
i = 3, the output is `123`
No simple way that I can see, without making it more than three commands. Sounds like you're trying to push the limits of what case fallthroughs are meant for. At which point, it would be more clear to just use a standard for loop.

 ``1234`` ``````for (int n = 1; n <= i; n++) { printf("%d", n); }``````

Related:
https://stackoverflow.com/questions/188461/switch-statement-fallthrough-should-it-be-allowed
Last edited on
@aumars

Here's one way..

 ``12345678910111213141516171819202122232425262728293031`` ``````int len; cout << "Type a number, 1 thru 7.."<> len; for(int x=1;x<=len;x++) switch(x) { case 1: cout << "1"; break; case 2: cout << "2"; break; case 3: cout << "3"; break; case 4: cout << "4"; break; case 5: cout << "5"; break; case 6: cout << "6"; break; case 7: cout << "7"; } }while (len>0 && len < 8);``````
Are you certain a switch is the right tool for your problem?

This looks like an XY problem ( xyproblem.info ). If this is the case, best ask about your problem rather than your attempted solution.
 ``1234567891011`` ``````#include #include using namespace std; int main() { const string digits = "0123456789"; int i; cout << "Enter i: "; cin >> i; cout << digits.substr( 1, i ); }``````

I'm inclined to agree with @Ganado and @mbozzi, though:
Last edited on
I'm trying to print three strings in a predetermined order, and the number of strings being printed is `i`

so

 ``123456`` ``````switch (i) { case 1: printf("%s", "A"); case 2: printf("%s", "B"); case 3: printf("%s", "C"); }``````

So the resulting string is either A, AB, or ABC.

I apologise for the confusion, this has nothing to do with printing numbers, and everything to do with printing strings in a particular order.
Last edited on
Do we actually care what is printed? No. You want something printed "in order".

You can do that with a switch: EDIT: not easily.
 ``1234567`` ``````using std::cout; switch (i) { case 3: cout << "A"; case 2: cout << "B"; case 1: cout << "C"; }``````

However, a loop is good too:
 ``12345`` ``````std::vector words { "A", "B", "C" }; for ( int w=0; w < i and w < words.size(); ++w ) { std::cout << words[w]; }``````
Last edited on
 ``123456789101112`` ``````#include int main() { puts( "enter a number in [1,9]: " ) ; unsigned int i = 0 ; scanf( "%u", &i ) ; char text[] = "123456789" ; if( i < 9 ) text[i] = 0 ; puts(text) ; }``````
I solved the issue using `std::deque`:

 ``1234567891011121314`` ``````#include #include std::deque d; int i; switch (i) { case 3: d.push_front("C"); case 2: d.push_front("B"); case 1: d.push_front("A"); } for (auto &it: d) std::cout << it;``````
Last edited on
Does a recursive function meet your idea, like this:

string func(int n) {
return n>0 ? func(n-1)+char('A'+n-1) : "";
}
OP is working with an XY problem.