c - Is there a way to loop a switch case base program from within the switch case statement? -
so while practicing variations of switch-case blocks, wanted try , loop switch-case part of program within not able far in trials. tried using continue not work unless switch-case block inside loop. tried designing while loop conditions, worked not accurately, did not execute blocks inside cases break out of switch , terminate program cases not executed. tried goto believe there rules labels used far not favouring use of goto inside switch. researching shows people looping switch-case externally (like switch-case inside while loop or loop etc), not find examples of 1 trying build loop within switch-case block of code. don't mind if solution repeats code beginning or starts external point outside switch-case again, purpose program somehow re-execute switch-case without enveloping same switch-case in loop. learning c programming again after long time hence did long search not find apt result. maybe doing long search after long time might have lost touch. there similar question solution given , accepted user indicates use of while envelope switch case , not other way around want. here link
c programming while switch case program
here code:-
#include <stdio.h> int main() { int input; printf( "4. play game\n" ); //i have re-edited question , shortened code , included additionally tried per advice, focus on question more, w/o changing main question . printf( "selection: " ); scanf( "%d", &input ); switch ( input ) { case 4: printf( "thanks playing! taaaa\n" ); break; default: while(input!=4) //just sample condition, , know doesnt check letters or characters input, not point. want see if solution on similar thought/methodology exists. { printf( "no u have give correct input\n" ); scanf("%d", &input); continue; // tried goto , return; in loop realize not jump me out of loop , program. } } return 0; }
alternatively, can while loop used made run not accurately yet without errors or warnings. thing while loop, described earlier above, did not execute blocks inside cases broke out.
i tried attach output image seems reputation went below 10 due down-voting hence couldnt attach sorry. attached again.
if want insane code, can mess around variants on code:
#include <stdio.h> /* ** disclaimer ** appalling idea - not use it. ** seems meet criteria of 30553881. ** maybe. */ extern void playgame(void); extern void designgame(void); extern void playmultiplayer(void); int main(void) { int input; printf("1. play game\n"); printf("2. design game\n"); printf("3. play multiplayer\n"); printf("4. exit\n"); printf("5. auto-try-again\n"); printf("selection: "); if (scanf("%d", &input) == 1) { redo: switch (input) { while (input > 0 && input != 4) { case 1: playgame(); break; case 2: designgame(); break; case 3: playmultiplayer(); break; case 4: printf("taaaa!\n"); break; default: printf("no u have give correct input\n"); break; } if (input != 4 && scanf("%d", &input) == 1) goto redo; } } return 0; } void playgame(void) { printf("%s called\n", __func__); } void designgame(void) { printf("%s called\n", __func__); } void playmultiplayer(void) { printf("%s called\n", __func__); }
note break;
statements after case labels break while
loop, not switch
. use continue
instead of break
, mean next iteration execute playgame()
, exit loop (because of break
— unless replace break
continue
, or goto
of sort, or return
).
disclaimer
this appalling code — not use it!
but show can sorts of things in c insane. duff's device see plausibly useful chunk of code related (which not plausibly useful).
Comments
Post a Comment