1.1 Hello, World!
Let’s write our very first computer program. Type the following code into the console and press Enter:
cat("Hello, World!")
In the console you should see the following output:
## Hello, World!
That’s it—you just wrote a computer program. A computer program is simply a sequence of instructions that perform a specific task when they are executed (carried out) by the computer. In the above example, there was only one instruction in the sequence: it was the command to reproduce the string “Hello, World” in the console exactly as it is. The name of that command is cat()
.
Let’s try another small program.
Type the following code into the console and press Enter:
cat(2+2)
In the console you should see the following output:
## 4
We have been using the cat()
function so far. Actually you can get output without it. For example, suppose you type:
"Hello, World!"
Then in the console you see:
## [1] "Hello World"
There are quote-marks around the text: that’s not so pretty, but we still get the basic output.
Similarly, you can try:
2 + 2
## [1] 4
Notice that when we don’t use cat()
the output begins with a strange [1]
. In the next chapter we’ll learn why this happens; for now, just ignore it.