100 Days of SwiftUI Learning — Day 5

Krishna
4 min readMay 5, 2022

--

The only place success comes before work is in the dictionary.

Agenda

  1. if statements
  2. Multiple conditions
  3. switch statements
  4. ternary conditional operator

If statement

Syntax:

if condition {
Statement / Statements
}

If the condition is true, the statements within the curly braces gets executed. A general condition will be a comparison statement. For example, if a variable is an Integer, we check if the value is less than, greater than or equal to a number. Based on the result, the statements inside the braces gets executed.

Comparison operators

  1. Greater than and greater than or equal to: >, > = (Note: There is no space after > symbol).
  2. Less than and less than or equal to: <, < =
  3. Equals to: ==
  4. Not equal to: !=

We can use the functions that we used for collection data types as a part of condition.

//Example:
let scores = [10,20,30]
if scores.count ==3 {
print("The count is \(scores.count)")
}

isEmpty

This is used to check if a collection data type or a string is empty.

//Check if a string is emptylet name == ""
if name.isEmpty == true {
print("The string is empty")
}

We know for a fact that the statements inside the braces will get executed, only if the condition value is true. So, if the string name is empty, the value of name.isEmpty will be true. Because of this, the condition can be trimmed down as follows.

let name == ""
if name.isEmpty {
print("The string is empty")
}

Multiple Conditions

if … else

if condition {
if statements
}
else {
else statements
}

To better understand this let us consider the case where this can be used. To be eligible for drivers license, one has to be 16 years old. So, instead of writing two if statements, one to check if the provided age is greater than or equal to 16 and other to check if less than 16, we can use if … else.

let age = 16if age >=16 {
print("You are eligible to get your license")
}
else {
print("You are not eligible at this time")
}

if … else … if

Consider the situation where you are checking for three different conditions for same variable. Say you are checking the sleep time of a person and let us assume the required is 7 hours. If a person sleeps less than they are considered under slept and if more than 7 they over slept.

let sleepTime = 7if sleepTime == 7 {
print("Well rested")
}
else if sleepTime < 7{
print("Under slept")
}
else {
print("Over slept")
}

Nested if conditions and “&&” operator

If you have to check for multiple statements at the same time, you can do that in two ways.

Let us consider the example of good temperature. If the temperature is in between 20 and 25, you can say it is good.

let temp = 24if temp >= 20 {
if temp<= 25 {
print("Good weather")
}
}

In this case, we are using nested if condition. This can be implemented in another way by using && operator.

let temp = 24if temp >= 20 && temp <=25{
print("Good weather")
}

Using or (||) operator

Similar to and operator we have seen above, there can be situations where you can or operator.

Syntax

if condition1 || condition2 {
statements
}

Switch Statements

Switch can be used when you have multiple if..else.. if statements. The tutorial on hacking with swift gives a very good example to illustrate the use case. Refer this page for the example.

Syntax:

switch variable_name {
case condition1:
statement 1
case condition2:
statement 2
.
.
.
case condition n:
statement n
}

Though the above syntax seems practical, there can be situations where you cannot cover all possible conditions. In that case, you can use default statement. That is, if the value of your variable is not covered in the conditions that you have written, the statements under default gets executed.

switch variable_name {
case condition1:
statement 1
case condition2:
statement 2
.
.
.
case condition n:
statement n
default:
statements
}

Always remember to write your default statements at the end because, switch will check for conditions in order from top. If you write default statement on top none of the conditions will get checked and your result will be the same for any value of your variable.

fallthrough

Say for example, you have written n cases and a default case. Now, if your ith case meets the requirement then all the consecutive statements after that should be executed irrespective of the condition. To get this we use “fallthrough”.

Ternary Conditional Operator

Ternary operator checks the condition and return one of two values.

let age = 16
print(age >=16 ? "Eligible for license" : "Not eligible")

In the above example, age> = 16 is the condition. If it is true, “Eligible for license” is printed. If not “Not eligible” will be printed.

To anyone who is wondering, why I am writing the content that is already there on the website, today is the best example. I was feeling a bit lazy and was not able to focus. But writing this article and to keep up with the challenge, I forced myself to read and understand the content. Though that should not be the way to learn something, personally it works for me.

That is all for today’s learning. See you tomorrow.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Krishna
Krishna

No responses yet

Write a response