Buckys C++ Programming Tutorials - 17 - if / else Statement

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what is going on guys welcome back to your 17th C++ tutorial and in this tutorial I'm going to be covering something called the if-else statement now let's go ahead and first I want to build a basic if statement because an FL statement is pretty much like an enhanced if statement so let's go ahead and use the knowledge or the information that we learned in the last tutorial and build on it so let's go ahead and make a simple program we have a variable called age and go ahead and set it equal to whatever your ages now go ahead and make a basic if statement now if you remember from the left story all an if statement does is it runs a simple test if that test is true it runs a bit of code if that test is false it doesn't do anything so let's go ahead and test age say if age is greater than 60 let's go ahead and say um C out Wow you are old so if the user is over 60 let's go ahead and say you are old and now let's go ahead and end that line run this program and see what we get we get nothing and that is because I'm 24 so this program does nothing but you build this program you show it to your boss and you're like hey where's my raise in there like wow this program is impressive in all however wouldn't it be nice if we could give a message to the user no matter how old they are what if instead of skipping this if the test is false let's give them all an alternative message like if anyone's over 60 we're going to tell them they're old if anyone is under 60 we're going to tell them hey get a job so saying alright um I suppose I could put another if statement and then Ray age is less than 60 but you know what that isn't the best way to do things I'm going to show you guys something called the if-else statement so the if statement basically ran a simple test and if that test is true it ran this bit of code and if it was false it did nothing it skipped it but now we want to say all right else if the test is false then we want to run this bit of code so either way it's to run a bit of code either way if it's true it's going to run this bit of code let me just go ahead and copy that if it's false it's going to run this bit of code but let's not put while you're old for either one if they're under 60 let's go ahead and write you are young get a job so now when we go ahead and run our program unlike before when it says our right since this test isn't true I'm just going to skip it it says all right this tests is it true so what am I supposed to do it's false well I'm supposed to do the else so let's go ahead and nothing be talking run it and it says you are young get a job and of course if the user is like 87 we run the program and they say wow you are old so now you can see instead of just having a basic if statement where it was either run a bit of code or do nothing we now have the option of something happens for true and something happens for false so now either way your computer program is going to do something so that is the basics of an if-else statement so now that you guys know that let me go even deeper I said that you can go ahead and run a line of code inside the if statement well you can also run multiple lines of code just by putting them under each other so if we go ahead and we copy this we can go ahead and paste it paste it paste it it doesn't matter you can go ahead and run it four times as long as it's in between those braces right there server saying all right what if I wanted to do something like this what if not only I wanted to check if they were over 60 but if they were over 60 I wanted to check if they were over a hundred so let's go ahead and do that so what I want to want to teach you guys now is that inside if statements instead of just running simple lines of code you can also put additional if statements this is called nesting of statements so let's go ahead and say if and remember this code is only going to run if the user is over 60 so now we want to test if the user is over 100 and then we're going to write C out armed why are you still alive I know this is harsh but hey just trying to get the point across so now let's go ahead and go around this bit of code right now so our program is going to say this alright if the age is over 60 which it is I'm going to run this bit of code right here and in this bit of code I'm going to test if the user is over 100 well they're not over 100 but they are over 60 so nothing is going to happen so we're going to run this nothing is going to happen but say our user was 178 pretty old huh well we're going to say all right is your age over 60 yes so now go here is your aive is your age over 100 yes it is so now run this bit of code right here and we're going to go ahead and run it and it's going to say why are you still a lot because come on guys let's face it if you're 178 years old you should be dead just saying so now we get begin to see how we can make complex tests and very complex programs and I know that I it's called nesting and if statement inside another if statement but you can also nest an if-else in here and you can also nest another if in here and you what you can do is you can nest if inside of if inside of if as many times as you want but say you wanted to test if the user was over 10 then over 20 then over 30 then no to 40 and the 50 well there's even a more simple way to do this than nesting a thousand different ifs inside of each other and I'll probably be teaching you that in the upcoming tutorials but for now what I want you guys to take away from this tutorial is instead of a basic if statement where if the test is false then it just skips the code you can now add an else and what this does is it gives it an alternative choice and also a side from that you can also nest if inside other ifs so that way you can basically test two things and if two things are true run a bit of code so as long as you understand those concepts you should be good go so for now thank you guys for watching don't forget to subscribe and yeah if you have any questions send me a message on my website the new Boston calm and I'll be glad to answer them for you so thank you for watching don't forget sub and I will see you next story
Info
Channel: thenewboston
Views: 544,278
Rating: 4.9480124 out of 5
Keywords: tutorial, beginners, game, class, program, pointers, lesson, thenewboston, bucky, roberts, computer, string, vector, download
Id: jK83lln_T1k
Channel Id: undefined
Length: 7min 2sec (422 seconds)
Published: Wed Apr 06 2011
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.