0

I have this in my code:

for(each player)
{
  for(each packet of this player)
  { 
    switch(packet_id)
    {
      case PACKET_TYPE:
      { 
         if(someething is wrong)
         {
           skip this player iteration and go on with next player
         }
      }break;
     }
   }
}

There are Two 'FOR' and one 'SWITCH'

How do i escape from the current PLAYER iteration, jumping to another player, from within the switch statements?

  • A break will cause 'case' to break;
  • A continue; will cause next packet to be iterated
  • .. ?
PeeS
  • 1,164
  • 3
  • 20
  • 43
  • 1
    You can use goto. You can put the inner loop in a function and return from it. You can use a flag for that, that is checked in both loops. It's not really that important. – selalerer Oct 28 '13 at 13:00

5 Answers5

5

There's no particularly nice way to do it. Options are:

  • Move the inner loop into its own function, and exit using return; or
  • Set a flag to indicate that the inner loop should exit; or
  • Use goto to jump out of the inner loop.
Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
  • Will have to manage it some other way around, as i am looking forward to code it more clearly than using gotos /flags. – PeeS Oct 29 '13 at 07:00
  • Handled it by moving the inner loop into own function that when needed invokes 'return' from the method to skip processing current player. Sorry for the DUP, the case can be closed now. – PeeS Oct 29 '13 at 07:10
2

A break will cause 'case' to break?

Yes.

A continue; will cause next packet to be iterated?

Yes.

You can add a flag and AND (&&) it with the inner loop:

for(each player)
{
    bool flag = true;
    for(each packet of this player && flag)
    {                              ^^^^^^^ 
        switch(packet_id)
        {
            case PACKET_TYPE:
            { 
                if(someething is wrong)
                {
                    flag = false;
                    ^^^^^^^^^^^^^
                }
            }break;
        }
    }
}

Also, there is a polite usage of goto in this case for escaping through nested switches/loops.

"This is the last remaining stronghold for the use of goto." read here

Community
  • 1
  • 1
masoud
  • 55,379
  • 16
  • 141
  • 208
1

You could refactor the packet loop for the player into its own function or method and the condition statement could then have a return; which would dump you back into the players loop.

void processPackets(player){
  for(each packet of this player)
  { 
    switch(packet_id)
    {
      case PACKET_TYPE:
      { 
         if(something is wrong)
         {
           return;
         }
      }break;
     }
   }
}
quamrana
  • 37,849
  • 12
  • 53
  • 71
0

What about a bool flag proceedToNextPlayer?:

for(each player)
{
  bool proceedToNextPlayer = false;
  for(each packet of this player && !proceedToNextPlayer) 
  { 
    switch(packet_id)
    {
      case PACKET_TYPE:
      { 
         if(someething is wrong)
         {
           proceedToNextPlayer = true;
           skip this player iteration and go on with next player
         }
      } break;
     }
   }
}
lolando
  • 1,721
  • 1
  • 18
  • 22
0

You can always use flag:

for(each player)
{
    bool skip_this_player = false;
    for(each packet of this player and not skip_this_player)
    { 
        switch(packet_id)
        {
            case PACKET_TYPE:
            { 
                if(someething is wrong)
                {
                    skip_this_player = true;
                    continue;
                }
             }break;
         }
     }
  }
Paul Evans
  • 27,315
  • 3
  • 37
  • 54