Hello WISCONSIN!!

First off, I wanted to apologize for my lack of posts over the past few weeks, I have had a lot going on at work and I haven’t really had much in the way of cool things about which to post. I really could have written this any time, but well, inspiration doesn’t just come at you, right? (That is so the opposite of rawtruff.com ((Remind me sometime to tell you THAT story)) ((Wait, how are you going to re… whatever I digress)) but the point here is that I was lazy alright? Why do you always make me admit my flaws!?!? WHY !?!?)

So now that we have that out of the way, let’s move onto what you really came here to read about. Conditional logic! (Seriously though, if you came here to read about that you are a NERD! What the hell NERD!?) So, as in the past lets start with the google definition of conditional logic:

A conditional statement, symbolized by p q, is an if-then statement in which p is a hypothesis and q is a conclusion. The logical connector in a conditional statement is denoted by the symbol . The conditional is defined to be true unless a true hypothesis leads to a false conclusion.

Apparently conditional logic is what I call it, but the rest of the googles say it’s conditional statements. Whatever, it’s the same damn thing. Now that we’ve defined it we are good, right? Because that definition totally makes perfect sense and needs no further explanation. (I’m lying again, man I do that a lot in these posts.)

So what is the REAL definition of conditional (statements) logic? Basically it is when we turn something that isn’t a mathematical equation into a one. The most common type is If-Then (As stated in the amazing definition above.) Let’s give some examples, that will help more.

Say for instance we wanted to try to see if we have three apples in our pile of apples.
ApplePile

(Ahh google, without you I wouldn’t have terrible pictures for my examples… Good job google good job.)

So first we would run some sweet cmdlet in PowerShell like Count-Apples (This cmdlet really needs to exist, I might just make it after this post. I’ll edit this post with the code if I do.)

So we run this cmdlet, something like this:

Count-Apple

Now, we see that our cmdlet says this picture has eight apples pictured. It could be cool and say something like “This picture has 8 apples” but it would be returned as [string], and that is much harder to work with, so instead we’ll just get 8 back and it will be [int]. Now we want to write some code, some real code that is capable of doing something with the information we get! That is where our If-Then statements come in.

But before that, we CAN work with the cmdlet as is, but it’s sloppy and makes for ugly code. (And I REALLY REALLY REALLY hate ugly code… PUT YOUR DAMN INDENTATIONS IN THERE!!!) So let’s fall back on our old friend the Variable! Our code will look something like this:

$apples = Count-Apple -Picture .\ApplePile.jpg

Now if we return $apple we get type [int]! This is the bread and butter of our conditional (statement) logic!

count-applevariable

So far we’ve made it about a page and a half without using a single If-Then statement! WOOT I am still far too wordy for my own good. (I was afraid I’d have lost my touch since I haven’t written a post in a while.) Let’s get to it.

Now that we have our [int] we can ask a question that the computer can understand. I suppose this is as good of a place as any to explain some things about computers that may or may not have been adequately explained by this blog. (Hint! It wasn’t, I just don’t want to have to edit some other posts where it probably SHOULD have been explained.)

Computer’s don’t think like people. Like, at all. It makes a whole lot more sense to call it conditional statements over ‘logic’ because computers don’t really do logic in the traditional sense.

So how does a computer think? Well a processor has a whole lot of functions, but if we break it down to it’s most basic, it can only process either a 1 or a 0. This is due to the nature of electricity. Circuits can only either be ON or OFF, if they are ON it is viewed as a ‘1’, if they are OFF it is viewed as a ‘0’. There is another type of question that we DO understand as humans. (Especially anyone raised in the developed world where we had to take grade school tests.) That logic is the glorious “TRUE” or “FALSE”! (Yeah, you remember those tests, the ones where you could not study at all and still have a 50/50 shot of getting a good grade?? Except of course that isn’t how statistics work and if guessed on all of them you had a much lower chance of getting a passing grade, but this blog isn’t about statistics, if you are interested in those, I would suggest algebra.com. It has some great proofs on explaining THAT whole thing.)

So computers are only capable of thinking in this binary process. Whether you call it ON/OFF, 1/0, or TRUE/FALSE doesn’t really matter, they all basically mean the same thing, and that thing leads us into the great conditional (statement) logic! We will ask the computer if our statement is TRUE or FALSE, and depending on the response we get we will have it do something for us. BACK TO THE PILE! (Of apples I mean.)

We have our code above that let’s [int]$apples = 8. Let’s ask the computer a question about it! (As an aside, you have to ask the computer in conditional statements because the computer does not understand “is” as a question. So this question won’t look like a ‘question’ but I assure you it is.)

$apples = Count-Apple -Picture .\ApplePile.jpg
if($apples -eq 3)
    {
    return $true
    }

else
    {
    return $false
    }

So what this code has just done is this:

If [int]$apples is equal to 3, please return TRUE for us. If [int]$apples is equal to ANYTHING else please return FALSE. (Else is put at the end of question statements so that you can have a catch all. Basically it means that: If no other IF is true, do this.)

So now we have some conditional (statement) logic! There are several other types of conditional (statements) logics out there, but those will have to be saved for the next post as this has already broken the thousand word mark. I promise though it won’t be several weeks before the follow up post with this one. I want to make sure that I finish this series of beginner stuff soon. As always, if this was helpful, feel free to leave a comment, and if you thought it was totally stupid and I should just quit writing, then man, you are harsh and maybe you should come up with some more constructive feedback, and probably see a counselor, I mean you don’t need to carry all that hate around with you man… Seriously.

One thought on “PowerShell Basics, CONDITIONAL LOGIC WOO! (Part 1)”
  1. Your style is so unique in comparison to other folks I’ve read stuff from.
    I appreciate you for posting when you’ve got the opportunity, Guess I will just bookmark this blog.

Leave a Reply

Your email address will not be published. Required fields are marked *