Hello, welcome to the 22nd tutorial on the c++ programming series. And in this part, we will look at conditional statements, conditional statements LA to execute a block of code. But only if a condition is met. This is extremely useful in a program, you're going to be actively checking. If the user had done stuff always certain actions have occurred, then you will execute a certain piece of code basically perform a certain function. That'll show you how they work.
There's quite a few different conditional statements. Actually, the more one or the other, this extension of Deacon, the original base conditional statement, all of which you will most likely use a lot all the time. So we're gonna do is create a variable called I make it equal to nine. So the first conditional statement we're gonna be looking at the first part is the if statement was just basically checks does a certain value equal a null value of which we can substitute variables as well. So, if we this is the general format of an if statement and in here you put your condition there I was going to put pi double equals nine. Remember when we looked at operators earlier on in this series, we said that the double equals is the comparison operator with a single equal is the assignment operator.
You can also combine or the operators or you can do and they can do multiple addition or all or all of which we've looked at earlier on. So if you want more information, just flip back in this series thoroughness do is i equals nine. Then we're just going to see out something simple like I is night TD TDC out snowmen and rowing Run it. On Phase II, it says number three, change it to, let's say eight. Basically, eight doesn't equal nine. So no finish should be printed at the end up in a word printed, I could I didn't call knife D brackets on Actually, I mean these curly braces aren't actually necessary at all.
If you don't have these curly braces, only the in the following line will actually be present within the conditions within the if statements like for port nine there again, it's only the first following code line will be in their third port like a number line below here. This will be technically outside the if statement. But I generally put in curly braces at the end of one line seem to put this a lot neater and easier to read. But this way, you can have multiple lines here. You can also have nested if basically if within that Then at night we want to it's quite common practice of law in a very complex program. The next conditional statement we're going to look at is the aos steam which basically run this piece of code if the if statement is false and the syntax of that is you put the keyword else no condition because it's just saying otherwise run this piece of code so we're gonna put STD prop is not valid right it is nine but if we put it to eight said oh he's not money basically go through this does it for nine no it did not.
Otherwise just run this piece of code and it's the same situation here as well. You don't have to have the further brace it will be the same with all the other sabol mentioned after this. You don't have to have the curly braces and you can have multiple lines the hump only if you have the curly braces. So the next part We're going to be looking at is the else if statement and the elsif go between the if and else else. If this is it, this basically just says, if this statement is false, then check this statement, they can have multiple alpha 51. Otherwise, if all of them fail, we'll just bump back to the house take away my thing.
Quietus have an if statement on its own. Yeah, you can do nothing wrong with that. But if for example, in this in this instance, I'm just writing so so you can see some visually. Here we do i equals equals eight, and we do equals equals seven. I was gonna simply copy and paste this. The reason why you may not want to do If same thing want to do if elsif else if etc, is, in this instance, I equal eight.
So if we do or equal if eight equals nine Now, does it quite Yes, and by definition, a token equals seven. Because if this condition is true, then the other condition can't be true. Obviously, you might change the value of oil or any other variable. Therefore, you might want an if statement. But obviously that will depend on the situation. And that's something you're going to have to judge when you're programming your application.
So if I just run this, then I think if I put seven he said, oh seven, but if I were to just put 67 He says, I is not valid unlock. So you can have as many elsif as you want, and the else isn't compulsory. So if I can get rid of that. So if I event one isn't valid, and it's not one of these conditions, then it To them print out anything so you can have if on its own you can have if else if else or if elsif else. And like I said, you can have multiple health if remember again, it's going to mention it one more time the curly braces aren't necessary but they help organize code. Plus if you want multiple lines in the condition statement, you need to have the curly braces.
Sorry about that. That's it for this this notification that's it for this tutorial. In the next tutorial we'll be looking at the for loop should have any questions feel free to message us our support system with a curry Okay, the email will be in the description. You can comment on this video with a sterically message for YouTube. You can do whatever you feel comfortable with. All the recording for source code will also be in the description and as usual, thank you for watching and I hope you have a nice day.