Variables
A Variable is a portion of computer memory for storing a data value.
It is created by assigning a value or an expression to it.
Assigning a value to a variable is done with the assignment operator: $\lt$–
An equal sign, = is used in some cases; however the assignment operator is highly recommended.
It is also highly recommended that:
(A.) The variable is on the LHS (Left Hand Side) of the assignment operator.
(B.) The value or the expression is on the RHS (Right Hand Side) of the assignment operator.
For example, we declare a variable in R this way:
variable <– value
OR
variable <– expression
Variable names:
(1.) Can be single letters (please avoid unless if used in loops)
(2.) (a.) Can contain a combination of letters, digits, and underscores but must not begin
with a digit.
(b.) It may begin with a letter or a period (not an underscore).
(c.) If it starts with a period, it cannot be followed by a digit.
(d.) Also, it should not contain two consecutive underscores.
Be it as it may:
(a.) Please avoid underscores and decimals
(b.) Use a combination of letters and digits such as:
num1 to represent the first number
variable1 to represent the first number
(3.) Can contain only letters but must not be any of the keywords or system-defined functions.
It is highly recommended to avoid using any keywords as an identifier in any R program.
The keywords are:
if, else, repeat, while, function, for, next, break, TRUE, FALSE, NULL, Inf, NaN, NA, NA_integer_, NA_real_,
NA_complex_, NA_character_,
The functions are:
(a.) The letters can be Camel case (similar to the hump of a camel) such as:
firstVariable...the V is the hump
In Camel casing letters: two letters are merged as one; the first letter of the second word is
an uppercase letter while all other letters are lower case letters.
In firstVariable, V is uppercase; all other letters are lower case
(b.) The letters can be Pascal case such as FirstVariable
In Pascal casing, two letters are merged as one; the first letter of the first word and the
first letter of the second word are uppercase letters while all other letters are lower case letters.
In FirstVariable, F and V are uppercase; all other letters are lowercase.
(c.) uppercase letters (please avoid...it denotes someone who is yelling)
(d.) lowercase letters (okay, but try to avoid)
Student: What if you have three words?
How do you write it in Camel case? Pascal case?
Teacher: Say we want to write the variable, first arithmetic sequence
Camel case: firstArithmeticSequence...A and S are the only uppercase
Pascal case: FirstArithmeticSequence...F, A, S are the only uppercase
In this course, for all applicable variables; please use:
(i.) A combination of letters and digits such as num1 OR
(ii.) Camel case letters such as firstNumber OR
(iii.) Pascal case letters such as FirstNumber
***We shall use single characters when we write conditions for Iteration statements/Loops
because it is easier to use single characters in such cases. We shall see examples when we discuss loops.
However, feel free not to use it if you wish.***
(4.) Cannot contain whitespaces.
A whitespace is a horizontal or vertical space.
For example: myNum has no whitespace; but my Num has a whitespace.
Variable names cannot contain whitespaces.
Hence: myNum is acceptable, but my Num is not acceptable.
It is also highly recommended to avoid special characters in C# variable names.
Special characters are all the non-numeric and non-alphabet characters on your keyboard such
as ~ (tilde), !(exclamation), @ (asperand), # (hash), $ (dollar), % (percent), ^ (caret), & (ampersand), *
(asterisk).
For all course work, please avoid special characters in variable names.
(5.) Are case sensitive.
These are all different variables:
firstresult (lowercase),
firstResult (Camel case),
FirstResult (Pascal case),
Firstresult,
FIRSTRESULT (uppercase),
first_result (snake case)
strFirstResult (Hungarian notation)
Student: Hungarian notation?
Teacher: Hungarian notation is a naming convention that follows the camel case naming but in which
the name is preceded by the data type represented by a three-character ID.
str means string
Similarly, int is for integer; dec is for decimal;
dbl is for double; bln is for Boolean among others.