segunda-feira, 23 de dezembro de 2013

terça-feira, 7 de fevereiro de 2012

Control Structures goto

goto

Transfers program flow to a labeled point in the program

Syntax

label:
goto label; // sends program flow to the label

Tip

The use of goto is discouraged in C programming, and some authors of C programming books claim that the goto statement is never necessary, but used judiciously, it can simplify certain programs. The reason that many programmers frown upon the use of goto is that with the unrestrained use of goto statements, it is easy to create a program with undefined program flow, which can never be debugged.
With that said, there are instances where a goto statement can come in handy, and simplify coding. One of these situations is to break out of deeply nested for loops, or if logic blocks, on a certain condition.

Example

for(byte r = 0; r < 255; r++){
    for(byte g = 255; g > -1; g--){
        for(byte b = 0; b < 255; b++){
            if (analogRead(0) > 250){ goto bailout;}
            // more statements ... 
        }
    }
}
bailout:

References

Control Structures return

return

Terminate a function and return a value from a function to the calling function, if desired.

Syntax:

return;
return value; // both forms are valid

Parameters

value: any variable or constant type

Examples:

A function to compare a sensor input to a threshold
int checkSensor(){       
    if (analogRead(0) > 400) {
        return 1;
    else{
        return 0;
    }
}
The return keyword is handy to test a section of code without having to "comment out" large sections of possibly buggy code.
void loop(){

// brilliant code idea to test here

return;

// the rest of a dysfunctional sketch here
// this code will never be executed
}

See also

comments

References 

Control Structures continue

continue

The continue statement skips the rest of the current iteration of a loop (do, for, or while). It continues by checking the conditional expression of the loop, and proceeding with any subsequent iterations.

Example

for (x = 0; x < 255; x ++)
{
    if (x > 40 && x < 120){      // create jump in values
        continue;
    }

    digitalWrite(PWMpin, x);
    delay(50);
}
References

Control Structures break

break

break is used to exit from a do, for, or while loop, bypassing the normal loop condition. It is also used to exit from a switch statement.

Example

for (x = 0; x < 255; x ++)
{
    digitalWrite(PWMpin, x);
    sens = analogRead(sensorPin);  
    if (sens > threshold){      // bail out on sensor detect
       x = 0;
       break;
    }  
    delay(50);
}

References

Control Structures do... while

do - while

The do loop works in the same manner as the while loop, with the exception that the condition is tested at the end of the loop, so the do loop will always run at least once.
do
{
    // statement block
} while (test condition);

Example

do
{
  delay(50);          // wait for sensors to stabilize
  x = readSensors();  // check the sensors

} while (x < 100);

References

Control Structures while

while loops

Description

while loops will loop continuously, and infinitely, until the expression inside the parenthesis, () becomes false. Something must change the tested variable, or the while loop will never exit. This could be in your code, such as an incremented variable, or an external condition, such as testing a sensor.

Syntax

while(expression){
  // statement(s)
}

Parameters

expression - a (boolean) C statement that evaluates to true or false

Example

var = 0;
while(var < 200){
  // do something repetitive 200 times
  var++;
}

References