Swift Programming

Swift Optionals

In the previous article, we learned about different data types available in Swift and also noticed variable or constant declared of those types contains a default value.


let someValue = Int()

When you run the program, the output will be:

Swift guard statement

Swift If Statement describes how you can perform actions based on certain condition (boolean value). In this article, we will explore the benefits of guard statement over if statement to control the program flow and write more simpler and clean code.

Swift switch Statement

The switch statement is also a variety of Swift control statement e.g.if-else, guard etc., that performs different actions based on different conditions.

The beauty of switch statement is, it can compare a value with several possible matching patterns. Therefore, it can be used as a substitute for long if..else..if ladders while matching complex pattern.

Swift Ternary Conditional Operator

The ternary conditional operator "? :" is a shorthand for if-else statement.

The syntax for ternary conditional operator is:

condition ? value1 : value2

How ternary conditional operator works?

Here's how this works

Swift Operator precedence and associativity

Swift Operator precedence

Operator precedence is a collection of rules used in order to evaluate a given mathematical expression. When there are several operators used in a single expression, each part is evaluated in a certain order called as operator precedence. Certain operators have higher priority than others which affects how an expression is evaluated.

Swift if, if...else Statement

In programming, you may want to perform different actions based upon the specified condition is true or false (which is known only during the run time). For such cases, control flow statements are used.

Swift if (if-then) Statement

The syntax of if statement in Swift is:

Swift Operators

Operators are special symbols (characters) that carry out operations on operands (variables and values). Some basic operations includes assigning, changing, combining and checking values.

For example, + is an operator that performs addition.

In Swift variables & constants article, you learned about variables/constants. Now, in this article you will the use of operators on them.

Swift Bitwise and Bit Shift Operators

A bit is used to denote a binary digit. A binary digit can have two possible values either 0 or 1. As a beginner level programmer, you don't have to work with operations at the bit level.

Working with primitive data types such as: integer, float, boolean, string etc is enough. You may need to work at bit level when you are dealing with low level programming.

Swift while and repeat while Loop

In the previous article, we learned about for-in loop to run a set of tasks for a certain number of times. In this article, you will learn to use while and repeat..while as an alternative of for-in loop when the number of iteration is unknown.

A while loop executes a set of statements until a condition becomes false. These kinds of loops are best used when the number of iterations is not known before the first iteration begins. Swift provides two kinds of while loops:

Nested Loops in Swift

If a loop exists inside the body of another loop, it's called a nested loop. Here's an example of nested for-in loop.

for i in 1...5 {
    //statements of outer loop
    for j in 1...2 {
        //statements of inner loop
    //statements of outerloop

Here, the for j in 1...2 loop known as inner loop, is inside the body of for i in 1...5 known as outer loop.