Foreach skipping, switch cases.
#1

Umm...I was working on one of my scripts and I have to leave now, so, I was working on a problem where I want the foreach loop to go only once in one of the cases inside the switch. For example,

pawn Код:
foreach(new i : Player)
{
    switch(whatever_var)
    {
        case 1:
        {
            //does whatever
            //happens fully, like loops normally as foreach does.
        }
        case 2:
        {
            //does whatever
            //occurs once, foreach does not loop through this case more than once <== That is the question, how to make that possible
         }
     }
}
So, yeah, I might as well figure it out when I come back but until then if someone can help it will be appreciated.
Reply
#2

You could do something like this:

pawn Код:
new checked = 0;

foreach(new i : Player)
{
    switch(whatever_var)
    {
        case 1:
        {
            //does whatever
            //happens fully, like loops normally as foreach does.
        }
        case 2:
        {
            if(checked == 0)
            {
                checked = 1;
                //does whatever
                //occurs once, foreach does not loop through this case more than once <== That is the question, how to make that possible
            }
        }
    }
}
Sorry about the indentation, the forum messes it up.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)