Quantcast
Channel: Small Basic
Viewing all articles
Browse latest Browse all 669

Small Basic Featured Article: Automatic Type Conversion

$
0
0

You can find the latest and greatest version of this article here:

Small Basic: Automatic Type Conversion

  

Overview

You’ve probably seen how to write expressions with both integers and decimal numbers, and you also probably know how to glue (concatenate) two strings together using the plus sign. But when an expression includes both numbers and strings, things can get a little tricky! In this section, we’ll look at examples that show you how Small Basic converts types so that they're compatible.
 

Examples of Converting Type

This first example might be familiar. Let’s say someone asked you what the output was from this statement:

TextWindow.WriteLine( "Chapter" + 3 ) 'Result: Chapter3

  

You’d probably say that the number 3 gets converted to a string that gets appended to the string "Chapter" to make the new string "Chapter3". Of course, you’re right (and generally awesome)! Now, what about this next statement?

TextWindow.WriteLine( "5" + "6" ) 'Result: 11

 

Based on what you’ve learned so far, you might answer "56", because the plus sign will just glue the two strings ("5" and "6") together. Well, that’s not how Small Basic rolls! If you run this program, you’ll see the number 11 displayed in the output window! So, what’s going on?

When Small Basic sees the plus sign, it immediately assumes that you want to add numbers. It then looks at the two operands on either side of the plus sign and asks itself, “Do both these operands look like numbers?” If the answer’s yes, it’ll convert the two operands to numbers and then add them together! But if any one of the two operands doesn’t look like a number, then Small Basic interprets that operand as a string and the plus sign as a concatenation operator (for example, “fifty” + “6” would give you “fifty6”).
 

Examples of Converting Strings to Numbers

Table 1 shows a couple of examples that should make this clear. In all these examples, Small Basic interprets the operands to the left and right of the arithmetic operator (+, -, *, or /) as numbers and then does the math to evaluate the results. In Example 2, the white spaces around the numbers don’t stop Small Basic from recognizing that these operands are still numbers!

Table 1: Examples of automatic string-to-number conversion

Example

Statement

Output

1

TextWindow.WriteLine( "5.0" * "-2" )

-10.0

2

TextWindow.WriteLine( " 5.0 " * " -2.0" )

-10.00

3

TextWindow.WriteLine( "-5.0" * -2 )

10.0

4

TextWindow.WriteLine( "10" / "5"  )

2

5

TextWindow.WriteLine( -3 - "-5" )

2

 

Examples of Concatenating Strings

Table 2 shows some examples where one or both operands aren’t interpreted as numbers. In these cases, Small Basic thinks the plus sign means concatenation. The outcome of these examples is in the right column.

Table 2: Examples of string concatenation

Example

Statement

Output

1

TextWindow.WriteLine( 5 + "2-" )

52-

2

TextWindow.WriteLine( "5.0.0" + 2 )

5.0.02

3

TextWindow.WriteLine( 5.0 + "2+2" )

52+2

4

TextWindow.WriteLine( "a12" + "34" )

a1234

5

TextWindow.WriteLine( 2 + 2 + "ab" )

4ab

6

TextWindow.WriteLine( "ab" + 2 + 2 )

ab22

7

TextWindow.WriteLine( "ab" + (2 + 2) )

ab4

 

Now here’s the Small Basic play-by-play:

[1] The string "2-" isn’t a number. In math, you’ll write -2 to mean the negative of 2.

[2] The string "5.0.0" isn’t a number. A decimal number has only one decimal point.

[3] The string "2+2" isn’t a number. It includes a plus sign, which isn’t a digit and isn’t a decimal point. And Small Basic drops the zero when it outputs "5.0”.

[4] The string "a12" isn’t a number. It includes a character, a, which isn’t a digit.

[5] Small Basic starts evaluating this expression from the left. The first operation it sees is 2+2, which is a legal addition operation. So it evaluates this as 4. Then it sees: 4 + "ab". Because "ab" isn’t a number, Small Basic interprets the plus sign as a concatenation operator and gives you "4ab".

[6] Again, Small Basic starts at the left to find "ab" + 2 and gives you "ab2" because it can’t convert "ab" to a number. Next, it needs to evaluate "ab2" + 2, which again is taken to mean concatenation, resulting in "ab22".

[7] Small Basic calculates what’s in the parentheses first. It evaluates (2 + 2) to get 4. Now, it sees: "ab" + 4, which gives you "ab4".

  

Using String Operands

As we already mentioned, the plus sign’s the only arithmetic operator that applies to strings. If you write an arithmetic expression that includes –, *, or / with some strings as operands, then Small Basic tries to convert these strings to numbers. If it can’t convert the strings to numbers, then it won’t give you an error message. Instead, it silently sets the number to 0 and evaluates the expression. The examples in Table 3 show you what we mean.

Table 3: Using string operands with -, *, and /

Example

Statement

Output

1

TextWindow.WriteLine( 5 - "ab" )

5

2

TextWindow.WriteLine( "3+" * 2 )

0

3

TextWindow.WriteLine( "10-" / 3 )

0

 

Let’s explain the outputs from these examples:

[1] The minus sign only applies to numbers. Since "ab" isn’t a number, it’s converted to 0. The result is 5 - 0, which is 5.

[2] The multiplication sign only applies to numbers. Since "3+" isn’t a number, it’s converted to 0. The result is 0 * 2, which is 0.

[3] The division sign only applies to numbers. Since "10-" isn’t a number, it’s converted to 0. The result is 0 / 3, which is 0.

  

Conclusion

We’re showing you these examples to explain how Small Basic works. But we’ll never use quotation marks around numbers, and we recommend that you don’t either.

Quotation marks are usually only used to represent strings. The only time you should ignore this advice is if you have scientific notation (we’ll explain that later).

 

Credits

This article was originally written by Majed Marji and Ed Price.
  

Additional Resources

See Also

   
   
Find the latest and greatest version of this article here:

Small Basic: Automatic Type Conversion

   

Have a Small and Basic day!

   - Ninja Ed


Viewing all articles
Browse latest Browse all 669

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>