+ 1
How do you exit from a while loop from a switch case? [ANSWERED]
Suppose I have a program in a while loop. Inside this loop, there exists some switch cases, like this: while(true) { cin>> n; switch(n) { case 1: {} case 2: {} } } Now I wish to exit from the loop itself, not just the switch statement. How does one achieve this? I am currently using goto, but I wish to optimize my approach. I am unable to use break, as it simply brings meout of the switch case, but the program is still stuck inside the while.
21 ответ
+ 21
recently, I've learned an unusual way to work with while guy
If you put your read statement ( cin ) inside while loop you can control its behaviour based on your input. as many friends here know, cin is an istream object and if it reads something inconsistent from right side of >> operator OR if you insert EOF by doing Ctrl + Z on Win/ Ctrl + D on Linux or Android or Mac , its flag turns to false. By taking advantage of this capability we would come up with the following snippet.
int x = 0, y = 0;
while (std::cin >> x)
y += x;
The above loop continue to read the input as long as you put an integer value in prompt.
You may even want to make it a bit interesting like this.
int i = 0;
while ( std::cout << "#" << ++i << ": " && std::cin >> x)
+ 14
@Kinshuk I just wanted to say that you need an if statement outside of switch statement for Jamie's code to work.
+ 14
Ok, I was checking everything in my own similar project, maybe I misinterpreted but I need an if outside of switch, so that the code exits the while loop (and menu in my case) and goes to do something else, otherwise it just returns to the menu...
Anyway, if it works for Kinshuk, that's great. 👍
+ 14
So, to sum this all up, you just needed an if and break. It's funny how we sometimes get stuck with such simple things that are kind of obvious. Lol, happens to me all the time. :)
while(true)
{
cin>> n;
if(n == 999)
break;
switch(n)
{
case 1:
{}
case 2:
{}
}
}
+ 12
case 0:
cout << "Thank you for using my program." ;
return 0;
+ 5
use break or return
+ 3
What if I have more code after the while loop which I need to execute? Return won't help me in that.
I guess then Ill have to pack my while loop inside a dummy function, and then Ill be able to use return.
But I'm still waiting, for a better solution.
+ 3
@Jamie
Yes, this will work for me!
I can also do this:
while(true)
{
switch(n)
{
case 1: {}
case 2: {}
}
if(done) break;
}
Thank You!
+ 3
@Jamie
My case is just like the exception you described just now. So Ill further use an if. Thank You again!
+ 2
So either use a goto label, or a return inside a dummy function. Right?
I guess there are no other solutions than jumps.
@Very_Hard(asm & js & c++ & java & py & (@Hard))
The continue statement will only skip one iteration, not the loop itself. Ill use goto. Thank You!
+ 2
@DPlusPlus
Actually, you don't. See the while loop.
In my code though, you need the extra if.
+ 1
@Shubham Sharma
break doesn't work for both switch and the while loop.
0
I think the simplest way to do it is using a loop counter.
int counter=0;
while( counter!=1 )
{
cin>> n;
switch(n)
{
case 1:
{}
case 2:
{
counter=1; //Condition becomes false for loop so it will terminate.
break;
}
}
}
0
bool loop_control = true;
while(loop_control)
{
cin>> n;
switch(n)
{
case 1:
...
break;
case 2:
...
break;
default:
loop_control = false;
break;
}
//if you need to control something here, you can also use loop_control here
//So code dependent and independent of loop_control could run.
if(loop_control) {
...
} else {
...
}
//independent code
...
...
}
This could be one simple way of doing that I guess?
0
You can just simply write "return 0;" In place of a break inside the switch case . then simply your program terminates, and you will be moved out of your program.
I hope it will help you understand; if you have any queries, ask again.
0
exit() function will help a lot
https://www.digitalocean.com/community/tutorials/exit-function-c-plus-plus
click the link for detailed explanation