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");

Just put in a test between the end of your switch and the end of the while loop.

} //end of switch case

if (i == 0)
     break; // you need another break to get out of the while
} //end of while

(if you can still edit your post, pop in a [/code] at the bottom of your text, you had it right at the top)

Alternatively you could set your condition as the loop condition. A cunstruction I would use would be a do-while setup, as displayed below:

int i;

do
{//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
while(i != 0);

You can setup a flag outside the while loop, and test for it in the switch statement.
Something like this..

bool flag = 1;
while(flag){//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");
                        flag = 0;
			break;
               default: 
			printf("Give a Valid Option");
			break;
	}//end of switch - case
	}//end of while
	printf("END");

Breaking out of a loop from a switch is one of the few places in which a goto 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 case is probably going to be a cleaner alternative.

#include <stdio.h>

void foo(void)
{
   int i;
   while ( 1 )
   {
      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");
         return;
      default:
         printf("Give a Valid Option");
         break;
      }
   }
}

int main()
{
   foo();
   printf("END");
   return 0;
}

Be careful using scanf for user input the way you do. If you aren't already aware of the leftover newline character, search the forum a bit ("leftover newline" or "flushing input stream" or somesuch).

commented: Clear post, great suggestion(s). +6

Thank you for the replies and i am sure all will work fine in this case..
here i was just wondering whether we do have any provision in C to have multiple loop exit in one single statement?

multiple loop exit in one single statement?

Do you mean if two while loops are nested? If so, you need a break at each level.

while(condition1)
{
      while(condition2)
      {
               if(condition3)
                    break;
      }
      
      if (condition4)   //you could definitely** make condition4 depend
             break;        //on condition3 via a flag or something
}

**whether or not this is the best practice, I honestly do not know

IMO, 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 0:
         printf("Thank you for using the service");
         break;
      default:
         printf("Give a Valid Option");
         break;
      }
   }
   return;
}

This leaves one exit of the function -- at the end. I feel it's cleaner, but that's just me...

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.