0:00 what's going on guys? welcome back to your seventeenth c plus plus 0:03 tutorial and in this tutorial i'm going to be covering something called the if 0:07 else statement. now let's go ahead and first I want to build a basic if statement 0:12 because an if/else statement is pretty much like an enhanced if statement. so let's go ahead 0:17 and use the knowledge or to information that we learned in the last 0:20 tutorial and build on it. so let's go ahead make a simple program. 0:23 we have a variable called "age" and go ahead and set equal to whatever your age is 0:28 now go ahead and make a basic if statement. Now if you remember from the 0:32 last 0:32 tutorial all an if statement does is it runs 0:36 a simple test. If that test this true it runs a bit of code. 0:40 if that test is false, it doesn't do anything. so let's go ahead and test 0:44 age. say if age is greater than 0:48 sixty let's go ahead and say, uhm, 0:51 cout "Wow" 0:55 you are old. so if the user is over sixty, 0:59 let's go ahead and say, "you are old" and now 1:03 let's go ahead and end that line. run this program and see what we get. 1:06 we get nothing. and that is because I'm 1:09 24, so this program does nothing. but you build this program 1:14 you show it to your boss. you're like hey where's my raise? And they're like, wow, 1:18 this program is impressive in all, however wouldn't it be nice 1:22 if we could give a message to the user no matter how old they are. 1:26 what if instead of skipping this if the test is false 1:29 Let's give him an alternative message like 1:33 if anyone's over 60 we're going to tell them they're old. 1:36 if anyone is under 60 we're going to tell them, hey 1:40 get a job. So, you are saying alright. 1:43 I suppose I could put another if statement and then 1:47 write age is less than 60. But you know what? that isn't the best way to do things 1:52 I'm sure you guys something called if else statement. so the IF statement 1:56 basically 1:57 ran a simple test. and if that test is true it ran this bit of code 2:01 And if it was false it did nothing. it skipped it 2:04 but now we want to say, alright, 2:07 else if the test is false then we want to run this bit of code 2:11 so either way its going to run a bit of code 2:14 either way. if it's true its gonna run this bit of code. 2:18 Let me just go ahead copy that. if it false 2:21 its gonna run this bit of code. but let's not put "wow, you are old" for either one. 2:25 If they're under 60, let's go ahead and write 2:28 "you are young, get a job" 2:32 so now when we go ahead and run a program, 2:35 unlike before when it says alright, since this test is a true I'm just going to 2:40 skip it says alright, 2:41 this test isn't true, so what am I supposed to do it's false? 2:45 What is it I am supposed to do with else. so let's go ahead and 2:48 enough of me talking. run it and it says "you are young, 2:51 get a job" and of course if the user is like 2:55 87 we run the program and they say 2:59 wow, you are old. so now you can see instead of just having a basic if 3:04 statement 3:05 where it was either run a bit code or do nothing 3:08 we now have the option of something happens for true 3:12 and something happens for false. so now either way 3:16 your computer program is going to do something so that is the basics 3:20 of an if/ else statement. So, now that you guys know that, 3:23 let me go even deeper. I said that you can 3:29 go ahead and run a line of code inside an if statement. 3:33 well you can also run multiple lines of code just by putting them 3:36 under each other so if you go ahead and we copy this, we can go ahead and paste it 3:41 paste it, paste it. that it doesn't matter you can go ahead and run it four times 3:45 as long as it's in between those braces right there. 3:50 So, you are saying alright. what if 3:54 I wanted to do something like this. what if not only 3:57 I wanted to check if they were over 60, but if they were over 60 4:01 I wanna check they were over a hundred. so let's go ahead and 4:06 do that. so what mmm, I want to teach you guys 4:09 now is that, inside if statements 4:12 instead of just running simple lines of code you can also put additional if 4:17 statements. 4:17 this is called nesting IF statements. so let's go ahead and say 4:22 if and remember this code is only gonna run if the user is over sixty so now we 4:27 want to test 4:28 if the user is over 100. 4:32 and then we're going to write cout 4:37 uhm, "why are you still alive?" 4:40 I know this is harsh but hey 4:43 just trying to get the point across. so now let's go ahead 4:47 we'll run this bit of code right now. so our programs going to say this 4:51 Alright, if the age is over 60 4:54 which it is, I'm gonna run this bit of code right here, 4:58 and in this bit of code, I'm gonna test if the user is over 5:02 100. well they're not over 100, but they are over 60. so nothing is going to happen 5:08 so we're going to run this nothing is going to happen. But, say 5:12 our user was a 178. pretty old huh? 5:15 well, we're gonna say alright, is your age over 60? 5:18 yes, so now go here. is your ave (inaudable) 5:21 is your age over 100? yes it is, so now 5:25 run this bit of code right here. and we're goint to go ahead and run it and is going to 5:28 say 5:29 why are you still alive? because come on guys let's face it 5:32 if you're 178 years old, you should be dead. just saying. 5:36 so now we begin to see 5:39 how we can make complex test and very complex programs. and, 5:44 I know that I it's called nesting and "if" statement 5:47 inside another if statement. but you can also 5:50 nest and IF / else in here 5:54 and you can also nest another if in here. And, 5:57 we can do is you can nest if inside 6:00 if inside if's as many times as you want. 6:04 But, say you wanna test if the user is over 10 then over 20 then over 30 then over 6:09 40 then 50. well, there's even a a more simple way 6:13 to do this then nesting a thousand different ifs insdie each other 6:16 and I probably teaching that in upcoming tutorials, but for now 6:20 what I want you guys take away from this tutorial is instead of a basic 6:24 if statement, where if to test is false 6:27 then it just skips the code, you can now add in else 6:30 and what this does is gives it an alternative choice 6:34 and also aside from that, you can also nest 6:38 if inside other ifs. So, that you can basically test two things. 6:42 and if two things are true, run a bit code. 6:45 So, as long as you understand those concepts, you should be good to go. So, 6:49 for now thank you guys for watching. Don't forget to subscribe. And yeah, 6:52 if you have any questions, send me a message on my website, the New Boston dot 6:56 com 6:57 and I'll be glad to answer for he said thank you for watching. 7:00 Don't forget to sign up and I will see you next tutorial.