Navigation

    CreatiCode Scratch Forum

    • Register
    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • CreatiCode

    Operators for Comparing Values

    2D Blocks
    1
    1
    230
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • info-creaticode
      CreatiCode last edited by admin

       

      Prerequisite

      • Working with Blocks

       

      Introduction

      In the “Operators” group, there are 6 boolean blocks (>, <, ≠, =, ≥, ≤) for comparing between numbers or text strings. For example, you can compare between 3 and 5, or between “apple” and “orange”. The comparison result will be true or false.

       
       

      Comparing Numbers

      You can use the 6 comparison blocks to compare 2 numbers, the same way how you compare numbers in Math.

      For example, when you run the block “10 > 5”, it will return “true”. However, if you ask if “5 > 5” or “5 > 10”, you will get a result of “false”.

      For a reminder, the ≥ or ≤ blocks will return “true” when the 2 numbers are the same. For example:

       
       

      Comparing Single Letters

      If you are comparing single letters, such as “a” vs “b”, the computer will compare their positions in the list of a to z. For example, “a” is at position 1 in the list, and “b” is at position 2. Since 1 is less than 2, we say “a is less than b”. Obviously, the letter "z’ will be greater than any other letter. Here are some more examples:

       
      Note that it does not matter if a letter is in upper or lower case. All letters are converted to lowercase before being compared. For example:

       

      Comparing Text of One or More Letters

      When there is more than one letter, we need to compare the 2 sequences of letters by each letter from left to right.

      For example, if we are comparing “cake” vs “cap”:

      • Position 1: “c” vs “c”, we get a tie, so we move on to position 2.
      • Position 2: “a” vs “a”, we get another tie, so we move on to position 3.
      • Position 3: “k” vs “p”. Since k comes earlier in the a to z list, we know “k” is smaller than “p”. Therefore, we can stop here and declare “cake” is less than “cap”.

      Here are some more examples:

      1 Reply Last reply Reply Quote 0
      • First post
        Last post