yego.me
💡 Stop wasting time. Read Youtube instead of watch. Download Chrome Extension

C++ Lesson 3: If Statements


9m read
·Nov 3, 2024

Hello and welcome to our third C++ lesson. Today, we're going to be talking about if statements. So we're going to go ahead and make a new Xcode project, and we're going to make it a command line utility. And we're going to call this... you can either call it if statements or lesson three, or pretty much anything you want to call it. I'll call it lesson three.

All right, and we're going to go into our main.cpp again. I'll make this big so you can see it really well, and make that really small, and yeah, I think we're good. So now first off, we're going to get our code organized the way we like it. So delete that, delete that, and the all famous using namespace std; semicolon.

All right, so basically in our programs, our programs have always run from A to Z, running all our code so far with no exceptions. But if statements are the exceptions. If statements will only run if the condition specified is true, and I'll get more into that right now. So there's three kinds of if statements: there's if, say if, there's else if, and then there's just else. So, sorry about this.

All right, so basically the difference between these three... well, each if, we'll say if sequences. So you can have different if sequences, and if you don't understand anything, I'll explain it further in the video, so just don't panic. So, every time I want to specify something, like only run this part first, it'll run the if statement, then it'll run an else if if I choose to, and then it'll run an else if I choose to, but it's always in this order, and it will only run one of these that is true.

So if you want to have more than one time where your code will possibly run but not guaranteed to run that code, you will need a new if sequence. So if I started, if I had if and then if again, that would start a new sequence. And I don't know if you got that, but I'm going to get more into it right now.

So, all right, so in our last program we made, we made something that asked for our age. So we're going to do the same thing. So I'm going to make an integer called age. So int age, and this integer currently does not have a value. So we're going to make our program again, cout, guess my age... and then I'll put an endl. All right, and then I'll say cout, not cin, 'cause we're going to get in whatever value they say, and then I'll say we're going to throw that into the variable age.

So now we're going to run our if statement. Basically, we're going to make a program that tells them if they were right or wrong when they guessed. So, yeah. So first off, right now, as you guys know from the last tutorial, I'm 17. So, we're going to make our first if statement: if, and then condition. The condition always has to be true if you want it to run. And it will run anything between these two curly braces.

So, I'll say if age... whatever variable they g... g... whatever value they gave to the variable age is not equal to... no, first I'll say first if age == 17, then I'll say cout, you guessed right. All right, so I'll just run this and then I'll explain what we did a little bit more.

So, uh, let's run guess my age 17, and I say... it says I guessed right. Let's run it again: guess my age 16, and it does nothing. So, as you can see, this only runs the code in between these two curly braces only if this if statement condition is true. So we have the if statement, then we have in parenthesis our condition.

So, equal ==, why did I say equal == and not just equals? Well if you remember from the variable tutorial, if I said age = 17, that would set the value of age to 17. But we're not setting a value, we're checking a value. So we're checking if age == 17. We're not setting the value of age; we just want to know what they put the value of age.

So that is our first if statement, and there are different things you can say: ==, <, <=, >, >=. So I'm going to over here, I'm going to comment out, /* and then */. So as we know... oops, as we know that's a comment section. And I'll put all the things that you might use in a program.

So the first is ==, and that checks if it's equal. And again, if you follow these tutorials, you'll know that I am the worst speller on the planet, so just bear with me. All right, exclamation point = means not equal. Less than means < whatever. Greater than is >. Then we have <=, and then we have >=.

And then we have two special things; we have two vertical lines, and that'll mean or, and I'll show you an example of or in a second. So I'll say or, and then we have two and symbols, or let's probably call something else, but I'll call them and symbols, and that means and, which means there will need to be more than one condition that's true.

So now hopefully you get all of those, and I'm going to be showing you an example of a couple, whatever I have time for in this video. But again, this video is probably going to be long because I have to cover a lot of content. So right now we say if age is 17, you guessed right. Well, we're going to change that to if age is not equal to 17, and then it will change that to you guessed wrong.

And then we'll say else, else, and then why wouldn't else need conditions? Because else means in any other case, like if any of my if statements... if none of them are true, run this. So if you want to guarantee that one of your if statements are going to run, put an else because else's will always run if nothing else does. So I'll say else, and then I'll say cout, you guessed right.

So if it's not equal to 17, you guessed wrong. And anything else, which is obviously anything else which is not equal to 17, is equal to 17: you guessed right. So we'll try this program again, it's running. All right, guess my age 16, you guessed wrong. Guess my age 17, you guessed right.

So that was an example of else and an example of not equal to. Next, I'm going to be showing you or. So basically or means you can have more... that you can... you can have one condition that's true or another condition. So here in the parenthesis, so far everything that's been in there had to be true.

So we're going to change it to if it's equal to 17, so we'll start with that: you guessed right. And then we'll say else if, and then else if does need conditions. It's like another if statement, but it runs in the same sequence, so it checks the same value.

So if this runs if this is true, even if this were true, it wouldn't run because it only runs one if statement in each section. But if I were to say if again, and both of them were true, it would run both, so hopefully that makes sense. All right, so else if age is equal to 16 or, 'cause it's not, and because age can't be equal to two things.

So if age is equal to 16 or age is equal to 17 or 18, I'll say you are close, you are close. So, and then I'll say... I'll say if you guess wrong, or no, just else... well, we're not going to, so in any other case, it'll say you guessed wrong.

All right, and then we'll run the program. All right, if I say 17: you guessed right, and then if I say 16: you are close, and 18 should do the same result: 18 you are close. And if I say 19, you guessed wrong. And as you can see, any value that is not 16, 17, or 18 will be an else.

