Lesson goal: Comparing strings

Previous: The length of a string | Home | Next: Comparing strings (2)

When working with strings, you might want to ask true/false questions about them, and use results of these questions in a if statement. These questions often will involving checking if two strings are equal to one another, or if one may come before another (alphabetically). To do this, you can use the same operators as for numbers, which are:
  • == to test for equality
  • ~= to test for being not-equal
  • < to test if if a string comes before another alphabetically
  • > to test if if a string comes after another alphabetically

Now you try. Run this code and type in word when asked. Try words that come before and after computer alphabetically. Also try typing in computer exactly.

Type your code here:

See your results here:

This code will work, but try to add this functionality: if the word typed is not equal to "computer" (i.e. ~=), display the text "your word is not computer." Dismiss.
Show a friend, family member, or teacher what you've done!

Here is a share link to your code:

Does your code work? Want to run it on your iPhone?

Here's your code:

  1. Use [Control]-[C] (Windows) or [⌘]-[C] (MacOS) to copy your code.

  2. Paste it using [Control]-[V] (Windows) or [⌘]-[V] (MacOS) into this page

  3. Then click the "Use on iPhone" button that you'll see.