>> Hey friends, we just learned about numbers in C#,
and we're going to go into branches,
that's like if statements to do what they call conditionals.
We we're going to go and do that
inside of the browser like we've been doing.
But you made the suggestion that maybe it's time to graduate.
>> Right. At a certain point,
you want to start using
Visual Studio because even when we are in our browser,
we were getting little errors and squiggles and warnings,
but in Visual Studio,
you're actually going to get even more hints and
code fixes to help you out and it's just a lot more feedback.
So let's talk a little bit about branches.
>> Before we do that real quick, I'm just going
to go to visualstudio.com/free.
This is a URL that lets you get a free version of Visual Studio.
It's totally free.
It's called Visual Studio Community,
and it's got all the power of
the Visual Studio that programmers use,
because you're a programmer as well.
You can download that for Windows or for Mac and enjoy that.
So I've installed that already.
>> Okay.
>> Then I want to point out that here I'm in
the in-browser experience and
I could continue into branches and loops,
which you're certainly welcome to do.
Or if you look in your tree over here,
there's a section that says, work in your local environment.
We're going to do that. So we're here on
this page, learn conditional logic.
We'll make sure we have links to
all of that. So we're going to do this.
We're going to run Visual Studio and follow along,
copying not into an in-browser code environment
but rather copying Visual Studio directly.
>> Right. You can get all of that feedback. Yeah.
>> Okay. So I'll run Visual Studio,
and that's going to come up and I get
a page that's going to look something like this.
Yours will probably be blank because you can see me making up.
>> Right. If it's a completely fresh install.
>> So what should I do?
>> I think we should go ahead and create
a new project and we'll just work out with that.
So we want to choose the.NET Core console app,
I'm not going to go into explaining those terms.
Basically, it's just C#,
console apps are your basic command line applications
that print output and terminal window when you run them.
>> What we've been saying that Console.WriteLine,
it's like that black box that pops up.
So we're going to make it ConsoleApp.
In the future, in other videos,
we'll make web apps and different things like that.
So I want to go and say, new console app
and this is where we will learn about branching.
So already, I'm like what's happening?
>> Yeah. What is all this stuff?
>> Are there going to be videos that will explain
the menus and what's going on in this video?
>> We'll go around and so we've Console.WriteLine,
that's the only part of this that we are
familiar with from past videos.
Everything above it, the static means, string array,
args, class program and all that,
we can get into in a different video.
>> So we will talk about that, but for now,
pretend that we live only in between these two mustaches
>> Yeah. Curly braces,
or mustaches. Whatever you want to call them.
>> Do you know mustaches is not inclusive.
>> It is not.
>> I don't know.
>> I think in our curly brace.
>> Touche. So here we've got
our console app and what I'm going to do is hit
"Play" but what I like to do
is start without debugging and this is just a hotkey I remember.
This is only hotkey I'm going to ask everyone to remember.
I'm going to hit "Ctrl+F5".
This is nice because this lets me run my application.
So this is doing the exact same thing as hitting that Run button.
>> Yeah and this black box in the output window.
>> This console.
>> This is what we saw.
>> Yeah. Certainly if you don't want to do that,
you can just hit this green button.
The "Run" button. So Console.WriteLine. Hello world.
Again, Ctrl+F5. Cool it worked.
Now, how do we do branches and loops?
Well, this is going to be a tutorial and what
I could do if I want it to is,
so I could pick it up and I could snap it off to the side here,
and then I could have Visual Studio on the other side.
We could try this and see if this
works out and you can try the same thing.
So I'm walking through the tutorial,
and you'll see how I can just move the splitter over
just a little bit so I can see what's going on here.
So if statements let you make decisions.
The tutorial will tell you to go and
create directories and run things at
the command line but since we're using
Visual Studio and we just made a new console app,
we don't need to do that stuff.
We're just going to put our code right here.
>> Right. So let's then put our first if statement.
>> Okay.
>> So it looks like we declared a few variables that we can then
use as part of the conditions inside of the if statement.
>> So I'm going to hit "Copy" and then just as before,
I'm going to hit "Paste".
Hey that's cool, Visual Studio actually put it in
a nice location and it's all got coloring,
if is colored a certain color.
This looks really nice. It's pleasant looking here.
Will it work right off the bat?
>> Let's try it.
>> Okay. So I'm going to hit "Ctrl+F5" again,
which is again, the hotkey for hitting the "Play" button.
The answer is greater than 10.
Let's let's put that next to this here.
>> What exactly were the conditions
we were using in that if statement?
>> Well, we took A and B and we added them.
A plus B is 11.
>> So 11 is greater than 10 so it
did execute the Console.WriteLine,
that was within that if statement branch.
>> Okay. If I change that and I make it 5 plus 3.
>> So that would be less than 10.
It didn't find anything.
>> It didn't do anything. It just said and then told me
some things about how the application exited.
So that shows us that that if statement in fact, worked.
So in this case,
the sum was less than 10.
>> So if the conditions weren't met,
it doesn't actually execute the Console.WriteLine.
That's within the if statement.
>> Right. We had talked about Booleans before.
Remember when we said, does this string
contain this or does this string start with that?
Is that a Boolean?
>> Yes. So any if statement will need to return true or false as
the ultimate condition to decide whether or not it will
execute any of the code within that if statement block.
>> Right. So I could go and say,
bool_something equals that thing,
and I have to say semicolon at the end there,
and I can say, if something.
>> Right.
>> So that worked.
>> Like independently declaring
the Boolean outside of the if statement.
>> Lets see if it works. It does work.
So whether I put it inside here or outside there,
the fact that it's true or not.
It doesn't matter. Now I'm going to hit "Ctrl+Z" which is undo.
So you look and I just hit "Undo" and
there's a button here for "Undo" as well.
So that let me know that that actually worked.
So what about the else?
Remember when we said it was
smaller than 10 nothing really happened, it just stopped.
>> Right. So else would be if there's any condition
that we should do when the if statement condition isn't met.
So basically, instead of just not writing anything,
we could put in an else statement to say,
hey, this answer is not greater than 10.
>> Okay. So if the sum is greater than 10,
else do the other thing.
Cool. Let's try that with a small number.
So here it's going to be eight.
Again, I'm going to hit "Ctrl+F5" or I can hit the "Play" button,.
>> The answer is not greater than 10.
So it executed anything that was in that else block.
Since the if statement wasn't met.
>> Right. It says here any key to close
this window and just hit "Space" or any key.
This is interesting, I was actually going to ask
you this Kendra because I was a little confused.
Does it matter that this is indented,
can this be over here or over there?
What if I have other lines?
>> Yeah. So in this case,
if you ever want to have multi-line if statements,
you need to add curly braces.
As far as the whitespace trailing before and after if statements
that means indentation or extra newline characters and stuff.
I believe an if statement will execute
the single statement after it if there's
no curly braces. Lets try it though.
>> Visual Studio smarter about this,
if you get confused,
you can just say, "Format
Document" and Visual Studio will make it better.
>> Cool.
>> But yeah. Let's try that. Actually let's put that on one line,
let's put that on one line, that's weird.
That's not usually how you do things,
but I bet you that it'll work. It does.
The right thing to do,
I'm hearing you say is when all else
fails and there's a question just like order of operations,
always add an extra parenthesis,
it never hurts to add those braces and it's cool.
Look, Visual Studio is actually lighting them up.
So if I click on one of them,
its friend turns color.
>> It's highlighted a little bit.
That's another one of the benefits that working in
an actual Integrated Development Environment
over browser can provide.
So your brace matching.
>> Yeah. That's a good reason
for us to go and upgrade right there,
just one of the many reasons.
Let's try a complicated if statement.
>> Yeah. So this looks like it will
have multiple conditions inside of it.
>> So I'm just going to copy it and paste it,
and by the way,
I just noticed that when there was nothing there,
this turned gray saying they're not really used.
>> Well, it's a sign of it. It's never used.
>> Visual Studio is always
thinking about ways to help you do stuff.
>> Right. So that's basically saying that those variables weren't
necessary because they were
declared but you never actually use them in your code.
>> Now, this if statement seems scary and complicated,
but I bet you that we can break it down.
I think it's okay if I add some whitespace to make it simpler,
and by making it simpler,
it's more just for my own emotional well-being.
So that if statement that I
copied from that page it was a little bit intimidating,
so I broke it up into multiple lines just so
I could get a sense of what was going on.
So this one says,
if I add these together and they're greater
than 10 and that's either true or false,
and then this one said I assume if A equals B,
what's going on here though.
>> Right. So let's say And operator,
which basically just combines the two different conditions.
So the And operator means both sides of the statements on
either side of it needs to be true in order
for this if statement block to execute.
>> So if true and true.
If true and true so true plus true equals true.
>> Right.
>> That make sense. If I put that back all in one line,
well again, it might be a little intimidating.
You'll see a lot of these and get used to them.
So here we can go and run that,
and it'll say, yes the number is not greater than 10,
and for or the first number interesting or well,
here I've got some different code,
I want to say, not
and either thing can be true. Then that would be the case.
>> Here just one side needs to be true.
>> Okay. So if either one of those is true,
so in this case here,
either the answer is greater than 10,
which it is, or
the first number is equal to the second, which it is not.
So that makes sense. That is true and that is false.
All right. So let's move on.
>> Gets really long over there.
>> It acts intimidating.
I'm feeling more and more
uncomfortable about all this other stuff going on here.
>> Yeah let's take a minute to break down everything while
the envelope of all C# programs in the next video,
so we just covered branches.