Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using `continue` keywoard in a switch nest inside a foreach loop [closed]

I have the code below (which actually is much longer than you see!)

foreach (SensorPair sensor in _sensorPairs)
{
    sensorByte = (byte) sensor.Sensor;

    if (!packet.Contains(sensorByte))
        continue;

    index = packet.IndexOf(sensorByte);
    byteCount = sensor.ByteCount;

    switch (byteCount)
    {
        case 1:
            try
            {
                switch(sensor.ValueType)
                {
                    case SensorValueType.Unsigned:
                        val = (int)packet[index + 1];
                        if (val > 255)
                            //*** WHAT DOES THIS CONTINUE DO?
                            continue;   
                        else //rise the event
                           OnSensorReport();                              
                    break;

Does the continuekeywoard you see cause the foreach loop to start itterating next item or it just passes to the next case statement?

If it does not do anything with the foreach loop, how can I force the code to exit the switch and starts the next itteration in the foreach loop?

like image 718
Saeid Yazdani Avatar asked Jan 07 '13 22:01

Saeid Yazdani


People also ask

Can we use continue in forEach loop?

In C#, the continue statement is used to skip over the execution part of the loop(do, while, for, or foreach) on a certain condition, after that, it transfers the control to the beginning of the loop. Basically, it skips its given statements and continues with the next iteration of the loop.

Can you use continue in a switch statement?

We can not use a continue with the switch statement. The break statement terminates the whole loop early. The continue statement brings the next iteration early. It stops the execution of the loop.

Can we use while loop inside switch?

Yes, it is possible.


1 Answers

Yes, it continues the foreach loop.

It is always useful to consult the documentation ;-)

The continue statement passes control to the next iteration of the enclosing while, do, for, or foreach statement in which it appears.

or—more comprehensive—the C# language specification:

8.9.2 The continue statement

The continue statement starts a new iteration of the nearest enclosing while, do, for, or foreach statement.

The target of a continue statement is the end point of the embedded statement of the nearest enclosing while, do, for, or foreach statement. If a continue statement is not enclosed by a while, do, for, or foreach statement, a compile-time error occurs.

When multiple while, do, for, or foreach statements are nested within each other, a continue statement applies only to the innermost statement. To transfer control across multiple nesting levels, a goto statement (§8.9.3) must be used.

A continue statement cannot exit a finally block (§8.10). When a continue statement occurs within a finally block, the target of the continue statement must be within the same finally block; otherwise a compile-time error occurs.

A continue statement is executed as follows:

  • If the continue statement exits one or more try blocks with associated finally blocks, control is initially transferred to the finally block of the innermost try statement. When and if control reaches the end point of a finally block, control is transferred to the finally block of the next enclosing try statement. This process is repeated until the finally blocks of all intervening try statements have been executed.

  • Control is transferred to the target of the continue statement.

Because a continue statement unconditionally transfers control elsewhere, the end point of a continue statement is never reachable.

like image 73
Tim Schmelter Avatar answered Sep 28 '22 10:09

Tim Schmelter