When you desire your code to perform only if a certain condition is true then you can use an if statement.
You can execute a if like so:
var brand = "Apple" if brand == "Apple" { print("Brand is Apple") }
An if statement is begin with if followed by the condition then a block, open and close braces.
Let’s include a else after the if:
var brand = "Apple" if brand == "Apple" { print("Brand is Apple") }else { print("Brand does not equal Apple") }
The first if statement is checked, if true it’ll perform the if block. Else it’ll implement what’s in the else block.
You can also include more with else if.
You can actually include in various else if blocks for every condition you may have.
In my next blog, I’ll explain the switch() statements which are similar to if statements.
Leave a Reply