# Difference between revisions of "Control Structures"

Jump to: navigation, search

## do while

do{ statements }while(expression)
The do-while loop will run the statements between the braces over and over as long as the expression results in a non zero value (true). Once the expression results false, the program will skip beyong the while loop and move on. Since the do-while loop always checks the condition of the expression last, the do-while will ALWAYS run the statments at least once.
 ```int bursts = 0; // Create variable 'bursts' of type int and set it to '0'.   do { motor[rightMotor] = 50; // run 'rightMotor' at power level 50 motor[leftMotor] = 50; // run 'leftMotor' at power level 50   bursts = bursts + 1; // increment 'bursts' by 1   wait1Msec(3000); // wait for 3000 milliseconds before continuing   }while(bursts < 3) // while 'bursts' is less than 3:   motor[rightMotor] = 0; // stop 'rightMotor' motor[leftMotor] = 0; // stop 'leftMotor'```
• In this example, the loop will run the motors forward at a power level of 50 in three second bursts. It will run for 3 bursts before running the code after the structure, stopping the motors. It will always run for at least 1 three second burst before checking to see the condition of the expression.

## for

for(expression1; expression2; expression3){ statements }
The for loop runs the code between its braces as long as expression2 is true (non zero). It is a neat and compact solution to looping.
 ```for(int i = 0; i <= 3; i++) // initialize int 'i' to 0, and run the loop as long as 'i' is less than 3, // incrementing 'i' by 1 after each iteration of the loop { motor[rightMotor] = 50; // run 'rightMotor' at power level 50 motor[leftMotor] = 50; // run 'leftMotor' at power level 50 wait1Msec(3000); // wait for 3000 milliseconds before continuing. motor[rightMotor] = 50; // run 'rightMotor' at power level 50 motor[leftMotor] = -50; // run 'leftMotor' at power level -50 wait1Msec(500); // wait for 500 milliseconds before continuing. }   motor[rightMotor] = 0; // stop 'rightMotor' motor[leftMotor] = 0; // stop 'leftMotor'```
• In this example, the loop will run the motors forward at a power level of 50 for three seconds and then turn left for 500 milliseconds. It will repeat this process 4 times, completing a square.

## if else

if(expression){ statement1 } else { statement2 }
In the if statement, if the expression in parentheses is nonzero (true), control passes to statement 1. If the else clause is present and the expresssion is zero (false), control will pass to statement 2. The else part is optional, and if absent, a false expression will simply result in skipping over the statement 1. An else always matches the nearest previous unmatched if; braces may be used to override this when necessary, or for clarity.
 ```if(SensorValue(touch1) == 1) // if the touch sensor 'touch1' reads '1' (pressed): { nxtDisplayCenteredTextLine(2, "Touch pressed"); // display "Touch pressed" to the VEX LCD } else // else (the touch sensor 'touch1' reads '0' [unpressed]): { nxtDisplayCenteredTextLine(2, "Touch unpressed"); // display "Touch unpressed" to the LCD }```

## switch case

switch(expression){ case label1: statements1; case label2: statements2; break; default: statements3; }
The switch statement causes control to be transferred to one of several statements depending on the value of an expression, which must have integral type. The Substatement controlled by a switch is typically compound. Any statement within the substatement may be labeled with one or more case labels, which consist of the keyword case followed by a constant expression and then a colon (:).

No two of the case constants associated with the same switch may have the same value. There may be at most one default label associated with a switch - if none of the case labels are equal to the expression in the parentheses following switch, control passes to the default label, or if there is no default label, execution resumes just beyond the entire construct. Switches may be nested; a case or default label is associated with the innermost switch that contains it. Switch statements can "fall through", that is, when one case section has completed its execution, statements will continue to be executed downward until a break; statement is encountered. Fall-through is useful in some circumstances, but is usually not desired. In the preceding example, if label2 is reached, the statements statements 2 are executed and nothing more inside the braces. However if label1 is reached, both statements 1 and statements 2 are executed since there is no break to separate the two case statements.

 ```int nTaskToStart = 2; // int 'nTaskToStart' is set to '2'   switch(nTaskToStart) // test 'nTaskToStart' in the switch { case 1: // if 'nTaskToStart' is '1': StartTask(One); // start task One break; // break out of this switch statement and continue code after the '}'   case 2: // if 'nTaskToStart' is '2': StartTask(Two); // start task Two break; // break out of this switch statement and continue code after the '}'   default: // if 'nTaskToStart' is anything other than '1' or '2': StartTask(Three); // start task Three }```
• In this example only task Two is ever started. Had nTaskToStart been '1' then task only One would have been started. Likewise if nTaskToStart had been anything other than '1' or '2' task Three would have been the only one to start. There is no fall through in this switch statement as each case has a break to jump out of the switch once that case is finished.

## while

while(expression){ statements }
The while loop will run the statements between the braces over and over as long as the expression results in a non zero value (true). Once the expression results false, the program will skip beyong the while loop and move on. Since the while loop always checks the condition of the expression first, it is possible that the while loop never runs the statements within the braces.
 ```while(SensorValue(sonarSensor) > 20) // while the Sonar Sensor reads data greater than '20': { motor[rightMotor] = 50; // run 'rightMotor' at power level 50 motor[leftMotor] = 50; // run 'leftMotor' at power level 50 }   motor[rightMotor] = 0; // stop 'rightMotor' motor[leftMotor] = 0; // stop 'leftMotor'```
• In this example, the loop will run the motors forward at a power level of 50 as long as the sonar sensor reads values greater than 20. Once it reaches 20 it will skip the loop, running the code after the structure, stopping the motors. This program would drive your robot forward until it was 20 units away from an object.