[prev]Code[next]

Computers don't understand English, so you need to give them instructions using "code." For example, here's code to display the text "Hello, World!"

Example: Hello World

 
1
print("Hello, World!")

Click to run the code.

Run this program by clicking the large "run" button with the green triangle. Throughout this guide, you can run any example program this way. You can also change them to experiment with your own programs, which is the best way to learn programming!

Exercise: Print your name. Change the above program by replacing "Hello, World!" with your name, still in quotes. (Just edit the code in the box above!) Press the "run again" button to see the results.

Text in quotes is called a "string".


You can have the computer run multiple commands in a row using multiple lines. Blank lines are ignored.

Here's a program that will surprise you with how much fruit you may end up eating if you follow the maxim "An apple a day keeps the doctor away."

Example: Apple Calculator

 
1
2
3
4
5
6
7
printp("I eat an apple every day.")
printp("How many apples in my whole life?")

lifeSpan = 76
totalApples = lifeSpan * 365

printp(totalApples)

Click to run the code.

This example introduces a few things at once. It shows how to put numbers directly in your code, do mathematical calculations with them, and print them. It also uses names like lifeSpan to store values like 76, by setting a value for a name using "=". The names are called "variables", and we'll talk more about them later.

Exercise: Increase your lifespan! Try changing the number 76 to something bigger, and run your new program to see that the results make sense.

Exercise: A little more math. In addition to * (times), you can use + (plus), - (minus), / (division), and as many parentheses as you want for grouping. For example, if you only eat apples every other day, replace 365 with (365 / 2), the number of days per year you eat an apple.

What are print and printp?

The print command has a long history of being the basic way to display text, going back to when computers used typewriter-like printers instead of screens! When you use print on AppJet, your text is used to build a web page. Web pages have structure such as sections and paragraphs that you'll learn more about in later lessons.

The printp command is short for "print paragraph", which has the effect of displaying text on its own line, separated from the text around it. Try changing printp to print in the above example, and you'll see that the text can run together when there are no paragraphs involved.

You can probably think of many ways you might want to extend the example above. You might want to change the appearance of the output, allow things like "lifeSpan" to be typed in by a user of your program, or do more complicated calculations (for example, we forgot about leap years!).

We'll teach you how to make the computer do these things in the coming sections, but first we'll take one lesson to talk about programming on the web.

Previous Lesson: Introduction Next Lesson: Web Apps