Swift Variables Strings Array Loop and Functions with Example

In this article, We are going to cover Swift Variables Strings Array Loop and Functions with Examples,

Types of Variables in Swift, Strings in Swift with examples, Arrays in Swift, Loop in Swift with examples, Functions in Swift with examples

Swift Variables Strings Array Loop and Functions with Example

#1: Types of Variables in Swift

Int or UInt − this is used for whole numbers. More specifically, you can use Int32, Int64 to define 32 or 64 bit signed integer, whereas UInt32 or UInt64 to define 32 or 64 bit unsigned integer variables.         

For example 42 and -23.

Float − this is used to represent a 32-bit floating-point number and numbers with smaller decimal points.

For example, 3.14159, 0.1, and -273.158.

Double − this is used to represent a 64-bit floating-point number and used when floating-point values must be very large.

For example, 3.14159, 0.1, and -273.158.

Bool − this represents a Boolean value which is either true or false.

String − this is an ordered collection of characters.

For example, “Hello, World!”

Character − this is a single-character string literal.

For example, “C“

Optional − this represents a variable that can hold either a value or no value.

Tuples − this is used to group multiple values in single Compound Value.


For example 42 

Variable Declaration and Assignment:

var aNumber:Int // uppercase I for Integer!
aNumber = 5 // variable assignment


Constant declaration uses the let keyword

let PI = 3.141592654

#2: Strings in Swift

For taking inputs at run time in swift language, readLine() function is used .In given example, user want to print name on new line, in such a way that user can enter name at runtime. In last line of code \name!  Address of variable name which we have declared in previous line. \n means our output will get printed on new line

Declaring and printing of string:

In given code, string  named as  “Hello, Swift 4”  is declared. Last line is used to print the string


var stringA = "Hello, Swift 4!" 
print( stringA)


Hello, Swift4

Concatenating of string:

Given code represents, concatenation of two string . Here WORLD and HELLO two strings concatenation is used.


let constA = "Hello," 
let constB = "World!" 
var stringA = constA + constB 
print( stringA )



String comparison:

In given code strings are compared according to character used in both strings


var varA = "Hello, Swift 4!" 
var varB = "Hello, World!“
if varA == varB 
print( "\(varA) and \(varB) are equal" ) 
print( "\(varA) and \(varB) are not equal" ) 

Output :


#3: Arrays in Swift with Examples

Array declaration and printing of array:

In swift programming for declaring array let keyword is used.” let array name” is syntax for declaring array . Elements of array are entered in square bracket.

Below code represents array declaration and printing of array in range. For example in given code numbers ranging from 1 to 10 will get printed as an output

Array Declaration in Swift :

var arrayOfIntegers:[Int] = [1,2,3,4,5,6,7,8,9,10]
// print out all the numbers in the array
for(number) in arrayOfIntegers 

Counting of array Elements :

Below code gives output as number of elements given in array.

Array Printing in Swift


var someInts = [Int](count: 3, repeatedValue: 10) 
var someVar = someInts[0]
print( "Value of first element is \(someVar)" ) print( "Value of second element is   \(someInts[1])" )
 print( "Value of third element is \(someInts[2])" 


Value of first element is 10
Value of second element is 10
Value of third element is 10

Dictionaries (Keys and Values) in Swift:


let interestingNumbers = [
    "Prime":[2, 3, 5, 7, 11, 13],
    "Fiboncacci": [1, 1, 2, 3, 5, 8],
    "Square":[1, 4, 9, 16, 25],    
For (kind, numbers) in interestingNumbers{  
   print (kind, numbers)


(Square, [1, 4, 9, 16, 25])
(Fiboncacci, [1, 1, 2, 3, 5, 8])
(Prime, [2, 3, 5, 7, 11, 13])

#4: Loop in Swift with Examples

In the given example of code is printing of numbers from 1 to 9. var i =1  means new variable i is declared which has starting value as 1. Second line is repeat, means we have to execute part/ procedure written under repeat (increment I by 1 in example) while i <10.

#5: Functions in Swift with Examples

Following code denotes defined and use of function.

For example, in following code function named as simple () is defined, which is use to print message “hi how are you”.

In last line function is called because of which procedure written under function will get executed automatically.

This is used for the loops which we are calling multiple times. Instead of calling procedure multiple time if we declare under function no need to repeat whole procedure. Only we need to call function


// function definition
func addTwoInts(numberOne: Int, numberTwo:Int)-> Int 
return numberOne + numberTwo 

Function with parameters:

Here functions are defined along with parameters. In below example function named assimple1 is used for string operation only.

Therefore, in parameters only string is declared


// function call with variables from earlier example
answer = addTwoInts(firstNumber, secondNumber)
print(“The sum of the two numbers is \(answer)”) 
 print("The sum of \(firstNumber) + \(secondNumber) is:\(answer)")

Swift Variables Strings Array Loop and Functions with Examples


We have covered Swift Variables Strings Array Loop and Functions with Examples.

Types of Variables in Swift, Strings in Swift with examples, Arrays in Swift, Loop in Swift with examples, Functions in Swift with examples

Related Articles

iOS Architecture, Concepts and Xcode Installation [4 Steps]

How to Install Node.js on Mac OS [ 2 Steps]

Arrays in swift with examples


Swift official Site

Geeta Atkar

I am Geeta Atkar PhD(Pursuing), ME (Computer Engg), BE (Information Technology) , Assistant Professor, G H Raisoni College of Engg and Management, Pune and Research Scholar, Vellore Institute of Technology, Chennai

2 thoughts on “Swift Variables Strings Array Loop and Functions with Example”

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Share via
Copy link
Powered by Social Snap