LessonsBeginner: First stepsLesson 9

Comparisons and logic

Every way to compare values in xTalk, from = and < to contains and is among, and how to combine conditions with and, or and not.

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

Comparing values

Each comparison answers true or false. Most have a symbol and a word you can use instead, whichever reads better.

WriteOrTrue when
=isthe two are the same
<>is notthey differ
< >less than, greater than
<= >=at most, at least
ExampleRun it in the playground
put 5 > 3
put 5 >= 5
put 5 <> 3
put 5 is not 3
put 5 = 5.0
put "cat" is "dog"
Output
true
true
true
true
true
false

Comparing text

Text is compared in alphabetical order, and xTalk does not mind capital letters unless you ask it to, with the property caseSensitive.

ExampleRun it in the playground
put "ABC" is "abc"
put "apple" < "banana"
set the caseSensitive to true
put "ABC" is "abc"
Output
true
true
false

Is it in there?

xTalk has comparisons for text inside text that read like English. These are some of its most used words.

ExampleRun it in the playground
put "Hello world" contains "world"
put "world" is in "Hello world"
put "Hello world" begins with "Hell"
put "report.pdf" ends with ".pdf"
put "z" is not in "abc"
Output
true
true
true
true
true

is among is stricter: it looks for a whole word, item or line, not just some characters.

ExampleRun it in the playground
put "b" is among the items of "a,b,c"
put "bc" is among the items of "a,b,c"
put "bc" is in "a,b,c"
put "fox" is among the words of "the quick brown fox"
put "Fri" is not among the items of "Sat,Sun"
Output
true
false
false
true
true

And, or, not

Combine conditions with and (both must be true), or (at least one) and not (the opposite). Brackets make your meaning plain.

ExampleRun it in the playground
put 7 into tAge
put tAge >= 5 and tAge <= 12
put tAge < 5 or tAge > 65
put not (tAge = 7)
Output
true
false
false

xTalk stops as soon as it knows the answer: if the left side of an and is false, the right side is never worked out. That lets you guard a test that would otherwise fail:

ExampleRun it in the playground
put 0 into tCount
if tCount > 0 and 100 / tCount > 10 then
   put "Big share"
else
   put "Nothing to share"
end if
Output
Nothing to share

Try it yourself

A year is a leap year when it divides by 4, unless it divides by 100, except that one that divides by 400 is a leap year after all. Write one expression that puts whether tYear is a leap year, and try it with 1900 and 2000.

Show a solution
ExampleRun it in the playground
put 1900 into tYear
put (tYear mod 4 is 0 and tYear mod 100 is not 0) or tYear mod 400 is 0
put 2000 into tYear
put (tYear mod 4 is 0 and tYear mod 100 is not 0) or tYear mod 400 is 0
Output
false
true

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.