Syntax/elements of R program-comments, keywords


A program in R is made up of three things: Variables, Comments, and Keywords. Variables are used to store the data, Comments are used to improve code readability, and Keywords are reserved words that hold a specific meaning to the compiler.

Variables in R

variables which like any other programming language are the name given to reserved memory locations that can store any type of data.

In R, the assignment can be denoted in three ways:
= (Simple Assignment)
<- (Leftward Assignment)
-> (Rightward Assignment)

x=2
y<-3
4->z
print(x)
print(y)
print(z)

Comments in R

Comments are generic English sentences, mostly written in a program to explain what it does or what a piece of code is supposed to do. More specifically, information that programmer should be concerned with and it has nothing to do with the logic of the code. They are completely ignored by the compiler and are thus never reflected on to the input.

The question arises here that how will the compiler know whether the given statement is a comment or not?The answer is pretty simple. All languages use a symbol to denote a comment and this symbol when encountered by the compiler helps it to differentiate between a comment and statement.

Comments are generally used for the following purposes:
Code Readability
Explanation of the code or Metadata of the project
Prevent execution of code
To include resources

Types of Comments
There are generally three types of comments supported by languages, namely-

Single-line Comments- Comment that only needs one line
Multi-line Comments- Comment that requires more than one line.
Documentation Comments- Comments that are drafted usually for a quick documentation look-up


Note: R doesn’t support Multi-line and Documentation comments. It only supports single-line comments drafted by a ‘#’ symbol.

Comments are a way to improve your code’s readability and are only meant for the user so the interpreter ignores it. Only single-line comments are available in R but we can also use multiline comments by using a simple trick which is shown below. Single line comments can be written by using # at the beginning of the statement.

#this is a single line comment
if(FALSE)
{
  "this is a multi line 
  comment test"
}

Keywords in R

Keywords are the words reserved by a program because they have a special meaning thus a keyword can’t be used as a variable name, function name, etc.

We can view these keywords by using either help(reserved) or ?reserved.


  • if, else, repeat, while, function, for, in, next and break are used for control-flow statements and declaring user-defined functions.
  • The ones left are used as constants like TRUE/FALSE are used as boolean constants.
  • NaN defines Not a Number value and NULL are used to define an Undefined value.
  • Inf is used for Infinity values.
Note: R is a case sensitive language so TRUE is not same as True.

Comments

Popular posts from this blog

Programming in R - Dr Binu V P

Introduction

R Data Types