while(1){//return from the loop only when the input is zero
printf("\n1: Stack1\n2: Stack2\n0: to return\n");
scanf("%d",&i);
switch(i){
case 1:
printf("1");
break;
case 2:
printf(" 2");
break;
case 0:
printf("Thank you for using the service");
break;
default:
printf("Give a Valid Option");
break;
}//end of switch - case
}//end of while
printf("END");
deepin 0 Newbie Poster
Recommended Answers
Jump to PostBreaking out of a loop from a
switch
is one of the few places in which agoto
does exactly what is intended without introducing more complexity.But this is typically not the solution; instead, "functionizing" the loop/switch and using a
return
statement from the desired …
Jump to PostIMO, better code would be:
#include <stdio.h> void foo(void) { int i = 1; while ( i ) // or while ( i != 0) { printf("\n1: Stack1\n2: Stack2\n0: to return\n"); scanf("%d",&i); switch ( i ) { case 1: printf("1"); break; case 2: printf(" 2"); break; case …
All 7 Replies
jonsca 1,059 Quantitative Phrenologist Team Colleague Featured Poster
Gonbe 32 Newbie Poster
vaibhav2614 0 Newbie Poster
Dave Sinkula 2,398 long time no c Team Colleague
tux4life commented: Clear post, great suggestion(s). +6
deepin 0 Newbie Poster
jonsca 1,059 Quantitative Phrenologist Team Colleague Featured Poster
WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.