diff --git a/3 - Functional Programming/1.1 - Variables.tex b/3 - Functional Programming/1.1 - Variables.tex index ae14dcb..f7ef89c 100644 --- a/3 - Functional Programming/1.1 - Variables.tex +++ b/3 - Functional Programming/1.1 - Variables.tex @@ -57,8 +57,8 @@ A quick list of operators include: \item Equals = \item Equal To == \item Not Equal To != -\item Less Than < -\item Greater Than > +\item Less Than $<$ +\item Greater Than $>$ \item Logical AND \&\& \end{enumerate} @@ -83,8 +83,8 @@ 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 (>=). +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'' @@ -92,25 +92,63 @@ a = 10 b = 5 c = a + b +print name == ``Brett'' -if name == ``Brett'': - print ``Name is Brett'' +print a > 10 -if a > 10: - print ``A is greater than 10'' +print b >= 5 -if b >= 5: - print ``B is greater than or equal to 5'' +isNotSix = c != 6 +print isNotSix -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}. +\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{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. diff --git a/3 - Functional Programming/1.2 - Control Statements.tex b/3 - Functional Programming/1.2 - Control Statements.tex index 6501f15..e5b39d8 100644 --- a/3 - Functional Programming/1.2 - Control Statements.tex +++ b/3 - Functional Programming/1.2 - Control Statements.tex @@ -31,8 +31,71 @@ If statements are great and help us execute portions of our code based on the va But what if the condition of the if statement equates to false? With if statements we can append an else statement and a block of code to the end of an if statement that will get called if the if statement is false. +\begin{lstlisting}[caption={If-Else Statement}] +name = ``brett'' + +if name == ``john'': + print ``Name is john'' +else: + print ``Name is not john'' +\end{lstlisting} + +The output of this program will be \pigOut{Name is not john}. +When the program hits the if statement it evaluates the conditional \pigVar{name == ``john''} which evaluates to \pigVal{false}. +Normally the program will continue on its way but since we provided an else statement that gets executed instead. +An If-Else statement allows us to program ``if this then do this, otherwise do this.'' \subsection{Else If Statements} +Ok... wait, we just did If-Else statements not we are doing Else if statements? +Yes, but they are different I swear! +An If-Else statement allows us to execute code regardless of whether a conditional is true or false but with an else if statement we can provide multiple conditionals to an if statements. + +\begin{lstlisting}[caption={Else If Statement}] +name = ``brett'' + +if name == ``john'': + print ``Name is john'' +else if name == ``brett'' + print ``Name is brett'' +\end{lstlisting} + +See? I told you it was different. +So the output of this program is \pigOut{Name is brett} and this is because when the program gets to the if statement and evaluates it as false, it then continues down the list of conditionals. +This works similar to how the else statement before worked, but this time we are giving the if statement multiple conditionals to check. +We can expand this example by adding more else if statements. + +\begin{lstlisting}[caption={Else If Statement 2}] +name = ``brett'' + +if name == ``john'': + print ``Name is john'' +else if name == ``brett'' + print ``Name is brett'' +else if name == ``barbara'': + print ``Name is barbara'' +\end{lstlisting} + +Just like the first example this program will ouput \pigOut{Name is brett}. +This is because when the program gets to \pigVar{name == ``john''} it evaluates to false causing the program to skip to the next conditional \pigVar{name ==''brett''}, which then evaluates to true causing the code block given to execute. +The last conditional \pigVar{name == ``barbara''} will then be skipped and the program will continue past the if statement. +\par + +Now... what is we add an else statement to the end of this? +With an if statement we could append an else statement to the end telling it what to do if the conditional failed. +With an else if statement we can also append an else statement to the end telling it what to do if all of the conditionals fail. + +\begin{lstlisting}[caption={Else If Else Statement}] +name = ``brett'' + +if name == ``john'': + print ``Name is john'' +else if name == ``barbara'': + print ``Name is barbara'' +else: + print ``Well, I'm not sure what your name is'' +\end{lstlisting} + +This program will output \pigOut{Well, I'm not sure what your name is} because both conditionals, \pigVar{name == ``john''} and \pigVar{name == ``barbara''}, evaluate to false causing the if statement to continue on its merry way. \subsection{For Loops} diff --git a/5 - Design Patterns/1.1 - Singleton.tex b/5 - Design Patterns/1.1 - Singleton.tex new file mode 100644 index 0000000..e69de29 diff --git a/5 - Design Patterns/Design Patterns.tex b/5 - Design Patterns/Design Patterns.tex new file mode 100644 index 0000000..e69de29 diff --git a/6 - Data Structures/1.1 - Linked Lists.tex b/6 - Data Structures/1.1 - Linked Lists.tex new file mode 100644 index 0000000..e69de29 diff --git a/6 - Data Structures/1.2 - Doubly Linked Lists.tex b/6 - Data Structures/1.2 - Doubly Linked Lists.tex new file mode 100644 index 0000000..e69de29 diff --git a/6 - Data Structures/1.3 - Stacks.tex b/6 - Data Structures/1.3 - Stacks.tex new file mode 100644 index 0000000..e69de29 diff --git a/6 - Data Structures/1.4 - Queues.tex b/6 - Data Structures/1.4 - Queues.tex new file mode 100644 index 0000000..e69de29 diff --git a/6 - Data Structures/1.5 - Hash Maps.tex b/6 - Data Structures/1.5 - Hash Maps.tex new file mode 100644 index 0000000..e69de29 diff --git a/6 - Data Structures/1.6 - Binary Trees.tex b/6 - Data Structures/1.6 - Binary Trees.tex new file mode 100644 index 0000000..e69de29 diff --git a/6 - Data Structures/1.7 - B-Trees.tex b/6 - Data Structures/1.7 - B-Trees.tex new file mode 100644 index 0000000..e69de29 diff --git a/6 - Data Structures/1.8 - B+ Trees.tex b/6 - Data Structures/1.8 - B+ Trees.tex new file mode 100644 index 0000000..e69de29 diff --git a/6 - Data Structures/Data Structures.tex b/6 - Data Structures/Data Structures.tex new file mode 100644 index 0000000..e69de29 diff --git a/7 - Algorithms/Algorithms.tex b/7 - Algorithms/Algorithms.tex new file mode 100644 index 0000000..e69de29 diff --git a/Programming In General.pdf b/Programming In General.pdf index 0e9528d..9307ffa 100644 Binary files a/Programming In General.pdf and b/Programming In General.pdf differ diff --git a/Programming In General.tex b/Programming In General.tex index f0e53fa..1909edc 100644 --- a/Programming In General.tex +++ b/Programming In General.tex @@ -171,15 +171,51 @@ Brett \textsc{Langdon} \input{"./4 - Object Oriented Programming/1.3 - Polymorphism"} \vfill \pagebreak -\section{Design Patterns} -\input{"./4 - Object Oriented Programming/1.4 - Design Patterns"} +\chapter{Design Patterns} +\input{"./5 - Design Patterns/Design Patterns"} +\vfill +\pagebreak +\section{Singleton} +\input{"./5 - Design Patterns/1.1 - Singleton"} \vfill \pagebreak \chapter{Data Structures} -\input{"./5 - Data Structures/Data Structures"} +\input{"./6 - Data Structures/Data Structures"} +\vfill +\pagebreak +\section{Linked Lists} +\input{"./6 - Data Structures/1.1 - Linked Lists"} +\vfill +\pagebreak +\section{Doubly Linked Lists} +\input{"./6 - Data Structures/1.2 - Doubly Linked Lists"} +\vfill +\pagebreak +\section{Stacks} +\input{"./6 - Data Structures/1.3 - Stacks"} +\vfill +\pagebreak +\section{Queues} +\input{"./6 - Data Structures/1.4 - Queues"} +\vfill +\pagebreak +\section{Hash Maps} +\input{"./6 - Data Structures/1.5 - Hash Maps"} +\vfill +\pagebreak +\section{Binary Trees} +\input{"./6 - Data Structures/1.6 - Binary Trees"} +\vfill +\pagebreak +\section{B-Trees} +\input{"./6 - Data Structures/1.7 - B-Trees"} +\vfill +\pagebreak +\section{B+ Trees} +\input{"./6 - Data Structures/1.8 - B+ Trees"} \vfill \pagebreak \chapter{Algorithms} -\input{"./6 - Algorithms/Algorithms"} +\input{"./7 - Algorithms/Algorithms"} \vfill \pagebreak\end{document}