Skip to content

Exercise 1 of 10 · A complete program

Say Hello with cat()

What you will make

A tiny script that prints two lines of welcome text to the console.

The one new idea: Printing text with cat()

Every R program you write starts with getting output on the screen, and cat() is the plainest, most predictable way to do that — you'll use it in nearly every exercise from here on.

Go straight to the code ↓

The concept

R's cat() function prints whatever you hand it, character for character, with nothing extra added except a single space between separate arguments (which you can turn off with sep = ""). Crucially, cat() never adds a line break on its own. If you want output to end a line and let the next cat() call start fresh, you have to include the newline character \n yourself, inside the string, right where you want the break to happen.

This is different from some other beginner-friendly languages where print automatically starts a new line every time. In R, cat("a") followed by cat("b") prints ab on one line — no newline appears unless you write one.

Worked example

r
cat("== Corner Cafe ==\n")
cat("Open until 9pm.\n")
Output
== Corner Cafe ==
Open until 9pm.

Both calls here end their string with \n, so each one prints its own line and the second message starts cleanly below the first. If the \n were removed from the first call, the two messages would run together as == Corner Cafe ==Open until 9pm. on a single line — the exact issue you'll find in this exercise's starter code.

Your turn

Look at the two cat() calls in the starter code. The second one already ends its string with \n, so it correctly finishes its own line. The first one is missing that \n, which is why its message and the next one end up stuck together. Add \n to the end of the first call's string, right before the closing quote, so the output shows two separate lines.

If something goes wrong

If your output still shows both messages on a single line, double-check that you added \n inside the quotes of the string, not after the closing parenthesis, and that you used a backslash followed by a lowercase n. If you see a stray space appear where you didn't expect one, make sure you edited the existing string rather than adding \n as a brand-new, separate argument to cat().

Write your code

Runs in your browser. Press Run (or Ctrl/Cmd+Enter) and the output is checked for you.

Ctrl/Cmd+Enter to run

Press Esc then Tab to move keyboard focus out of the code editor.

Ready
Output will appear here after you run your code.

The runtime is starting in the background. You can type now — it will be ready before you are.

The answer appears here once you have run your code at least once.

Things that often go wrong here

Adding \n as a separate argument, like cat("Welcome to SkillAIVibe!", "\n").
This does add a newline, but cat() also inserts its default separator (a space) between arguments, so you'd get an extra space before the line break instead of a clean line ending.
Switching to print() to try to fix the spacing.
print() adds an index like [1] in front of the text and wraps strings in quotes, which does not match the plain output this exercise expects.
Assuming the program produced no output for the second line.
Both cat() calls do run — the text is just glued together on a single line because the first call never ends it with \n.

Want a blank editor instead? Open the R playground.