Hello welcome to the eighth tutorial in the swift basic series and in this poem we look at conditional statements. conditional statements basically allow a block of code will run if a third condition is met. conditional statements are one of the core foundation that many print all applications brought upon. Especially game if you've ever made gains. Any capacity, even if it's using some sort of visual editor will know that being able to check for conditions is essential, even in general application checking data, for example. Go ahead and open up our project.
What we're gonna do is just get rid of this second, we're going to create a variable called on the initialized to the value seven. And then to do a basic conditional statements you do if bracket bracket brackets aren't necessary here, but we recommend them or they do help maintain code and To generate readability, but again, many languages do you need a lot c++ and C, we can get rid of that put the condition without to go we prefer brackets and then in here you put the condition. So we're just going to put it in the body for knowing the cause. It basically is a check a comparison operator, whereas the single equals is the assignment operator. So you don't want to get mixed up with the code you'll have. Well, you'll have issues basically, we'll just do it does i equal nine?
And where do print ln and like what do I know if I run this? Nothing pops up simply because I eat at night but if I change this to nothing, I run out of it. Actually nine which is great. The next step For a conditional statement, if else if so you can do else if this is basically allows you to perform a number check if the first check isn't true. So this will only get performed if the first statement true. So in theory could do multiple Yes, but they will all get checked.
And you may not want that. So Alfa Ali equal equals eight. So if we're doing rent, and then oh boy is eight I run this and it just says all is knowing. But if I change this to eight, and then they fade it they don't say nine anymore because I isn't nine. And the final part of a conditional statement is the else statement that you can add and this has led to on a number code if none of the conditions are true for the print and do i is valid? And I'm just going to change on the first date the IE date for do on seven, I'm running it first check that icon I know.
Does it collate? Nope, then it goes to the house. Basically, none of conditions are true. You don't actually need an analysis to do so you can have it like this. I can add multiple elsif today, or you can add a light list with this if else or you can just have a if statement. One thing to bear in mind you can have multiple outs within one single conditional section statement.
And that's it for this tutorial. That's what conditional statements are. They are exciting. really powerful because you can put conditional statements within conditional things as well which is pretty cool. Well said brackets aren't necessary. So if I were to get rid of these and change this to nine now and run it does i equals nine, but we just prefer the readability that brackets provide so it's up to you.
In the next tutorial we're gonna look at the for loop if you have any questions feel free to message your support sonar system, Dakota, UK, the email will be in the description. You can comment on this video or directly message us via YouTube or the require link for source code will also be in the description and as usual, thanks for watching and have a nice day.