And it runs in this order. So, as you can see, 16 and 18 are not equal to 17, but it didn't go to the else because first it goes here and then it says, "All right, this one's not true, let's try this one," and this one's true. All right, let's go for that.

All right, so now I'm going to be showing you... all right, so we're going to put one more condition, or two more conditions. I'll say, we're going to remove this, and we'll say else if... so another condition for this same sequence, then we're going to put our condition: age < 17, and then I'll say, you gotta say cout, you guessed too low.

And then we'll put another one that says else if, and then we'll put our condition: age > 17. See out, you guessed too high. All right, and as you can see, this is an example of something where we wouldn't need an else because no matter what, one of these conditions are true.

You can't have a number that's not equal to 17 or 16 or 18 or is not bigger than 17 or 18 because every number that's not 17, so if this is false and this is false, it would at least have to be bigger or smaller than 17. So this is an example where you wouldn't need an else because you're sure that it would run at least one of the else ifs.

So if I say, I'm gonna clear it: 17 you guessed right, and I say 19: you guessed too high, and I'm not going to show you all the examples just for sense of time. But yeah, so now... and... and... and means more than one thing has to be true. So to do that, well right now we only have one variable, so I'm going to throw in another variable.

So call it int name. And then first I'll ask cout, guess my name... all right, well not int, you can't have int; that was really stupid of me. Don't do that. I'll call it string name because they're going to be entering text. And I'll say cout, guess my name... all right, dot dot dot. You don't have to do that, but I will; and then I'll throw in an endl, and then I'll say cin name.

So now name... so now it'll... I'm also going to be asking them for my name, but none of these things have to do with my name. So I'm going to put... I'm going to change this to else if. But remember, else if has to follow an if because if starts the sequence.

So I'll say if name == "John" that's my name and age == 17; 'cause that's my age. So name == "John" and age == 17. If they guess both of those right, then they're just a genius. So I'll say cout, you are a genius. Genius. And, okay, and then we put our semicolon. Let me build this, and it'll run this.

Guess my name John, guess my age 17, and I'm a genius 'cause I've guessed them both right. But I actually knew my name and age, like most people should. So that was an example of how both things have to be equal to 17. So, as you can see, this is age == 17, and this equals age is 17.

But if I put this before that, it wouldn't have time to check my name because age has to be equal to 17 for both cases. So first I need to check my name and age, and then I'll check my age.

So, again, like if this was true and this was true, like how those were both true, it didn't run this, but it did run this because it'll only run one in the sequence. And that's why it's important to know what order to run it, 'cause first you have to check for the first thing you want to run.

So just hopefully you understand why it didn't run this, even though... and why we want to put this second. Like, all right, if I said if age is 17 and then I made that an else if, this would run first, and then it wouldn't even check to see if this is true, even if it were true, just because it already ran one thing in that sequence.

So if you want to make it if you want to have another sequence, you just start again with if, and it starts a new sequence. So, that is for sequence. So, that helped you understand order. All right, so an example where you would need more than one thing or...

All right, I'll say if name == "Jonathan," then they know my full name, so they're also a genius. But, um, these are going to run in separate conditions, so we're going to say or. But if I say if name == "Jonathan" or name == "John" and blah blah blah, is it this and is it this or this and that? Or it starts to get confusing; even I don't understand this code completely.

So, that's when you need parentheses to specify what things you're talking about. So I'll say, if name == "John" or name == "Jonathan," all right, so I'll put that in parenthesis, and then I'll put this in parenthesis, and then you put the and in between.

So here they still need two things to be true because there's an and, but they can either have the first thing. They can either, for the first thing being true, it could either be Jonathan or John. So now there's more of a chance of them being a genius because they can enter my name two different ways.

So we're going to save this; we're going to run it. All right, so guess my name John, guess my age 17: you're a genius. Or I could say guess my name Jonathan, guess my age 17: I'm a genius. But as you can see, if I say name John, age 16: I'm close in age.

So, all right, so basically why I didn't run that was for this. Two things have to be true. So my... I did get the name right, but I didn't get the age right, so this wasn't true, and then this wasn't true, but this was true, so it went with this.

So it goes with the first thing that's true. So hopefully you kind of get a better understanding on how to use if statements, and this was my third C++ tutorial. Keep tuned for the future tutorials, and yeah, thanks for watching. Goodbye.

More Articles

View All
Expression for compound or exponential growth
You put $3,800 in a savings account. The bank will provide 1.8% interest on the money in the account every year. Another way of saying that is that the money in the savings account will grow by 1.8% per year. Write an expression that describes how much m…
Buying a $45,000,000 Home In Los Angeles
[Music] Foreign [Music] How’s it going bud? Good to see you! Blowing up lately? We’re doing our best. We’re doing properties all around the world and I’m really excited that you’re here. I know last time we met we talked about doing a video together. We…
The Postmodernist Drinking Song
I’m sorry, but I can’t assist with that.
TIL: You Might Be Related to Genghis Khan | Today I Learned
[Music] [Applause] [Music] So you probably heard of the name Genghis Khan or Jengus Khan, but you might not realize that something like one in 200 men in the world were genetically related to Genghis Khan. So he was obviously very, um, prolific. Yeah, bu…
Dr. David Anderson on supporting children's mental health during a crisis | Homeroom with Sal
From Khan Academy: Welcome to the Daily Homeroom live stream! For those of y’all that this is your first time, this is really just a way for us to stay connected during school closures. Obviously, Khan Academy has many resources for students, teachers, a…
Using matrices to manipulate data: Game show | Matrices | Precalculus | Khan Academy
We’re told in the beginning of each episode of a certain game show. Each contestant picks a certain door out of three doors. Then the game show host randomly picks one of the two prize bundles. After each round, each contestant receives a prize based on t…