Simple Processing: Equals operator

The first operator is the equals symbol, =. As a mathematical symbol it is used for assignment. In other words, you use the equals symbol to set the value of a variable. The value you set to the variable can be a simple value or an expression. The simpler assignment is shown below, and the expression version can be found in some of the later examples. The code shown below can also be found here.
OpenConsole()
name$ = "John"
height.f = 186.6
weight.f = 80
age.w = 23
copy_height.l = height
PrintN(name$)
PrintN(StrF(height))
PrintN(Str(copy_height))
PrintN(Str(height))
PrintN(Str(age))
Input()
CloseConsole()
End
The first four lines (after the OpenConsole() command) set the values of the variables (on the left hand side of the = operator) using strings and numbers typed directly into the code. You can only set the values of variables, so it must always only be a variable name on the left hand side of the = operator when you are using it for assignment. Note that you must store strings in string variables and numbers in numeric variables or the compiler will give you an error.

The next line shows how to set the value of a variable using another variable - it is just the same except you use a variable name on the right hand side to use as the value. PureBasic will automatically convert between different numeric types automatically (copy_height is a long and height is a float).

Equals example output The next five lines (starting with PrintN) show you what the values of the variable are, and an example of commands you can use to display values. Obviously these values should be the same as the ones we set in the code. The output from the program is shown in the screengrab on the right and the lines of code are explained below.

First of all, remember that the PrintN() command takes a string as a parameter and prints it to the console. The first print line displays the name$ variable. Since this is a string type, we can put the variable name directly as the parameter and it will be displayed. As you can see, "John " is printed to the console, so this is as expected.

The next line - PrintN(StrF(height)) - displays the height variable. Since numeric variables (in this case a float) are not strings we must convert it to a string before printing it. The command to convert float numbers to strings is StrF, which takes one parameter - the floating point number to convert. This is the reason for putting height (the name of the float variable) in the brackets. The StrF() command returns a string. You will notice that each command (PrintN and StrF) has its own set of brackets, and that the StrF command is inside the PrintN brackets. This means that the parameter for the PrintN command is the result of the StrF() command which, as described above, is a string.

As you can see, the value displayed is "186.60006" which does not seem correct - after all, we set the value of height to 186.6. The reason for this is that is a limitation of the float number format and affects all computers and languages which use this number format (not just PureBasic). Computers which work in 1's and 0's cannot always store values which have decimal fractions with exact precision. However, there is some good news. To get around this you can shorten the amount of digits that get displayed - this will be demonstrated in a later chapter. The other piece of good news is that PureBasic will have a "double" type added for extra precision (roughly twice as accurate) in the future.

The next line is PrintN(Str(copy_height)). This displays the value of the copy_height variable. This is another numeric variable so we must convert it before displaying it. This time the type of the variable is one which whole numbers are stored so to convert this we must use the Str command, which is the same as StrF except that the parameter in this case is a long. The value displayed in the console is "187", which you may also think is strange. However, remember that PureBasic will automatically convert between types when you assign values to numeric variables. When converting float types to any of the types for storing whole numbers the behaviour is to round to the nearest number.

The line PrintN(Str(height)) shows what happens if you use the wrong conversion command for a variable of a certain type (in this case a float is being converted using the command for whole numbers). What happens in this case is that PureBasic will try to convert the float into the type to fill the parameter. As mentioned above, the parameter to the Str command is a long type, so the effect is the same as the previous line of code.