The robot counts |
|
Numerical expressions | |
Setting a value to a variable | |
Using variables in a program | |
Summary | |
Variables | |
The data processed by a program are stored in the computer memory. In order to access the data, the programmer associates a variable with the data. |
|
* The variable name is defined by the programmer. This name allows the computer to retrieve the data in its memory. The variable name is like a data address in memory. | |
A variable name may contain letters and digits but no space and must begin with a letter. Its maximum length is 32 characters. It must be different from the robot language keywords. | |
*The variable value is the content of the memory location referenced by the name. * In RobotProg, the variables are defined for the whole program (main program and all subroutines).Their values are integer. All variables have the value 0 when the program execution begins. * Example : in this chapter, we are going to count the number of steps made by the robot. It will be represented by a variable with the name nSteps. |
|
Numerical expressions | |
Numerical expressions are calculus formulas They may contain variables, numbers, parenthesis and operators. The calculus result is an integer. |
|
Setting a value to a variable | |
Setting a value to a variable is done with an assignement instruction like : |
|
|
|
The sign = is used in many programming languages for the assignment instruction. |
|
Using variables in a program | |
Now, you are going to write a program to find haw many steps were done by the robot. |
|
|
|
- Run the program and click the button "Show variables", so that you can watch the variable nSteps value changing when the robot moves. - Try to run the program for different initial positions of the robot : the program will end with different values of nSteps |
|
Build a program moving the robot like in previous program but with a different way of counting the number of steps, using the keyword DistanceFromWall |
|
|