Bird
Raised Fist0
R Programmingprogramming~5 mins

First R program in R Programming - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - First R program
Start R script
↓
Run cat("Hello, world!")
↓
Display output on console
↓
End program
The program starts, runs the cat command to show text, then ends.
Execution Sample
R Programming
cat("Hello, world!")
This code prints the text Hello, world! to the console.
Execution Table
StepActionCode EvaluatedOutput
1Execute cat functioncat("Hello, world!")Hello, world!
2Program endsNo more codeNo output
💡 No more code to run, program ends.
Variable Tracker
VariableStartAfter Step 1Final
No variablesN/AN/AN/A
Key Moments - 2 Insights
Why do we use cat() in R to show text?
cat() tells R to display the text on the screen cleanly, as shown in step 1 of the execution_table.
Does the program store the text in a variable?
No, this simple program just prints text directly without storing it, so variable_tracker shows no variables.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is printed at step 1?
AHello, world!
Bcat("Hello, world!")
CNo output
DProgram ends
💡 Hint
Check the Output column in execution_table row 1.
At which step does the program finish running?
AStep 1
BBefore Step 1
CStep 2
DNever ends
💡 Hint
Look at the Action column in execution_table row 2.
If we remove cat(), what happens to the output?
ANo output is shown
BText still prints
CError occurs
DProgram runs twice
💡 Hint
R evaluates string literals and prints them (with [1] formatting), so text still prints.
Concept Snapshot
cat("text")
- Shows text on the console
- Runs code line by line
- No variables needed
- Program ends after last line
Full Transcript
This simple R program uses cat() to show the text Hello, world! on the screen. The program starts, runs the cat command, then ends. There are no variables used. The output appears on the console after running cat().

Practice

(1/5)
1. What does the print() function do in R?
easy
A. It saves the text to a file.
B. It displays the text or value inside the parentheses on the screen.
C. It creates a new variable.
D. It deletes existing data.

Solution

  1. Step 1: Understand the purpose of print()

    The print() function is used to show output on the screen in R.
  2. Step 2: Compare options

    Only It displays the text or value inside the parentheses on the screen. correctly describes that print() displays text or values.
  3. Final Answer:

    It displays the text or value inside the parentheses on the screen. -> Option B
  4. Quick Check:

    print() shows output = It displays the text or value inside the parentheses on the screen. [OK]
Hint: Remember: print() shows output on screen [OK]
Common Mistakes:
  • Thinking print() saves to a file
  • Confusing print() with variable creation
  • Assuming print() deletes data
2. Which of the following is the correct syntax to print the message "Hello, R!" in R?
easy
A. print("Hello, R!")
B. print(Hello, R!)
C. print('Hello, R!)
D. print["Hello, R!"]

Solution

  1. Step 1: Check the use of quotes

    Text messages must be inside matching quotes. print("Hello, R!") uses double quotes correctly.
  2. Step 2: Verify function syntax

    print() requires parentheses, not square brackets. print("Hello, R!") uses parentheses correctly.
  3. Final Answer:

    print("Hello, R!") -> Option A
  4. Quick Check:

    Quotes + parentheses correct = print("Hello, R!") [OK]
Hint: Use matching quotes inside print() parentheses [OK]
Common Mistakes:
  • Missing quotes around text
  • Mismatched or missing closing quotes
  • Using square brackets instead of parentheses
3. What will be the output of this R code?
print("Welcome to R programming")
medium
A. [1] "Welcome to R programming"
B. Welcome to R programming
C. Error: unexpected string
D. NULL

Solution

  1. Step 1: Understand print() output format in R

    R prints output with an index [1] before the text when printing a character string.
  2. Step 2: Match output with options

    [1] "Welcome to R programming" shows the correct R output format including the index.
  3. Final Answer:

    [1] "Welcome to R programming" -> Option A
  4. Quick Check:

    print() output includes [1] index = [1] "Welcome to R programming" [OK]
Hint: R shows [1] before printed strings [OK]
Common Mistakes:
  • Ignoring the [1] index in output
  • Expecting plain text without index
  • Confusing output with error messages
4. Identify the error in this R code:
print('Hello R)
medium
A. Missing closing parenthesis
B. print() cannot print text
C. Mismatched quotes around text
D. Missing comma inside print

Solution

  1. Step 1: Check quotes in the code

    The code starts with a single quote but does not close it properly, causing mismatched quotes.
  2. Step 2: Verify parentheses

    The closing parenthesis is present, so the error is not missing parenthesis.
  3. Final Answer:

    Mismatched quotes around text -> Option C
  4. Quick Check:

    Quotes must match exactly = Mismatched quotes around text [OK]
Hint: Always close quotes properly inside print() [OK]
Common Mistakes:
  • Ignoring missing quote errors
  • Assuming parentheses are missing
  • Thinking print() needs commas inside
5. You want to write a first R program that prints your name and age on separate lines. Which code will do this correctly?
hard
A. print("Name: Alice\nAge: 25")
B. print("Name: Alice")\nprint("Age: 25")
C. print("Name: Alice", "Age: 25")
D. print("Name: Alice") print("Age: 25")

Solution

  1. Step 1: Understand how to print multiple lines in R

    Using two separate print() calls prints on separate lines. The newline character \n inside a single print() prints \n literally, not a new line.
  2. Step 2: Check code correctness

    print("Name: Alice") print("Age: 25") correctly uses two print() calls on separate lines with proper syntax.
  3. Final Answer:

    print("Name: Alice") print("Age: 25") -> Option D
  4. Quick Check:

    Separate print() calls for separate lines = print("Name: Alice") print("Age: 25") [OK]
Hint: Use separate print() calls for multiple lines [OK]
Common Mistakes:
  • Using \n inside print() without escape
  • Writing \n outside quotes causing syntax error
  • Trying to print multiple lines in one print() without proper escape
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard