Sequential Languages

In this lab, you will learn about other programming languages and how you can learn them by building on your skills with Snap! programming.

On this page, you will learn about sequential programming languages and see that you can already kind of read them.

A programming language is characterized by two things: a philosophy of programming and details of notation. Most modern programming languages are multi-paradigm but basically sequential. This includes Snap!, even though you have learned some composition (nesting) of functions (known as functional programming) and some ideas about sprites as objects (object-oriented programming).

You learned about reporters and commands on Unit 1 Lab 2 Page 4: Making Your Own Block and about predicates on Unit 2 Lab 1 Page 2: Checking the Player's Guess.

Multi-paradigm languages take ideas from several different approaches to programming style. Here are a few approaches you have seen in Snap!:

It would be nice to add some images showing short examples of each programming type. --MF 12/17/24

Learning another language of the same type is much easier than learning your first one. For example, here’s the binary search function from Lab 1 of this unit:
binary search for (value) in (data) {
            script variables (low) (high) (current index) (current item)
            set (low) to (1)
            set (high) to (length of (data))
            repeat until ((low) > (high)) {
                        set (current index) to (average of (low) and (high)) #comment: find the middle word
                        set (current item) to (item (current index) of (data))
                        if ((current item) = (value)) { report (true) }
                        else {
                                    if ((current item) > (value)) #comment: eliminate half the list {
                                                set (high) to ((current index) – (1))
                                    } else { set (low) to ((current index) + (1)) }
                        }
            }
            report (false)
}

  1. Talk with Your Partner Review the Snap! binary search algorithm shown above, and discuss how it works so that you can compare it with the same algorithm written in other languages.

Python

Here's the same procedure written in Python:

def binary_search(value, data):
    low = 0
    high = len(data)-1
    while (low <= high):
        current_index = average(low, high)    # find the middle word
        current_item = data[current_index]
        if (current_item == value):
            return True
        elif (current_item > value):          # eliminate half the list
            high = current_index-1
        else:
            low = current_index+1
    return False
  1. Talk with Your Partner Read the Python version of the binary search function can compare it to the Snap! code. Discuss the similarities and differences.
For the most part, this is line-for-line the same as the Snap! version, apart from tiny details such as
return
instead of report. There are also several more significant differences in notation.
  1. Read more about some of the differences when using Python.
    • Python uses makes all variables local automatically. In Python, there's nothing like the script variables block. Variables are automatically local (usable only within their procedures) and will only be global (usable anywhere in the program) if you use a
      global
      or
      nonlocal
      statement. Why does Python do this?
      Automatically making variables all local encourages programmers to use global variables only when really necessary. You learned why global variables are sometimes necessary but otherwise best avoided on page Unit 2 Lab 1 Page 4: Keeping Score with Global Variables. (If you need a review, you learned about the script variables block and local variables on Unit 2 Lab 1 Page 1: Starting a Number Guessing Game.)
    • repeat () {
} Python uses indentation to organize code structure. Snap! uses C-shaped blocks (like the repeat block shown right) to control which instructions are part of a loop or conditional; Python uses indentation instead. See a comparison.
      Snap! gives a visual indicator of which instructions are and aren't part of a loop or conditional. The C-shaped blocks vividly clarifies the extent of the loop or conditional:
      if (current_item > value):
high = current_index-1
      Python uses indentation (how many spaces at the beginning of each line) for the same purpose:
      if (current_item > value):
          high = current_index-1
      Python is unique among popular languages in making the amount of spacing significant in this way.
    • Python list numbering starts at 0 not 1. In Python, as in many text-based languages, the first item of a list is item number 0, not item 1. So for example, item 5 is actually the sixth item.
    • Python uses parentheses instead of nested reporters. Snap! lets you nest blocks inside each other to tell the computer the order in which you want to perform operations. In text-based languages like Python, you must use parentheses if your expression doesn't follow order of operations. See an example.
      • The expression (3*4)+5 could be written as
        3*4+5
        because of order of operations.
      • But 3*(4+5) would need to be written as
        3*(4+5)
        in Python.
    • Python doesn't allow spaces in variable names. So you can't have a variable like current item. Instead, Python programmers often use underscore characters ( _ ) inside variable names (such as in
      current_item
      ).
    • Python uses one equals sign to set a variable and two equals signs as a predicate. Like many text-based languages, in Python:
      • A single equals sign sets the value of a variable, such as
        x = 3
        instead of  set () to ().
      • Two equals signs checks for equality:
        x == 3
        instead of (x) = (3).
      Why does Snap! do it differently?
      The notation of using an equals sign to set a variable is the cause of many bugs, even among experienced programmers. Consider trying to read code that says
      x = x+1
      instead of set (x) to (x + 1) or change (x) by (1).

      Why does Python use = like this?
      This design choice is inherited indirectly from Fortran, a programming language invented back in the 1950s when computers could display only 64 different characters, so there were fewer options available.

      How can I make this easier for myself?
      If you find yourself programming in a language that uses
      =
      for assigning a variable, you can train yourself to pronounce
      =
      as "gets" or "becomes" as in "x gets 3" or "x becomes 3" rather than as "x equals 3."
