How do you exit from a while loop from a switch case? [ANSWERED] | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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.

9th Nov 2017, 11:46 AM
Kinshuk Vasisht
Kinshuk Vasisht - avatar
17 Answers
+ 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)
9th Nov 2017, 3:09 PM
Babak
Babak - avatar
+ 14
@Kinshuk I just wanted to say that you need an if statement outside of switch statement for Jamie's code to work.
9th Nov 2017, 12:21 PM
dρlυѕρlυѕ
dρlυѕρlυѕ - avatar
+ 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. 👍
9th Nov 2017, 12:33 PM
dρlυѕρlυѕ
dρlυѕρlυѕ - avatar
+ 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: {} } }
9th Nov 2017, 1:12 PM
dρlυѕρlυѕ
dρlυѕρlυѕ - avatar
+ 12
case 0: cout << "Thank you for using my program." ; return 0;
9th Nov 2017, 11:49 AM
dρlυѕρlυѕ
dρlυѕρlυѕ - avatar
+ 5
use break or return
10th Nov 2017, 1:35 AM
Asiri H
Asiri H - avatar
+ 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.
9th Nov 2017, 11:51 AM
Kinshuk Vasisht
Kinshuk Vasisht - avatar
+ 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!
9th Nov 2017, 12:14 PM
Kinshuk Vasisht
Kinshuk Vasisht - avatar
+ 3
@Jamie My case is just like the exception you described just now. So Ill further use an if. Thank You again!
9th Nov 2017, 12:26 PM
Kinshuk Vasisht
Kinshuk Vasisht - avatar
+ 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!
9th Nov 2017, 12:05 PM
Kinshuk Vasisht
Kinshuk Vasisht - avatar
+ 2
@DPlusPlus Actually, you don't. See the while loop. In my code though, you need the extra if.
9th Nov 2017, 12:23 PM
Kinshuk Vasisht
Kinshuk Vasisht - avatar
+ 1
return
9th Nov 2017, 11:50 AM
Niush
Niush - avatar
+ 1
@Shubham Sharma break doesn't work for both switch and the while loop.
9th Nov 2017, 3:39 PM
Kinshuk Vasisht
Kinshuk Vasisht - avatar
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; } } }
3rd Oct 2020, 10:29 AM
Talha Ahmed
Talha Ahmed - avatar
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?
24th Mar 2022, 5:24 AM
Kevin Madhu
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.
17th Apr 2023, 7:15 PM
Ankesh Kumar Pandey
0
exit() function will help a lot https://www.digitalocean.com/community/tutorials/exit-function-c-plus-plus click the link for detailed explanation
23rd Nov 2023, 8:51 AM
Dhuv