|
|
Variables act as aliases to the values that we want them to represent and they allow us to access and manipulate the
|
|
|
values that we assign to them.
|
|
|
For example we could use the variable \pigVar{name} to represent the value \pigVal{"Brett Langdon"}.
|
|
|
We do this with programming so that we can then access the value \pigVal{"Brett Langdon"} with a shorter representation, \pigVar{name}.
|
|
|
|
|
|
|
|
|
\subsection{Declaration}
|
|
|
|
|
|
To start with variables we need to declare their existence.
|
|
|
By declaring a variable we are saying to the program, here is our alias and here is the value that we want it to represent.
|
|
|
|
|
|
\begin{lstlisting}[caption={Variable Declaration}]
|
|
|
a = 10
|
|
|
print a
|
|
|
\end{lstlisting}
|
|
|
|
|
|
In the above example we are saying that we want to store the integer value \pigVal{10} into the variable \pigVar{a}.
|
|
|
We can then use the variable \pigVar{a} to access the value \pigVal{10}.
|
|
|
This program will output \pigOut{10} rather than \pigOut{a}.
|
|
|
\par
|
|
|
|
|
|
When we declare variables we are telling the programming language to allocate some space in your computers memory
|
|
|
in order to store the value that you need it to.
|
|
|
The amount of space needed to store each variable depends based on your specific language being used and which data
|
|
|
type is being used to store the value.
|
|
|
|
|
|
|
|
|
|
|
|
\subsection{Data Types}
|
|
|
|
|
|
Programming languages support different types of data types or different types of values that they can represent in variables.
|
|
|
Some programming languages use multiple different types of values but most of them support the basic types: string, integer (multiple kinds)
|
|
|
and boolean (true or false).
|
|
|
|
|
|
\begin{lstlisting}[caption={Data Types}]
|
|
|
string = "Brett"
|
|
|
integer = 10
|
|
|
boolean = false
|
|
|
\end{lstlisting}
|
|
|
|
|
|
Please keep in mind that each programming language supports different data types and you should research those types to better
|
|
|
understand variables in that language.
|
|
|
As well some programming languages that are strickly typed which requires us to define the data type of the
|
|
|
variable on declaration (unlike our sudo language).
|
|
|
|
|
|
|
|
|
\subsection{Operators}
|
|
|
Operators are symbols we can use to manipulate variables.
|
|
|
There are a few different types of operators, Arithmetic, Comparison and Logical operators.
|
|
|
A quick list of operators include:
|
|
|
|
|
|
\begin{enumerate}
|
|
|
\item Addition +
|
|
|
\item Subtraction -
|
|
|
\item Multiplication *
|
|
|
\item Division /
|
|
|
\item Equals =
|
|
|
\item Equal To ==
|
|
|
\item Not Equal To !=
|
|
|
\item Less Than <
|
|
|
\item Greater Than >
|
|
|
\item Logical AND \&\&
|
|
|
\end{enumerate}
|
|
|
|
|
|
And there are more!
|
|
|
\par
|
|
|
|
|
|
Arithmetic operators include the mathematical symbols for addition (+), subtractions (-), division(/), multiplication(*) and equals(=).
|
|
|
These operators are used to directly manipulate and change variables.
|
|
|
For example we can add two variables together and then store that sum into a third variable.
|
|
|
|
|
|
\begin{lstlisting}[caption={Addition Operator}]
|
|
|
a = 10
|
|
|
b = 5
|
|
|
c = a + b
|
|
|
|
|
|
print c
|
|
|
\end{lstlisting}
|
|
|
|
|
|
We store the value of \pigVal{10} into the variable \pigVar{a} and the value \pigVal{5} into the variable \pigVar{b}.
|
|
|
Lastly, we store the sum of the variables \pigVar{a} and \pigVar{b} into the variable \pigVar{c}.
|
|
|
The output of this program will be \pigOut{15}.
|
|
|
\par
|
|
|
|
|
|
Comparison operators are used to compare variables against other variables or values.
|
|
|
The output of a comparison operator will be a boolean value (\pigVal{true} or \pigVal{false}).
|
|
|
Some of the popular comparison operators are the equals to (==), not equals to (!=), less than (<), less than equal to (<=), greater than (>) and greater than equal to (>=).
|
|
|
|
|
|
\begin{lstlisting}[caption={Comparison Operators}]
|
|
|
name = ``Brett''
|
|
|
a = 10
|
|
|
b = 5
|
|
|
c = a + b
|
|
|
|
|
|
|
|
|
if name == ``Brett'':
|
|
|
print ``Name is Brett''
|
|
|
|
|
|
if a > 10:
|
|
|
print ``A is greater than 10''
|
|
|
|
|
|
if b >= 5:
|
|
|
print ``B is greater than or equal to 5''
|
|
|
|
|
|
if c != 6:
|
|
|
print ``C is not 6''
|
|
|
\end{lstlisting}
|
|
|
|
|
|
In this example we are showing a few of the comparison operators.
|
|
|
The output of this code will be:\\*
|
|
|
\pigOut{Name is Brett\\*
|
|
|
B is greater than or equal to 5\\*
|
|
|
C is not 6}.
|
|
|
|
|
|
\subsection{Conclusion}
|
|
|
|