Worked on if-else and else is sections of chapter 3, bumped data structures and algorithms up a chapter and made design patterns its own chapter, mapped out data structures chapter
@ -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 ($>$=).
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.
+ 63- 0
3 - Functional Programming/1.2 - Control Statements.texView File
@ -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.