How to Write a If Statement and Conditional Operator in Swift?

If Statement and conditional operator in Swift

Tested on: Xcode 12.2, Swift 5.3

Sometimes you want to run code if certain conditions are met. For example, if the username and password are correct, log in. If it is incorrect, ask the user to enter again. In terms of code or Swift code, you can write an If Statement. Combining If Statement and comparison operators you can show a different state in a different situation.

Note: Before you read this reference you must know the following topic, Comparsion and Logical Operator.

In this topic you will learn about:

  1. What is a If statement?
  2. If Else statement
  3. If, Else If, Else statement
  4. Conditional Operator





1. If-Statement

If statement allows you to run a certain code if the condition is true. The code below will print "yes it is true" if the value is larger than five. If it is not, it will not print anything. This allows you to do something only when the condition is met.




2. If-else statement

If statement allows you to run a certain code if the condition is true. But what if you want to run a certain code if it is false. You can use extend the if statement with else. This allows you to run a certain code if the condition is false. For example, if the value is not larger than five, it will run the code in the else statement and the result will print "yes it is false".

Swift Code :

let value = 2
if value > 5 {
   print("yes it is true!")
} else {
   print("yes it is false!")
}

Output :

yes it is false!




3. If, Else If, Else statement

You can go even further. Sometimes you want to check for multiple conditions. In the example below, the if checks whether the value is less than 6 and print out "Less than six!" if it is true. The if statments then checked the rest of the condition in order:

  1. Value is less than 20
  2. Value is less than 30
  3. Others

Swift Code :

let value = 22
if value < 6 {
   print("Less than six!")
} else if value < 20 {
   print("greater or equal to six but lesser than 20!")
} else if value < 30 {
   print("greater or equal to 20 but lesser than 30!")
} else {
   print("others")
}

Output :

greater or equal to 20 but lesser than 30!


Note: You can write mutiple conditions inside using AND and OR operator such as

Swift Code :

let value = 6
if value > 6 || value < -20 && value != 21 {
    .....
}



4. Conditional Operator

The conditional operator is related to the if statement but it can only return one of the two value and unable to run a group (block) of code like the if statement you have seen above. The basic syntax of condition statement is shown below:

CONDITION ? TRUE VALUE : FALSE VALUE


For example, if value is more than 5 assign "FIVE" else assign "NOT FIVE"

Swift Code :

let value = 6
let newValue = value > 5 ? "FIVE" : "NOT FIVE"
print(newValue)

Output :

FIVE


The code below is the same as the following above. But it is written as a IF statement.

Swift Code :

let value = 6
var newValue = ""

if value > 5 {
    newValue = "FIVE"
} else {
    newValue = "NOT FIVE"
}

print(newValue)

Output :

FIVE


Long right?
So it is recommended to write a conditional operator if you can to assign a value if it is true or assign to another value if it is false.