Can we please offer them a super quick taste of Python programming in a little windowlet as a FTYD? Then maybe also we could recommend a easy-to-setup compiler as an ITIT or TIF for those kids who want to explore further. Same comment for JS. Any other FYTD exercises you can think of for this lab would be great for breaking up the page and increasing engagement. Maybe something like "read this python and answer a multiple choice question about it" (a.k.a., "Look! You really can read some python/JS already!"). --MF, 12/20/21

Umm sure go for it. -bh 2/18/22
This needs updating, and Mary and Michael need to fix the python CSS. --MF 09/02/25
You've seen Python code throughout this course. Click for some examples.

JavaScript

Here's the same binary search procedure written in JavaScript:

function binarySearch(value, data) {
    var low, high, currentIndex, currentItem;
    low = 0;
    high = len(data)-1;
    while (low <= high) {
        currentIndex = average(low, high);    // find the middle word
        currentItem = data[currentIndex];
        if (currentItem == value) {
            return true;
        } else if (currentItem > value) {     // eliminate half the list
            high = currentIndex-1;
        } else {
            low = currentIndex+1;
        }
    }
    return false;
}

This doesn't look so different from the Python version, but there are a few significant differences.

  1. Talk with Your Partner Read the JavaScript version of the binary search function can compare it to the Python code and the Snap! code. Discuss the similarities and differences.
  1. Read more about some of the differences when using JavaScript.
    • JavaScript creates local variables differently. Inside a procedure, JavaScript's
      var
      statement is equivalent to script variables (a). There are better ways to create local variables in JavaScript, but they look different from Snap!.
    • JavaScript uses braces to organize code structure. Braces
      { }
      mark the extent of what would be a C-shaped block in Snap!. (Although JavaScript programmers do generally indent code more or less the same way Python programmers do, in JavaScript the indenting doesn't matter to the computer but is just to make it more readable to people.)

      I cut all of this because it's confusing. There are semicolons at the end of every line above. --MF 9/2/25

      Within the braces, statements are separated by semicolons (
      ;
      ). (In JavaScript, semicolons go between statements:
      {statement; statement; statement}
      , but in the C programming language, which is otherwise quite similar, the semicolons go after statements:
      {statement; statement; statement;}
      .

      CUT FROM BEFORE:
      This is the sort of thing that drives students of text languages crazy. It's one of the advantages of block-based languages such as Snap!.

    • JavaScript calls everything functions (commands and reporters). JavaScript uses the name "function" to describe all procedures, including what Snap! calls "commands," the ones that don't report a value.
    • JavaScript also doesn't allow spaces in variable names. Just as in Python, you can't have a space inside a variable name in JavaScript. Instead, JavaScript programmers often use "camel case," so called because the capital letters in the middle of a variable name are supposed to look like the humps of a camel. For example,
      currentItem
      instead of current item.
    • JavaScript also uses one equals sign to set a variable and two or three equals signs as a predicate. JavaScript, like Python, uses the = notation instead of  set () to (). But instead of using
      ==
      to test whether two values are equal, JavaScript has two ways to do that:
      ==
      , which is like (x) = (3), and
      ===
      , which is like is (x) identical to (3)?.

Learning Another Sequential Language

There are things to learn about notation (syntax, which is the text-language equivalent of block colors and shapes). But you will be able to learn the details of one of these other languages much more quickly now because you've learned Snap!. You already know how to program in a sequential language!

The syntax of a language is the notation it uses (punctuation, words reserved for specific purposes, etc.). Human languages have syntax, too: nouns vs. verbs, commas vs. semicolons, and so on.

The semantics of a language refers to the meaning or purpose of each piece of syntax.

I'm not sure why this was on the page. Semantics is not mentioned anywhere else and an irrelevant vocab term feels like an odd way to wrap up. --MF, 12/20/21
On this page, you learned about sequential programming languages and saw how you can already somewhat read two other sequential programming languages (Python and JavaScript) even though you haven't learned their syntax.