Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Break out of a while loop that contains a switch statement

I am having trouble figuring out how to break out of a loop that contains a switch statement. Break breaks out of the switch, not the loop.

There is probably a more elegant solution to this. I have implemented a flag that starts out as true and gets set to false and ends the loop. Can you offer a better solution?

Background: this code is used in a bar code workflow system. We have PocketPCs that have bar code scanners built in. This code is used in one of those functions. It prompts the user for different pieces of data throughout the routine. This piece allows them to scroll through some inventory records displaying that info on the PocketPC terminal (paged results) and allows them to enter "D" for Done, "Q" to quit.

Here is the current C# example that needs to be improved:

do {     switch (MLTWatcherTCPIP.Get().ToUpper())     {         case "": //scroll/display next inventory location             MLTWatcherTCPIP.TerminalPrompt.ScrollBodyTextDown();             break;         case "P": //scroll/display previous inventory location             MLTWatcherTCPIP.TerminalPrompt.ScrollBodyTextDown();             break;         case "D": //DONE (exit out of this Do Loop)             // break; // this breaks out of the switch, not the loop             // return; // this exists entire method; not what I'm after             keepOnLooping = false;             break;         case "Q": //QUIT (exit out to main menu)             return;         default:             break;     } } while (keepOnLooping); 

Here is an example of code that does this in VB.NET

Do     Select Case MLTWatcherTCPIP.Get().ToUpper         Case "" ''#scroll/display next inventory location             MLTWatcherTCPIP.TerminalPrompt.ScrollBodyTextDown()         Case "P" ''#scroll/display previous inventory location             MLTWatcherTCPIP.TerminalPrompt.ScrollBodyTextUp()         Case "D" ''#DONE (exit out of this Do Loop)             Exit Do         Case "Q" ''#QUIT (exit out to main menu)             Return     End Select Loop 

Thanks,

like image 466
joshblair Avatar asked Dec 31 '09 22:12

joshblair


People also ask

How do you break a while loop with a switch case?

Use the continue statement to finish each case label where you want the loop to continue and use the break statement to finish case labels that should terminate the loop. Of course this solution only works if there is no additional code to execute after the switch statement.

Does Break break out of a switch statement?

You can use the break statement to end processing of a particular labeled statement within the switch statement. It branches to the end of the switch statement. Without break , the program continues to the next labeled statement, executing the statements until a break or the end of the statement is reached.

How do you break out of a while loop?

To break out of a while loop, you can use the endloop, continue, resume, or return statement. endwhile; If the name is empty, the other statements are not executed in that pass through the loop, and the entire loop is closed.

How do you stop a switch loop?

Switching loops can be prevented using Spanning Tree Protocol (STP). The purpose of Spanning Tree Protocol is to select the fastest network path if there are redundant links in the network.


1 Answers

I'd try to avoid it, but you could use...

goto

However, angry mobs with pitchforks become an occupational hazard if you choose to do so.

like image 189
Kevin Montrose Avatar answered Sep 22 '22 05:09

Kevin Montrose