Program Flow

From robotics

There are several control structures to get your program to flow the way you want.

Simple conditionals: if, switch-case

Loops: while, do-while, for

Jump statements: break, continue, return, goto, exit

Conditionals

If

The if statement is used evaluate a condition and execute the code that follows if the condition is true.

if( x < y){
  // execute anything between the curly bracket once if the condition is met.
}

The if statement can also be used extended with else:

if( x < y){
  // execute if condition is true
} else {
  // execute if condition is false
}

It can also be extended to many cases with any number of else if clauses:

if( x < y){
  // execute if condition is true
} else if ( x > y){
  // execute if condition is first condition (x<y) is false, but second condition (x>y) is true
} else {
  // execute if none of the conditions were true
}

Note that in a if-else if-else block, one and only one of the blocks of code will be executed.

Switch case

The switch case statement can be used to replace certain if-else if-else blocks, when all you need is to execute different code depending on an exact value of a variable.

switch(var){
case 1:
 //if var == 1 then run until the next break
break;
case 2: 
 // if var == 2 then run until the next break
break;
default: 
 // if var does not match another case do this
}

Keep in mind that only numbers and characters can be used with switch-case, not Strings or any other datatype. Also, omitting break; in a case will cause "fall-through" and the next case below it will be executed.

Loops

The for loop is used to repeat the code within the curly brackets a set amount of times.

for(int x =0; x <10; x++) // execute anything between the curly bracket until the x started in the first parameter, reaches the second parameter, while incrementing by the third parameter each time the loop runs.


The while loop is used to continuously run a loop until the condition is not met.

while (x>10) // execute anything between the curly bracket continuously until the condition is not met. 


The do .. while loop is used to run a set of code once before checking the while condition. This allows the code to execute at least once before checking the condition of the while.

do // executes anything between the curly brackets once then continuously if the while condition is met.
while (x<y);

Switch case and break

Switch cases allow you to switch between different cases or scenarios without writing lots of if statements. The switch case takes in a variable and compares it to each of its cases and runs the code in the case that matches.

Break is used to exit the switch case. If you do not put a break in each case then the program will “fall through” to the next case and execute that code.

switch(var){
case 1: //if var = 1 then run until the next break
break;
case 2: // if var = 2 then run until the next break
break;
default: // if var does not match another case do this
}

continue, return, goto, and exit

Continue is used to skip the rest of a loop and rechecks the condition statement. Works with do, while, and for loops.

for (x = 0; x < 255; x ++) {

   if (x > 40 && x < 120){      // create jump in values
      continue;

}

//code here will be skipped, until the if statement //becomes false.

}

Return will terminate a function. You can also use it to return a value from the function;

return; return val;

goto is used to go to a pre-defined point in the program and is frowned upon in the CS community.

loop{
for (byte r =0; r<20:r++){
for (byte g =0;g>20;g++){
If (digitalRead(pin)=1){
goto bail;
}}}}
bail: // anything here will be run if the goto is reached

exit is used to end the program and return a value. exit is not referenced anywhere in the documentation currently.

loop{
Serial.print(“hi”); // print hi
exit(1);  // ends the loop and outputs “Exiting with status 1.” Over serial.
}