Journey to became an iOS Developer
Operators, conditions, and loops for Swift
Hi everyone, actually I’m a Java developer and I developing Android apps. I recently decided to learn Swift. I want to share what I learned here. I hope it will be enjoyable :) Let’s begin.
Operators
Arithmetic operators are the same as Java or other programming languages.
1. Arithmetic Operators
One of the differences between Swift and Java is that they don’t use semicolons at the end of the line.
As you have noticed, we do not use semicolons, unlike java.
2. Comparison Operators
While defining a variable in Swift, it is not mandatory to specify the variable type, except in some cases. swift detects variable types automatically type for you.
Conditions
Conditional operators are the same as in Java or other programming languages. There are only a few simple different situations.
1. if-else
If-else structure is not very different in Swift and Java. Swift provides a little more flexibility. E.g the use of ( ) is optional, but the use of { } is mandatory.
2. Ternary Operator
Ternary operator is a different form of if-else. It needs three values, first value is condition. If the condition is true, the second value is returned, otherwise, the third value is returned.
3. Range Operator
If there is an operation that you need to perform in a certain range of numbers, Swift uses the Range operator for this. It is quite simple to use.
Loops
Loops are the same as other languages. There are only minor differences.
1. For Loop
With Array
With Range Operator
With Stride Function
The loop iterates over the range from 1 to 10 with an interval of 2 However, 10 is not included in the sequence.
2. While Loop
3. Repeat Loop
The loop is similar to while
loop with one key difference. The body of repeat...while
loop is executed once before the condition expression is checked.
In conclusion, these structures used in swift are not very different from Java. They are used in Swift same way with simple differences.