A language agnostic book on programming.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

194 lines
8.1 KiB

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}), which means they can be used in a conditional statement or even stored into a variable.
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
print name == ``Brett''
print a > 10
print b >= 5
isNotSix = c != 6
print isNotSix
\end{lstlisting}
In this example we are showing a few of the comparison operators.
The output of this code will be:\\*
\pigOut{False\\*
True\\*
True}.
\par
Logical operators are used to evaluate multiple variables, comparisons or conditionals.
The two most popularly used Logical operators are the Logical AND (\&\&) and the Logical OR ($\vert$$\vert$).
An example of using Logical operators would be to construct a conditional statement based on multiple inputs.
\begin{lstlisting}[caption={Logical AND Example}]
name = ``Brett''
age = 22
print name == ``Brett'' && age == 22
\end{lstlisting}
The output of this code will be \pigOut{True}.
With the Logical AND operator we are saying that our conditional statement is only true if both \pigVar{name} is equal to \pigVal{``Brett''} AND \pigVar{age} is equal to \pigVal{22}.
If either side of the operator (\&\&) is false then the entire conditional is false.
\par
The other Logical operator is the OR operator and tells our conditional that one side or the other must be true in order for the entire conditional to be true.
This means that either the left or right side can be false as long as the other side is true.
\begin{lstlisting}[caption={Logical OR Example}]
name = ``Brett''
age = 23
print name == ``Brett'' || age == 22
\end{lstlisting}
This program will output \pigOut{True} because eventhough \pigVar{age} is not \pigVal{22}, \pigVar{name} is \pigVal{``Brett''}.
If we switched around the conditionals, put \pigVar{age == 22} on the left hand side of \pigVar{||}, then the program will have the same output; this is becuase with the Logical OR only one side needs to be true.
\par
One thing to remember when dealing with either of the Logical operators is that programming languages will evaluate the conditionals from left to right.
For example, in the above ``Logical AND'' example, if \pigVar{name} were not equal to \pigVal{``Brett''} then the right hand side \pigVar{age == 22} would never even get to evaluate.
For the second example using the Logical OR, if \pigVar{name == ``Brett''} were true (which in this case it was) then the right hand side would never get to evaluate.
This is becuase with Logical OR's only one side needs to be true, if the left hand side is true then there is no need to even try the right hand side.
\subsection{Increment/Decrement Operators}
Most programming languages give us a few operators to use to increment or decrement the value of number variables.
There exists four operators for this purpose, increment by \pigVal(1) (++), decrement by \pigVal(1) ($-$$ -$), increment by (+=) and decrement by ($-$=).
\begin{lstlisting}[caption={Increment Operators}]
num = 5
++num
num += 10
print num
\end{lstlisting}
The output of this code will be \pigOut{16}.
The first line sets \pigVar{num} to \pigVal{5}.
The second then increments \pigVar{num} to \pigVal{6}.
The third line then increments \pigVar{num} by \pigVal{10}.
Lastly we print the value of \pigVar{num} which at this point is \pigVal{16}.
\begin{lstlisting}[caption={Decrement Operators}]
num = 16
num--
num -= 10
print num
\end{lstlisting}
This program does the complete opposite of the one above.
It starts with \pigVar{num} at \pigVal{16} and then decrements it to \pigVal{15} and finally to \pigVal{5}.
There is one main difference in this program; the decrement operator ($-$$-$) is placed to the right of \pigVar{num} where with the increment operator (++) above
is appears to the left of \pigVar{num}.
These operators can be placed on either side of the variables you wish to effect.
\par
When the operator is in front of the variable it is called pre-increment or pre-decrement and the latter is post-increment and post-decrement.
This pre/post choice does not apply to the increment by (+=) and decrement by ($-$=) operators; they must be placed to the right of the variables you wish to effect.
It is worth while to investigate how your language of choice handles pre/post operators as you may not get the desired result.
For the remainder of this resource only the pre operators will be used.
\subsection{Conclusion}
In this section we have covered the basics of variables, how they are declared, used and evaluated.
Variables will be the building blocks from which we will continue through this book.
It is very important that you are able to use variables in your prefered language as their use is exhausted in every following section and chapter.
\par
The next section will cover the use of Control Statements.