473,466 Members | 1,358 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

break a while statement

38 New Member
hallo all,

how could I break the while statement inside the switch statement as follows:

Expand|Select|Wrap|Line Numbers
  1. enum st {s0, s1} state;
  2. while(true)
  3. {
  4. switch (state)
  5.  
  6. case s0:
  7. // do something
  8. break;
  9.  
  10. case s1:
  11. if(condition)
  12. //do something
  13. break;   //=======> here the while statement should be broken
  14. else
  15. //do something
  16. break;  //========> here the break of the case s1
  17.  
  18. }
any suggestion please..

best regards..
Jul 29 '10 #1
2 1232
code green
1,726 Recognized Expert Top Contributor
Use a variable in the while condition and set this to false inside case.
Expand|Select|Wrap|Line Numbers
  1. jump = 1;
  2. while(jump)
  3. {
  4.     switch (state)
  5.     {
  6.     case s0:
  7.     // do something
  8.     break;
  9.  
  10.     case s1:
  11.     if(condition)
  12.     //do something
  13.     jump = 0;
  14.     break; //=======> here the while statement should be broken
  15.      else
  16.     //do something
  17.     break; //========> here the break of the case s1
  18.     }
  19. }
  20.  
Hmmm, you will also need an escape form the while to prevent an infinite loop.
Why are you using a while loop around the switch anyway?
It seems unnessescary
Jul 29 '10 #2
Oralloy
988 Recognized Expert Contributor
Basically code_green has the answer.

There's always "goto". And now that I've recommended that, just forget I ever did.

Otherwise, introduce an exit condition, as code_green recommends - either a loop control variable or an "exit" state.

Expand|Select|Wrap|Line Numbers
  1. enum st {s0, s1, sExit} state;
  2. while(state != sExit)
  3. {
  4.   switch (state)
  5.   {
  6.   case s0:
  7.     // do something
  8.     break;
  9.  
  10.   case s1:
  11.     if(condition)
  12.     {
  13.       //do something
  14.       state = sExit;
  15.     }
  16.     else
  17.       //do something
  18.     break;
  19.   }
  20. }
  21.  
Jul 29 '10 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: Xah Lee | last post by:
# here's a while statement in python. a,b = 0,1 while b < 20: print b a,b = b,a+b --------------- # here's the same code in perl
1
by: jjliu | last post by:
Could someone point me out that why the following perl program only print 'title'. I expect the contents in h1, keywords and description to print out as well. thanks. JJL #!/usr/bin/Perl ...
4
by: muser | last post by:
I have a logical error in my program, I have submitted the program and my tutor hasn't listed the other problems with the code, but said that the program won't run because of a while statement....
4
by: James E Koehler | last post by:
I can't get the WHILE statement to work in MySQL. The version of MySQL that I am using is: Ver 12.16 Distrib 4.0.6-gamma, for Win95/Win98 (i32) running on Windows MX. Here is the relevant...
2
by: herrcho | last post by:
#include <stdio.h> int main() { char c; int length=0; printf("input string : "); gets(c); while(c != '\0'); printf("the length of input string is %d \n", length-1); printf("the number of...
1
by: Tmillwee | last post by:
I have the following while statement in my code While dataReader.read( ‘Do Stuff†End Whil The dataReader contains 19 rows of data, but the while loop continues and fails on a 20th loop. I...
1
by: hdogg | last post by:
Scope Woes - IF statement nested in WHILE statement -PHP I have an array $actuals_sum. <?php while(conditions) { if($i == '24) {
4
by: Villanmac | last post by:
First up thanks to the guys who pointed out my error last week. Now I was wondering If I could get some help with this problem because again I have fallen into trouble. Basically im using the...
2
by: mvillanu | last post by:
Hi All, I'm having trouble trying to figure out how to use the WHILE statement and I was wondering if anyone can help me. Here is what I would like to do. I have a table with dealer ID and BSID,...
2
by: nleake | last post by:
Hey, I've been trying to use Python to help me with some DNA sequencing work. I've figured out how to use Biopython for the importing work; however, I need some python code to help me remove a...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.