So you want to learn how to write computer programs? Where do you start? What language do you learn first? In this article, I’m going to suggest that you start with an interpreted language that has a shell program (also called a REPL) you can use to practice with.
What is a Shell Program?
A shell program is a program that allows you to type in programming statements and receive the output from those commands immediately. Shell programs are also called REPLs, with stands for Read-Evaluate-Print-Loop. In other words, a REPL is a program that reads programming statements, evaluates (executes) them, prints their output, and then loops back so you can enter another statement.
Languages such as Python and JavaScript have shell programs you can use to practice writing programs. This is because Python and JavaScript are interpreted languages (go here for more information on interpreted programming languages) and it is common to implement a shell program for an interpreted language. Languages such as C++ and Java, which are compiled languages (go here for a definition of compiled programming languages), don’t have REPLs that come with the language but there are separate REPL programs you can buy. However, I suggest you start your programming education with either Python or JavaScript because interpreted programming languages are generally easier to learn than compiled languages, so let’s see how you can get REPLs for those two languages.
As I began my programming education many years ago during the dark first PC ages, just about every PC came with a BASIC interpreter, some of which had the interpreter installed in ROM. You just boot up the computer and you’re immediately in the shell program. I still remember how easy it was to sit down at the computer with my textbook and just start entering BASIC statements to see what would happen. That’s how learning programming should be for everybody.
Python and JavaScript Shell Programs
When you download the Python language you automatically get a shell program. To use it, simply open a command prompt window and type ‘python’. You will see a little message and then the Python shell program prompt will appear:
>>>
The Python shell is now waiting for you to enter Python statements for it to evaluate. I’ll show you how to work with the Python shell later in this article.
Getting a REPL for JavaScript is a little harder. A very popular choice in the JavaScript world is a program called node.js. Node is a system for creating JavaScript applications but when you download Node, you also get a JavaScript REPL. Go to Node’s home page and download Node. Once it is installed, you can start the shell program by typing ‘node’ at a command prompt. The program loads and you see a prompt like this:
>
Now you can start entering JavaScript statements and have the shell program interpret them.
Another option for JavaScript is the SpiderMonkey shell program. This shell program is developed by Mozilla (Firefox) and it also interprets JavaScript code but has a few different features than Node. You can find the SpiderMonkey shell program here. After you download the right shell program for your computing environment, you can open the shell program by typing ‘js’. The shell prompt appears like this:
js>
And you’re ready to start entering JavaScript statements.
A third way to use a JavaScript interpreter is to open a web browser and find the developer tools option for that browser. There will be an interpreter of some type located there you can use to execute JavaScript statements. Here is a screen shot of the JavaScript interpreter in use with Firefox:
Example Shell Interactions
Here are some screen shots that show some examples of interacting with the Spidermonkey, Node, and Python shell programs, in that order.
As you can see from these examples, interacting with the shell is easy and very intuitive. Just enter the statements you want to execute and you can immediately see the result in the shell.
Writing Shell Scripts
The JavaScript and Python shell programs also let you write what are called shell scripts. A shell script is a program that is written in a text file and then given to the program interpreter to run. Once you’ve mastered a language using the shell program, you’ll want to switch to shell scripts for writing more complex programs.
Here is an example of writing a shell script for Spidermonkey. I work in a Windows environment so the first thing I do is open Notepad (no judgement, please) and type in my program. I have to make sure when I save the file to save it with a .js extension, as required by the shell. Here is a screen shot of the text file:
The next step is to execute the script from the command line. Here is a screen shot:
I can execute the same program as-is with Node, except I need to change the print function to console.log since Node doesn’t use the print function. Here is the screen shot:
I can do the very same thing with Python. First, create a Python program in a text editor and be sure to save it with a .py extension. If you are copying my program, you’ll need to make one or two changes, but I’ll leave those as an exercise for you, as any good instructor would do. Then call the shell program with the name of the script file. Here is a screen shot:
Online Interpreter Environments
There are several online JavaScript and Python interpreters you can use. Just do a search for web-based JavaScript or Python interpreters and you will be shown several sites you can use for exploring these languages online. One of my favorite sites that hosts many different languages is repl.it. I encourage you to explore their site and play around with the many interpreters found there. This site also have online compilers you can use to learn compiled languages such as C++ and Java without having to download and install a compiler and/or an IDE to write, compile, and run programs.
Why Start with an Interpreter?
Learning an interpreted programming language for your first programming language has several advantages over learning a compiled language first:
- An interpreter allows you to enter a statement and see the results immediately. A compiler forces you to go through the edit-compile-build-run cycle every time you want to evaluate a program.
- Interpreted languages have a more user-friendly syntax than do compiled languages. For example, Python doesn’t require curly braces for designating blocks of code (it uses indention) and Python statements don’t have to end with a semicolon.
- The ease of entering statements and seeing their results encourages exploration because there isn’t such a long program development cycle (see reason 1 above).
- Some programming concepts are easier to demonstrate first using an interpreter. For example, when I am teaching my students that a literal evaluates to itself, it’s hard to demonstrate this with a compiled language. With an interpreter, a learner can simply type a literal value into the shell and see that the shell returns just that value. Another example is order of operations. A learner can modify the order of operations in an arithmetic statement and see immediate changes without going through the compiling cycle.
Learning Programming with an Interpreter
If you are just starting out learning to program, pick a language that has a shell program you can use. The best way to learn programming is to explore what the language can do with short, simple programs, building your way up to harder, more complex programs. You can do this best with a shell program. If you are learning C++ or Java, look for one of the shell programs I mentioned earlier in this article.
The most important thing to do is just start. Start writing programs and start right now.