Concept Explanation: Conditional Statements

Printer-friendly version

 

Computer logic is based around the ability of a computer to compare integers. Even the most complex choice can be boiled down to being able to determining if one number is greater than, less than, or equal to another number. This behavior forces us to form our logical instructions in a way that can be answered through evaluating two numbers. The result of this evaluation is a Boolean.

Let's take a look at the conditional statement in our example.

ConditionalStatements

In the example program, the NXT will determine if the robot is situated above a white region of the board or not. To do this, we use a light sensor pointed towards the ground. The light sensor will shine a light and monitor the brightness of the reflection. The sensor translates the brightness level to an integer. A black region would have a low number where a white number would have a high number. This behavior allows us to compare this value to another integer to determine if the NXT is over a dark region.

The light sensor will output a value from 1 to 100. In this instance the light sensor would output a number above 40 for a white area and below 40 for a black area. With this conditional statement, a white region would return a True value and a black region would return a False value.