Transcript
Up to now, we’ve assigned constants and variables directly. Our next step is to change them when conditions change. For that, we use the if-else construct.
Let’s go back to the playground. Change the assignments on the bottom to:
size = 10
area = size * size
Add a boolean variable underneath
var isRound = true
The if
statement takes a boolean value. If true, the statement does what is inside the block of code indicated by the curly braces. For example, add the if statement.
if isRound {
area = pi * area / 4.0
}
We can send values to a console showing us values to print. So we can see these results easily, add print
. The print
statementNeeds commas between values so I can do this:
print("The area is",area)
Run the code. If the console doesn’t show, you’ll see an icon with a dot in the bottom corner. Tap that to bring up the console. We see the area for a circular pizza. Change isRound
to
var isRound = false
Run again, and we get the area of a square pizza.
Boolean expressions are often relations between two values. We use Six symbols to describe this relationship.
To test if the two values are the same, we use ==
for equals, using two equals signs to be different from the single equals of assignment. !=
is not equals.
There is >
for is greater than and < for is Less than for inequalities.
Finally, there are the combinations of equals and inequalities. Swift uses >=
for greater than or equal to and <= for less than or equal to.
I’ll use an inequality to determine the pizza size. I’ll need a string to store the size, so I’ll add.
var sizeName: String = ""
A small pizza is any pizza of size 10 or smaller. I’ll add an if statement like this to mark my pizza as small
if size <= 10 {
sizeName = "Small"
}
I’ll print out the results of what pizza we have
print(sizeName, pizzaName)
Run this, and the console shows a small pizza. Change to 16, and nothing shows. Suppose we wanted to indicate that any pizza larger than 10 is a regular pizza. That is all cases that are false for the if statement. We use else
to tell the Swift compiler to do this instead. I can modify my code like this to indicate regular pizzas.
if size <= 10 {
sizeName = "Small"
} else {
sizeName = "Regular" }
}
Run again, and now we have a regular pizza. However, we might want to mention large pizzas too. Large pizzas are pizzas that are bigger than size 16. How would we do that? If-else
takes any code you want in the code block, including another if
. Frequently, you’ll find nested if
statements, that is another if statement within an if Statement. In the else I can nest another if-else
like this
if size <= 10 {
sizeName = "Small"
} else {
if size > 16 {
sizeName = "Large"
}
else
{
sizeName = "Regular"
}
}
Notice a formatting change I made here. You’ll find different opinions of the placement of the else. Some put it on the same line as the closing of the If block, some on another. It is a matter of developer style and preference — there is no correct answer to this.
Rerun this. We get regular again since 16 is not small or large. Change size to 18, and we get a large pizza. Change the size to 10, and we get a small pizza.
If-else statements are very common and control most of our applications. You’ve seen the basics here, but in our next lesson, you’ll learn more advanced techniques with if-else, its cousin, the ternary conditional operator, and how all this works in SwiftUI, so Don’t forget to hit subscribe to get all the lessons.
The whole code
import SwiftUI
var size:Double = 8
let pizzaName:String = "Margherita"
let pi = 3.141
var area:CGFloat = pi * size * size
let isBigPizza:Bool = area >= 1000
size = 25
area = size * size
var isRound = false
if isRound {
area = pi * area / 4.0
}
print("The area is",area)
var sizeName: String = ""
if size <= 10 {
sizeName = "Small"
} else{
if size > 16 {
sizeName = "Large"
}
else {
sizeName = "Regular"
}
}
print(sizeName,pizzaName)
Leave a Reply