LessonsBeginner: First stepsLesson 2

Text and numbers

The two kinds of value you use most, how to write them, and how to leave notes in your code.

  • Beginner
  • Lesson 2 of 83
  • About 7 minutes
  • 7 examples

Values

Everything a program works with is a value: a name, a price, a line of a poem, the number of lives left in a game. In xTalk the two kinds you will use most are text and numbers.

Text goes between double quotes, so that xTalk knows exactly where it starts and ends. Programmers call a piece of text a string. Numbers are written as they are, with a point for the decimals and a minus sign for the negatives.

ExampleRun it in the playground
put "I am learning xTalk"
put 42
put 3.75
put -12
put "42"
Output
I am learning xTalk
42
3.75
-12
42

The last line is text that happens to be made of digits. It looks the same as the number 42, and in xTalk it mostly behaves the same too: Everything is text explains why.

Why the quotes matter

Without quotes, xTalk reads a word as a name: the name of a variable, or one of its own words. With quotes, it is just text. Compare these:

ExampleRun it in the playground
put "2 + 2"
put 2 + 2
put "pi"
put pi
Output
2 + 2
4
pi
3.141593

In quotes, 2 + 2 is three characters to show as they are. Without them it is a sum, which xTalk works out. And pi without quotes is one of xTalk's built-in constants, a name for a value it already knows.

Empty, and a quote inside a string

A string can have nothing in it at all. xTalk has a word for that: empty, the same as "". And because a double quote ends a string, a string cannot simply contain one. Instead you join the constant quote to it (the & joins two values; the next lesson is all about it):

ExampleRun it in the playground
put "" is empty
put "She said" && quote & "hello" & quote
put quote & "xTalk" & quote && "has" && the number of chars of "xTalk" && "letters"
Output
true
She said "hello"
"xTalk" has 5 letters

Capital letters

xTalk does not mind how you capitalise its own words: PUT, Put and put are the same command. Most people write them in lower case and keep capitals for the names they make up, like tTotalScore. What is inside quotes is shown exactly as you typed it.

ExampleRun it in the playground
PUT "Shouting works too"
Put 10 * 10
Output
Shouting works too
100

Notes for people: comments

A comment is a note that xTalk ignores, for whoever reads the code next (very often yourself, a month later). It starts with -- or # and runs to the end of the line. A longer note can go between /* and */, over as many lines as you like.

ExampleRun it in the playground
-- Work out the area of a rectangle
put 8 * 5 -- width times height
# a comment can start with # too
/* and this one
   covers several lines */
put "done"
Output
40
done

A line that goes on

A statement normally fits on one line. When one gets too long to read, end the line with a backslash \ and carry on below: xTalk reads the two lines as one.

ExampleRun it in the playground
put "This sentence is rather long, so it is split" && \
      "over two lines of the script"
Output
This sentence is rather long, so it is split over two lines of the script

Try it yourself

Make the Output show these three lines, exactly: the word Hello in double quotes, then the result of 365 times 24, then the text 365 * 24. Add a comment that says what the number means.

Show a solution
ExampleRun it in the playground
put quote & "Hello" & quote
put 365 * 24 -- the hours in a year
put "365 * 24"
Output
"Hello"
8760
365 * 24

Words in this lesson

Each one finds every lesson that uses it. The Dictionary has the whole story of each, with its syntax and more examples.