``To put it simply, a group of codes, when executed on a particular/certain conditions, are known as conditional statements. You can quickly identify them by figuring out some noticeable terms like if, if ...else, switch case. Generally, developers use it when needed to execute the code on a particular condition, and that could be any like, for example,
- if the value is more significant, lower, or equal to
- if the specified condition is true/false, etc.
Read and go over the detailed explanation below to better understand each type.
IF... ELSE Statements
Understand this example before jumping towards the main content. So consider giving someone a contract for painting a house, and in return, they will keep one condition to start working with you. The condition is:
- He/she will not work on Sundays
`swift
/* IF Sunday
Holiday
ELSE
Working Day */
//Conditional Statements works like this
`
To write it down in the code format, you have to start with the term "if" and the just like functions, add open and close braces, and end with parenthesis.
`swift
var day = "Sunday"
`
`swift
if (day == "Sunday") {
//Note: var day = "Sunday" is already declared but outside this conditional statement
print("Holiday")
} else {
print("Working day")
}
`
The variable day has a value called "Sunday". Remember that never use only one "=". It should be "==" because it compares our condition with the given value and only works afterward.
Now the question is: What happens if there are more conditions?
Answer. We use "Operators" for our conditional statements.
They are mainly two kinds of operators, "OR" and "AND" denoted as "||" and "&&" respectively.
Again try understanding this with the same example but with more conditions.
This time the conditions are:
Holiday on Sundays.
Also, off-day on National Holidays.
As you are familiar with simple IF...ELSE, directly understand the code format:
`swift
var holiday = "National"
if (day == "Sunday" || holiday == "National") {
print("Off day")
} else {
print("Working day")
}
`
If you see here, you use the "OR" operator because either condition should be true. When you press command+R, the program will run and go through this condition to check whether the given condition is true with the value you entered, but here either should be true to print "Off day" unlike the first example. Otherwise, it will print "Working day".
Now add some more toppings to the example. So, after five years of working in a position, a person tells his/her boss that he/she will only be having off-day on Sunday if there is a national holiday.
The condition for that would be:
`swift
if (day == "Sunday" && holiday == "National") {
print("Off day")
} else {
print("Working day")
}
`
Here, if you see the condition, you use the "AND" operator. This is because you are putting two needs simultaneously, and both should be true to take a holiday on Sunday. Otherwise, you have to go to work.
In technical terms, it will print "Off day" if the condition is proper, otherwise "Working day".
IF... ELSE IF Statements
Continue understanding this concept as well with the real-life example.
- The person is hustling so hard that he now keeps one more condition that he/she needs a vacation in June.
- Additionally, a holiday on Sunday if there is a National Holiday.
There you write:
`swift
var month = "June"
if (day == "Sunday" && holiday == "National") {
print("Off day")
} else if (month == "June") {
print("Vacation")
} else {
print("Working day")
}
`
Switch Statements / Switch Case
They work pretty much similar to if ...else condition but syntax and way of working are slightly different.
Let's declare a variable,
`swift
var event = "Apple"
`
Write down the keyword "switch" to start with the switch case. Then select the variable declared "event" and then a parenthesis.
`swift
switch event {
case "Apple":
print("WWDC")
case "Google":
print("Google I/O")
case "Facebook":
print("F8")
case "Microsoft":
print("Microsoft Build")
default:
print("Tech Event")
}
`
Here case works like if keyword and title inside the quotation mark work like a condition you saw in the round braces of if ...else.
What is default?
Answer. This is also one point where if ...else condition doesn't match with switch case. A default is required for every switch case to work as else, though not the same but similar. After every condition fails, default comes into action for giving out the same initial value of a declaration of variable/constant or whatever you set.
What's next?
We're happy to help you understand the concept of Conditional Statements, but in programming, there are other important topics that you might have gone through or are remaining. So we encourage you to go over the concept of functions, arrays, or dictionaries. If you are already familiar with all those topics, consider learning structures, closures, or methods next. Till then,
Eat. Sleep. SwiftAnytime. Repeat.
Top comments (0)