Please enable JavaScript to view this site.

The overloaded plus operator ( + ) performs either addition or concatenation, depending on whether the arguments are strings or numbers. How does this work with Mode Independence, which automatically converts between numbers and strings according to the operator and statement context? It depends on the type of the operand to the left of the plus sign. If it is a string expression, then the operation is concatenation and the right-side expression will be converted to string if necessary. Otherwise, the operation is addition and the right-side expression will be converted to a number if necessary. Determining the type of the left operand isn't always so obvious though, since it may be affected by a prior implicit conversion. Assignment Statements, for example, will expect the first operand after the equals sign to be of the type of the destination, and if not, will convert it accordingly, which could then affect how a subsequent + operation is interpreted. The following examples may help clarify this.

10 map1 num,f,6

20 map1 s$,s,10

 

30 print "1" + 2              ! "12" (concatenation)

40 print 1 + "2"              ! "3"  (addition)

 

50 num = "1" + 2              ! num = 3

60 s$ = "1" + 2               ! s$ = "12"

 

70 num = 1 + "2"              ! num = 3

80 s$ = 1 + "2"               ! s$ = "12"

 

The print statement (line 30 and 40) accepts either numeric or string argument expressions and does not coerce them either way, so the compiler decides between concatenation or addition purely on the type of the left operand with the results as shown in the comments above. Assignments, however, expect the first operand to the right of the equals sign to match the type of the destination variable, and will convert it if needed. So in the assignment on line 50, even though the left operand of the + is a string, it gets converted to a number to match the type of the destination variable (num), which in turn forces the + to be treated as addition. On line 60, since the destination s$ is a string, the left operand "1" also remains a string, which forces the + to be treated as concatenation, with the right operand 2 converted to the string "2". Line 80 shows the reverse effect, with the left operand 1 being converted to the string "1" because the destination is a string, and that causes the + to be treated as concatenation.

You can use the functions str$(n) and val(s), which convert numeric expressions to string and vice versa, to coerce this behavior, although that too can be confusing. For example:

100 num = str$(1) + 2           ! num = 3

110 s$ = val("1") + 2           ! s$ = "12"

 

You might have expected the str$(1) in line 100 to force that operand to be a string expression and thus force concatenation, but it gets overridden by the implicit conversion in the assignment statement, i.e. str$(1) effectively becomes val(str$(1)). Conversely, in line 110 val("1") converts the "1" to a numeric 1, but it is overridden by the implicit conversion back to string for the assignment to the string variable s$, which results in the + operator being treated as concatenation.

If you have to do arithmetic or concatenation with a mixture of string and numeric expressions and find yourself unsure, the best approach is to break up your logic into multiple statements—possibly using temporary variables—to avoid the implicit conversions. Or, use the unambiguous addition and concatenation operators and/or casting functions added in A-Shell 6.5.1655.

See Also

Created with Help+Manual 9 and styled with Premium Pack Version 5 © by EC Software