Category Archives: Swift Tutorial

Swift Tutorial

Swift While Loop

Swift While Loop

In Swift, the while loop will execute a block of statement as long as a test expression is true. While loop is useful when the number of iterations can not be predicted beforehand. The while loop evaluates test expression at beginning of each pass.

Swift While Loop Flow Diagram

swift-while-loop-flowchart-diagram

 

Syntax:-

Here, Condition is a Boolean expression that results in either True or False, if it results in True then statements inside loop body are executed and condition is evaluated again. This process is repeated until the condition is evaluated to False. If the condition results in False then execution is skipped from loop body and while loop is terminated.

Example:-

Here, we have initialized ctr and maxCtr to 1 and 5 respectively, in the next statement we have while loop, that checks the condition ctr <= maxCtr for each of the iteration. If the test condition returns True, the statements inside loop body are executed and if test condition returns False then the while loop is terminated. Inside the loop body we have incremented the ctr value by one for each if the iteration. When we run the above swift program, we see the following output –

Output:-

swift_while_loop

Swift For in Loop

Swift For in Loop

The for in loop takes a collection of data(arrays, dictionaries, sets , or anything that provides an iterator) and iterate through the items one at a time in sequence.

Swift For In Loop Flow Diagram

swift-for-in-loop-flowchart-dagram

Syntax:-

Here, items provides an iterator that allows us to fetch each of the single element from the given collection, and item hold the the current element fetched from the items.

Iterating Over Range Using For In Loop

In Swift, we can loop through the range elements using for-in loop as following –

Example:-

Output:-

swift_iterating_range_using_for_in_loop

Iterating Over an Array Elements Using For In Loop

In Swift, we can loop through the array elements using for-in loop as following –

Example:-

Output:-

swift_iterating_array_elements

Iterating Over a Dictionary Using For In Loop

In Swift, we can loop through the dictionary elements using for-in loop as following –

Example:-

Output:-

swift_iterating_dictionary_elements

Swift Iterating Over a Set Using For In Loop

In Swift, we can loop through the set elements using for-in loop as following –

Example:-

Output:-

swift_iterating_set

Swift Switch Case

Swift Switch Case Statement

The switch statement is simplified form of the nested if … else if statement , it helps to avoid long chain of if..else if..else statements. A switch statement evaluates an expression against multiple cases in order to identify the block of code to be executed.

Swift Switch Case Flow Diagram

switch-statement-flowchart

 

Syntax:-

Example:-

In the above swift program we have an integer constant dayOfWeek with initial value of 5, and we pass this variable as expression to following switch statement, value of dayOfWeek is tested with multiple cases to identify the block of code to be executed.

When we run the above swift program, will see the following output –

Output:-

swift_switch_case_statement