Introduction
In this blog, we will learn how to implement a decision in Swift programming language. The decision includes the if else ternary operators etc. We will also focus on the implementation in the given example.
What is decision making?
- It is a decision making statement and allows us to take the different paths of the statement, which depends on a condition.
- For example, in calculator, when the user presses the sum button, it shows the sum of the numbers.
- This is called decision making.
Decision making types
- if statement
- if else statement
- nested if else statment
- switch statement
if statement
- The first and most common condition in all the programming languages is the if condition, which controls the flow of a program through the user permission.
- It is if condition, which only works when a certain condition is true.
if else statement
I have if else statement, which is dedicated to the variable of the sample program .
- var a = 10
- var b = 20
- if (a < b) {
- print("a is less than b")
- } else {
- print("b is greater than a")
- }
if else statement states the condition to be true
- print("a is less than b")
On the contrary, else statement states the condition to be false.
- print("b is greater than a") exit the program.
Nested if else statement
I have nested if else statement dedicated to the variable of the sample program.
- var a = 10
- var b = 20
- var c = 30
- if (a < b) {
- print("a is less than b")
- }
- if (b < c) {
- print("b is less than c")
- } else {
- print("c is less than b")
- }
if statement states the condition to be true.
- print("a is less than b")
Again, if statement states the condition to be true.
- print("b is less than c")
It is else statement, which states the condition to be false.
- print("c is less than b") so exit the program.
Switch statement
- The switch statement is a control satement.
- This handles multiple selections by passing control to one of the case statements.
- It is not necessary to use it in Swift programming language in break statement.
Example program
- var index = 10
- switch index {
- case 100:
- print("value of index is 100")
- case 10, 15:
- print("value of index is 10 or 15")
- case 5:
- print("value of index is 5")
- default:
- print("default case")
- }
Output
- value of index is either 10 or 15
This is the basic concept of decision making in Swift programming language.