Programming If/Then & Forever Statements    

If/Then Statements allow you to write a program that reacts to something occurring.

Now that you have learned how to code Motion for your robot, lets add an If/Then Statement.

Step #1: Write a program for your robot that has it move forward with no time limit. Download it to your robot and test it.



If/Then Statements:

It is time to expand your programming knowledge beyond a line of blocks instructing a robot to move forward or backward.

If/Then & If/Then/Else block commands are excellent commands to use when you want your program react to something that is occurring.


Step #2: Add an If/Then statement to the Motion program you just wrote. Have the robot stop after the Timer counts to 3 seconds. Download it to your robot and test it.

Problem: A computer reads a program in a 'Top Down' or Sequential order. In other words, it starts reading each line of code from the top in a downward progression. But what if it reads the "Move Forward" line of code at .5 of a second and reads the "If/Then" line of code at 1 second. The problem is that the computer will only read the program once and when it eventually reaches "Timer=3" seconds, the computer will not know that it was suppose to "Stop Moving" because it had already read and passed that line of code 2 seconds earlier. The solution is to place your If/Then statements in a Forever Loop or Repeat Loop. That way, the computer will keep reading everthing in the Forever Statement over and over. Now you are guaranteed that when the Timer hits 3 seconds, the computer will notice it and "Then" "Stop Moving".

The Forever & Repeat block commands act as Loops so that the sensing action is endless and as long as the sensor sees the color you are requesting, it will continue to react with motion.

Step #3: Add a Forever statement to the Motion program you just wrote. The robot will now stop after the Timer counts to 3 seconds. Download it to your robot and test it.

Gradesheet: When you have the program working, have your gradesheet signed.