Break out of a while loop that contains a switch s

2019-01-08 22:04发布

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,

15条回答
疯言疯语
2楼-- · 2019-01-08 22:39

The only other way I know of is the dreaded goto. MSDN also says this.

However, I see no reason why you'd use it in this case. The way you have implemented works fine, and is more maintainable than a goto. I would keep what you have.

查看更多
萌系小妹纸
3楼-- · 2019-01-08 22:41

May or may not work but lamda why not give it a shot just for fun

while(  (expr) => (){
switch(expr){
case 1: dosomething; return true; 
case 2 : something;return true;
case exitloop:return false;}
});   
查看更多
虎瘦雄心在
4楼-- · 2019-01-08 22:43

Wrap it into a function and use a return statement to exit. How about that?

查看更多
Lonely孤独者°
5楼-- · 2019-01-08 22:45

IMO, this seems a perfectly fine way of breaking out of a while loop. It does what you expect with no side effects. I could think of doing

if(!keepOnLooping)
  break;

But that's not really any different in terms of execution.

查看更多
小情绪 Triste *
6楼-- · 2019-01-08 22:46

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.

查看更多
聊天终结者
7楼-- · 2019-01-08 22:48

You could change the switch statement to a for/foreach loop. Once the condition is met set "keepOnLooping" to false and then use break to get out of the loop. The rest should take care of itself.

查看更多
登录 后发表回答