So so far, we've learned how to write a basic go program consisting of one file in a package, Maine,
and we have that main function.
So we've also learned how to to create basic functions that we can call from Maine, how to store variables
and how to create our own types of variables with structures and types.
And we've learned about maps and slices.
One of the things that you need to know, and we're going to do it right now is how to make decisions
in a program, Engo in particular.
So the conventional method for making a decision is the IF statement, and it's really easy.
If you've worked in any other programming languages, you'll have no problem with it whatsoever.
And the syntax is simple.
If some condition, then we're going to do something, otherwise do something else or don't do anything
at all.
So let's just give it a try.
Well, first of all, create a variable and we're going to have a variable, which I'll call is true.
And I'm going to give that a shorthand syntax and I'm going to say true.
This is a type of bool which we haven't actually seen yet.
So let's do the other way of looking at it.
Just so you're familiar with the type var is true is a bool, which is a boolean, which means it can
only hold two possible values, true or false, and will say is true, is equal to trip.
And then we'll do a simple if statement.
If is true then in print then we start our curly brackets log print line.
True is true is followed by the value of is true.
Is true.
ELT's loved print line is true, is is true, same thing, and that's all we're going to do.
So if you think about this, it's a very, very simple program.
I've created a variable called Is True and I've assigned it the value of true.
And then I use go to say to to test that value if is true, which is saying exactly the same thing as
saying if is true is equal to true.
And what I say is the double equals sign here because that's a comparison.
It is true is equal to true print this otherwise print that lets run it.
Go run maeng go.
And what do you think is going to happen.
Pretty simple is true.
Is true.
Which is a really weird sentence when you think about it.
Let's try that again.
The variable named is true has the value of true.
Therefore what's here on line ten.
We'll get executed and what's here on line twelve will not get executed.
In other words, it should print out to the terminal.
Is true.
And it does and if we come up here and change is true to false, then it should print this letter instead.
So let's clear the screen and run it.
And it should print is true, is false, and it does.
So that's a basic if statement, if statements can become more complex than that, but not difficult
at all.
So let's try something else.
Let's create a new variable called cat is assign the value of cat and let's do it if statement if cat
is equal to cat log dot.
I've got to get inside the brackets there.
Logged print line cat is is Cat Elz, and again, inside those curly brackets, log gridline cat is
not cat again very simple will run it because Cat is assigned the value of cat.
That first one should actually get printed.
And it's given me a warning saying cat is always true.
But I know that because I'm just trying to demonstrate something here it was a cat is a cat and if I
say Cat two instead of cat it should put the second one.
Cat is not cat.
OK, let's try it with numbers.
My name
is equal to or is assigned the value of I'll use.
The shorthand is assign the value of one hundred and this time I'll do a second variable as well.
Is true is assigned the value of thoughts.
What if I want to test two conditions which is what I'm trying to demonstrate here.
If my name is Tomago greater than ninety nine and I'm going to use a double ampersand to say I want
to have this condition and another condition is true.
Then log, print line.
My name is greater than 99 and is true, is set to true.
Now what's going to happen if I run this program first?
I've been passing in two conditions and both of these have to evaluate to true in order for this to
take place.
So the first condition is the number of the variable stored in myname has to be greater than 99.
And the variable is true, has to be set to true.
Is are both of those conditions true?
Well, is 100 greater than 99.
Yes.
Is is true.
True.
No, it is set to false because one of these conditions is false.
This will never execute.
So if I ran this program, nothing should happen.
Let's clear the screen, run the program and nothing happens.
Exactly.
Now, if I change this, if I say and is true, is equal to false, I could do it like this is true
is equal to false.
And this time of year in the program, it will run and it will print it out, which is exactly what
I wanted.
But there's a shorthand.
So instead of saying is true, is equal to true, I can put an exclamation mark in front of it which
says not so if my name is greater than 99 and is true, is not true or not, is true, in other words,
is to is false.
This should run and it does.
And it prints out the line exactly as I expected.
And if I wanted to, I could put at else condition here, just as I did before.
And in fact I can go else ELT's ELT's, I put something in here.
So else if my number is less than one hundred and not and is true, which is the same thing as saying
and is true is true.
OK, and here I could have a third condition if myname
is exactly equal to 101 or is true.
Elusive, my name is greater than 1000 and is true, is equal to faults or not is true.
I can have all of those and I have an area.
One is this is truly mine, a much greater thousand and is true equal to faults, evaluative, but not
used.
Right.
I got to put something in there so I could just put log print lines and I'll just put really simple
things in there.
One
to three.
OK else if they're so now I can change these values and depending on what I change them to, one of
these conditions might execute.
So you can have as many Elsas as you want and in a statement, but honestly, you probably never want
to have more than one or two eltis.
I only ever go with one in the vast majority of cases.
Usually I test a condition and if it evaluates to true, I want to do something.
Otherwise I want to do something else.
If I have something that's more complex than that, then than that.
And I have more than two things, have two situations or two cases I ever want to handle, I chances
are I'm not going to use an if statement.
I'm going to use another decision structure which is which exists specifically for this kind of situation.
So I'm going to delete everything out of this.
And I clear the screen down here and we'll go to another decision structure called the switch statement.
And the switch statement is really easy.
So you simply say switch it on some value, which I'll call my var, which I haven't created yet, but
I will in a minute switch on my var and then you have cases and you have a case and some value.
So let's create my var.
So this makes sense.
My bar is assigned the value of cat and I'm going to switch on my var inside of those, these brackets
I'm going to have key statements.
So the first one is to say if the if my var has the value of cat do something.
So I put case followed by the value I'm looking for or the condition I'm testing for, followed by a
corn and then do something like the print line cat is set to cat and I better use lower case here.
Not confused myself then I can have another case.
Which has to come here, of course, another case, case dog and then do something, so I'll copy this
and paste this in here.
Cat is such a dog that I might have a third one, which I copy the whole thing and pass it down and
put it in the right spot again.
And fish cat is set to fish.
Now, I have an error somewhere.
What is it?
The error is I get an extra quote hidden down below the scroll here, extra closing bracket.
All right, so now I have a switch statement.
So I have a variable called cat called my Vah, which is set to cat.
And then I switch on it and I switch on the variable, my version.
And I simply say, if that variable happens to equal cat do this, if it happens to equal dog, do this,
if it happens to equal fish, do this and then I can have a special case default, which is to say,
if none of the above conditions are true, do this log print line and keep putting these in the wrong
spot for some reason.
Default.
Logged print online cat is something else, and I saw what I have to do.
So let's think about what's going to happen here.
I've established this variable.
I created this variable called my version of assign the value of Cat to it.
Then I switch and I handle a bunch of cases and I can have as many cases as I want.
Check this valuable mirror and compare it against all of these cases.
So one is cat is cat equal to cat?
Yes, it is.
Therefore, this will happen.
Now, in other programming languages that implement the switch statement, you actually will keep going.
You keep testing to see do you meet any of these other conditions.
So if the condition was case cat or and then case some other condition that matches it, it would keep
going.
And if it matched three of the cases, it would do all three of those actions.
Go does not do that go breaks out as soon as it matches one case.
And that's something you have to remember.
So if I run this program, it should print out cat is set to cat.
Let's try it.
Go run Mingo Cat said the cat.
If I said Cat two dog and run the program, it should print out the case that matches dog and cat,
said the dog.
If I said it to horse, it doesn't match any of these three cases.
What's going to happen?
Well, it's going to execute this default case.
So let's run that program and see what happens.
Cat is set to something else.
So those are your two basic decision structures.
The IF statement, which you use a great deal of the time, and the switch statement which you use periodically,
particularly when you have to make a decision based upon a lot of different cases.
There is a third one, but we're not going to get to that for a little while.
So bear in mind that the two main ones we've covered right now, the if statement and the switch statement
and the third one will get to a little bit later on.
That's enough for this time around.