SlideShare a Scribd company logo
Control Structures in Swift
> If Statement
> If Else Statement
> If Else If Statement
> Nested If Statement
> Switch Statement
Branching/
Selection
Statements
Looping/
Iteration
Statements
Jumping/
Jump
Statements
> For In Loop
> While Loop
> Repeat While Loop
> Break
> Continue
> Fallthrough
Control
Structures
Branching/Selection Statements
• If Statement
• If Else Statement
• If Else If Statement
• Nested If Statement
• Switch Statement
If Statements
• Indicates decision is to be made
• Contains an expression that can be true or false
• Performs action only if the condition is true
• Single-entry/single-exit
• Syntax:
if expression
{
execute statement if expression is true
}
normal statements after if..
If Statements
IF Statement Example
var grade = 66
if grade>=60
{
print(“You are passed.”)
}
Result
You are passed
IF ELSE Statement
• Indicates decision is to be made
• Contains an expression that can be true or false
• Performs different action if the condition is true and
different action if condition is false
• Single-entry/Two-exit
• Syntax:
if expression
{ execute statement if expression is true }
else
{ execute statement if expression is false }
IF ELSE Statement
IF ELSE Statement
• Indicates decision is to be made
• Contains an expression that can be true or false
• Performs different action if the condition is true and
different action if condition is false
• Single-entry/Two-exit
• Syntax:
if expression
{ execute statement if expression is true }
else
{ execute statement if expression is false }
IF ELSE Statement Example
let grade = 66
if grade>=60
{
print(“You are passed.”)
}
else
{
print(“You are Failed.”)
}
Result
You are passed.
IF ELSE IF Statement
• Indicates decision after another decision is to be
made(i.e. Multiple Cases)
• Contains an expression that can be true or false
• Performs again expression check if the condition is
false
• Syntax:
if expression
{ execute statement if expression is true }
else if expression
{ execute statement else if expression is true}
IF ELSE IF Statement
var medical = true
var attendance = 70
if attendance>=75
{
print("Eligible for exam")
}
else if medical == true
{
print("Eligible for exam with medical")
}
NESTED IF Statement
• If inside another If
• Indicates decision after another decision is to be
made
• Performs again expression check if the condition is
true
• Syntax:
if expression
{ if expression
{ execute statement if both if expressions are true }
}
NESTED IF Statement
var attendance= 80
var grade=66
if attendance>= 75
{
if ( grade >= 60 )
{
print(“You are Passed“);
}
}
Switch Statement
• Contains multiple cases(Multiple Branching
Statement)
• Single-entry/Multiple-exit
• Syntax:
switch literal
{ case value1:
execute statement if case1 matches
case value2,value3:
execute statement if case2 matches
default: execute if none case matches }
SWITCH Statement
var c = 3
switch c
{ case 1:
print("You got 1 diamond")
case 2:
print("You got 2 diamonds")
case 3:
print("You got 3 diamonds")
default:
print("Nothing here") }
SWITCH Statement
var c = 3
switch c
{ case 1,2:
print("You got 5 diamond")
case 3,4:
print("You got 10 diamonds")
case 5,6:
print("You got 15 diamonds")
default:
print("Nothing here") }
Note: switch does not
require explicit break
Looping/Iteration Statements
• For In Loop
• While Loop
• Do While Loop(now. Repeat While)
For In Loop
• Indicates Iteration/Repetition of statements
• It iterates over a sequence, such as ranges of
numbers or items in an array.
• Syntax:
for index in variable
{
statement to be executed..
}
For In Loop Example
var arr:[Int] = [10, 20, 30, 40]
for i in arr
{
print("Value of i is (i)")
} Result
Value of i is 10
Value of i is 20
Value of i is 30
Value of i is 40
For In Loop Example
for i in 1...5
{
print("(i) times 5 is (i * 5)")
} Result
1 times 5 is 5
2 times 5 is 10
3 times 5 is 15
4 times 5 is 20
5 times 5 is 25
Closed range operator
For In Loop Example
for i in 1..<5
{
print("(i) times 5 is (i * 5)")
} Result
1 times 5 is 5
2 times 5 is 10
3 times 5 is 15
4 times 5 is 20
Half Open range operator
For In Loop Example
let base = 3
let power = 4
var answer = 1
for i in 1...power
{
answer = answer * base
}
print("(base)'s power (power) is (answer)")
Result
3's power 4 is 81
For In Loop Example
• If you don’t need each value from a sequence, you
can ignore the values by using an underscore in place
of a variable name.
• Syntax:
for _ in variable
{
statement to be executed..
}
For In Loop Example
let base = 3
let power = 4
var answer = 1
for _ in 1...power
{
answer = answer * base
}
print("(base)'s power (power) is (answer)")
Result
3's power 4 is 81
While Loop
• Indicates Iteration/Repetition of statements
• Contains an expression that should be true in order
to perform actions repeatedly
• Keep on performing action till the condition is true.
• Syntax:
while expression
{
statement to be executed..
}
While Loop Example
var i = 1
while i<5
{
print("Value of i is (i)")
i = i+1
}
Result
Value of i is 1
Value of i is 2
Value of i is 3
Value of i is 4
(Do)Repeat While Loop
• Indicates Iteration/Repetition of statements
• Perform Operation and then check(Exit-Controlled)
• Perform action at least once even if condition is
false.
• Do while in swift is replaced by repeat while.
• Syntax:
repeat
{ statement to be executed.. } while expression
Repeat While Loop Example
var i = 1
repeat
{
print("Value of i is (i)")
i = i+1
} while i<5
Result
Value of i is 1
Value of i is 2
Value of i is 3
Value of i is 4
Jumping/Jump Statements
• Break Statement
• Continue Statement
• Fallthrough Statement
Break Statement
• Stops the execution of the Loop.
• Allows to exit from current Iteration.
• Mostly used with branching statements.
• Syntax:
loop
{ statement to be executed..
if expression
{ break }
}
Break Statement Example
var i = 1
while true
{
print("Value of i is (i)")
i = i+1
if i==5
{ break }
}
Result
Value of i is 1
Value of i is 2
Value of i is 3
Value of i is 4
Continue Statement
• Skip the current iteration.
• Prevent statements to be executed once only.
• Mostly used with branching statements.
• Syntax:
loop
{ statement to be executed..
if expression
{ continue }
}
Continue Statement Example
var cnt = 0
for i in 1...5
{
if i==2
{
continue
}
print("Start (i)")
}
Result
Start 1
Start 3
Start 4
Start 5
Fallthrough Statement
• By default you don’t need to mention break
explicitly while using switch cases.
• But if you need to prevent default break behavior,
you can use fallthrough statement.
• Syntax:
switch literal
{ case value1:
execute statement if case1 matches
fallthrough
default: execute if none case matches }
Fallthrough Statement Example
var c = 1
switch c
{ case 1:
print("You got 1 diamond")
fallthrough
case 2:
print("You got 2 diamonds")
fallthrough
default:
print("Nothing here") }
Result
You got 1 diamond
You got 2 diamonds
Nothing here
MCQs
1. Find the output
var i=2
var j=1
while j=i ? true : false
{ print(i) }
a. Error
b. 1
c. 2
d. None of these
1. Find the output
var i=2
var j=1
while j=i ? true : false
{ print(i) }
a. Error
b. 1
c. 2
d. None of these
2. Find the output
var cnt = 0
for i in 2...3
{
print(i,terminator:"")
if i==2
{ continue }
else
{ print(i,terminator:""); continue }
}
a. Error
b. 223
c. 233
d. None of these
2. Find the output
var cnt = 0
for i in 2...3
{
print(i,terminator:"")
if i==2
{ continue }
else
{ print(i,terminator:""); continue }
}
a. Error
b. 223
c. 233
d. None of these
3. Find the output
for i in 1...3
{
print(i,terminator:"")
i = i + 2
}
a. Error
b. 13
c. 1
d. None of these
3. Find the output
for i in 1...3
{
print(i,terminator:"")
i = i + 2
}
a. Error
b. 13
c. 1
d. None of these
4. Find the output
for i in 1...3
{
print(i,terminator:"")
};
print(2,terminator:“1")
a. Error
b. 1232
c. 12321
d. None of these
4. Find the output
for i in 1...3
{
print(i,terminator:"")
};
print(2,terminator:“1")
a. Error
b. 1232
c. 12321
d. None of these

More Related Content

What's hot

Introduction to java
Introduction to javaIntroduction to java
Introduction to java
Ajay Sharma
 
Interface in java
Interface in javaInterface in java
Interface in java
Kavitha713564
 
Control flow statements in java
Control flow statements in javaControl flow statements in java
Control flow statements in java
yugandhar vadlamudi
 
Java Course 8: I/O, Files and Streams
Java Course 8: I/O, Files and StreamsJava Course 8: I/O, Files and Streams
Java Course 8: I/O, Files and Streams
Anton Keks
 
C++ concept of Polymorphism
C++ concept of  PolymorphismC++ concept of  Polymorphism
C++ concept of Polymorphism
kiran Patel
 
Introduction to method overloading &amp; method overriding in java hdm
Introduction to method overloading &amp; method overriding  in java  hdmIntroduction to method overloading &amp; method overriding  in java  hdm
Introduction to method overloading &amp; method overriding in java hdm
Harshal Misalkar
 
Java operators
Java operatorsJava operators
Java operators
Shehrevar Davierwala
 
Java programming lab manual
Java programming lab manualJava programming lab manual
Java programming lab manual
sameer farooq
 
Learn C# Programming - Encapsulation & Methods
Learn C# Programming - Encapsulation & MethodsLearn C# Programming - Encapsulation & Methods
Learn C# Programming - Encapsulation & Methods
Eng Teong Cheah
 
Loops c++
Loops c++Loops c++
Loops c++
Shivani Singh
 
Class and Objects in Java
Class and Objects in JavaClass and Objects in Java
Class and Objects in Java
Spotle.ai
 
Chapter 2 Java Methods
Chapter 2 Java MethodsChapter 2 Java Methods
Chapter 2 Java Methods
Khirulnizam Abd Rahman
 
Spring Data JPA
Spring Data JPASpring Data JPA
Spring Data JPA
Cheng Ta Yeh
 
Java OOPS Concept
Java OOPS ConceptJava OOPS Concept
Java OOPS Concept
Richa Gupta
 
Methods in java
Methods in javaMethods in java
Methods in java
chauhankapil
 
Static and Dynamic polymorphism in C++
Static and Dynamic polymorphism in C++Static and Dynamic polymorphism in C++
Static and Dynamic polymorphism in C++
Anil Bapat
 
Access specifiers(modifiers) in java
Access specifiers(modifiers) in javaAccess specifiers(modifiers) in java
Access specifiers(modifiers) in java
HrithikShinde
 
Method overriding
Method overridingMethod overriding
Method overriding
Azaz Maverick
 
Packages in java
Packages in javaPackages in java
Packages in java
jamunaashok
 
Javapolymorphism
JavapolymorphismJavapolymorphism
Javapolymorphism
karthikenlume
 

What's hot (20)

Introduction to java
Introduction to javaIntroduction to java
Introduction to java
 
Interface in java
Interface in javaInterface in java
Interface in java
 
Control flow statements in java
Control flow statements in javaControl flow statements in java
Control flow statements in java
 
Java Course 8: I/O, Files and Streams
Java Course 8: I/O, Files and StreamsJava Course 8: I/O, Files and Streams
Java Course 8: I/O, Files and Streams
 
C++ concept of Polymorphism
C++ concept of  PolymorphismC++ concept of  Polymorphism
C++ concept of Polymorphism
 
Introduction to method overloading &amp; method overriding in java hdm
Introduction to method overloading &amp; method overriding  in java  hdmIntroduction to method overloading &amp; method overriding  in java  hdm
Introduction to method overloading &amp; method overriding in java hdm
 
Java operators
Java operatorsJava operators
Java operators
 
Java programming lab manual
Java programming lab manualJava programming lab manual
Java programming lab manual
 
Learn C# Programming - Encapsulation & Methods
Learn C# Programming - Encapsulation & MethodsLearn C# Programming - Encapsulation & Methods
Learn C# Programming - Encapsulation & Methods
 
Loops c++
Loops c++Loops c++
Loops c++
 
Class and Objects in Java
Class and Objects in JavaClass and Objects in Java
Class and Objects in Java
 
Chapter 2 Java Methods
Chapter 2 Java MethodsChapter 2 Java Methods
Chapter 2 Java Methods
 
Spring Data JPA
Spring Data JPASpring Data JPA
Spring Data JPA
 
Java OOPS Concept
Java OOPS ConceptJava OOPS Concept
Java OOPS Concept
 
Methods in java
Methods in javaMethods in java
Methods in java
 
Static and Dynamic polymorphism in C++
Static and Dynamic polymorphism in C++Static and Dynamic polymorphism in C++
Static and Dynamic polymorphism in C++
 
Access specifiers(modifiers) in java
Access specifiers(modifiers) in javaAccess specifiers(modifiers) in java
Access specifiers(modifiers) in java
 
Method overriding
Method overridingMethod overriding
Method overriding
 
Packages in java
Packages in javaPackages in java
Packages in java
 
Javapolymorphism
JavapolymorphismJavapolymorphism
Javapolymorphism
 

Similar to Control structures IN SWIFT

Mesics lecture 6 control statement = if -else if__else
Mesics lecture 6   control statement = if -else if__elseMesics lecture 6   control statement = if -else if__else
Mesics lecture 6 control statement = if -else if__else
eShikshak
 
Control structures in C
Control structures in CControl structures in C
cpu.pdf
cpu.pdfcpu.pdf
While , For , Do-While Loop
While , For , Do-While LoopWhile , For , Do-While Loop
While , For , Do-While Loop
Abhishek Choksi
 
Flow control in c++
Flow control in c++Flow control in c++
Flow control in c++
Subhasis Nayak
 
nuts and bolts of c++
nuts and bolts of c++nuts and bolts of c++
nuts and bolts of c++
guestfb6ada
 
03 conditions loops
03   conditions loops03   conditions loops
03 conditions loops
Manzoor ALam
 
Mesics lecture 7 iteration and repetitive executions
Mesics lecture 7   iteration and repetitive executionsMesics lecture 7   iteration and repetitive executions
Mesics lecture 7 iteration and repetitive executions
eShikshak
 
C++ problem solving operators ( conditional operators,logical operators, swit...
C++ problem solving operators ( conditional operators,logical operators, swit...C++ problem solving operators ( conditional operators,logical operators, swit...
C++ problem solving operators ( conditional operators,logical operators, swit...
mshakeel44514451
 
Flow of control C ++ By TANUJ
Flow of control C ++ By TANUJFlow of control C ++ By TANUJ
Flow of control C ++ By TANUJ
TANUJ ⠀
 
Introducing Swift v2.1
Introducing Swift v2.1Introducing Swift v2.1
Introducing Swift v2.1
Abhishek Dwivedi
 
Programming Fundamentals in C++ structures
Programming Fundamentals in  C++ structuresProgramming Fundamentals in  C++ structures
Programming Fundamentals in C++ structures
ayshasafdarwaada
 
Loops and conditional statements
Loops and conditional statementsLoops and conditional statements
Loops and conditional statements
Saad Sheikh
 
Control statements anil
Control statements anilControl statements anil
Control statements anil
Anil Dutt
 
Java chapter 3
Java chapter 3Java chapter 3
Java chapter 3
Abdii Rashid
 
06-Control-Statementskkkkkkkkkkkkkk.pptx
06-Control-Statementskkkkkkkkkkkkkk.pptx06-Control-Statementskkkkkkkkkkkkkk.pptx
06-Control-Statementskkkkkkkkkkkkkk.pptx
kamalsmail1
 
07 flow control
07   flow control07   flow control
07 flow control
dhrubo kayal
 
C statements
C statementsC statements
C statements
Ahsann111
 
Loop and while Loop
Loop and while LoopLoop and while Loop
Loop and while Loop
JayBhavsar68
 
CONTROL STRUCTURE IN VB
CONTROL STRUCTURE IN VBCONTROL STRUCTURE IN VB
CONTROL STRUCTURE IN VB
classall
 

Similar to Control structures IN SWIFT (20)

Mesics lecture 6 control statement = if -else if__else
Mesics lecture 6   control statement = if -else if__elseMesics lecture 6   control statement = if -else if__else
Mesics lecture 6 control statement = if -else if__else
 
Control structures in C
Control structures in CControl structures in C
Control structures in C
 
cpu.pdf
cpu.pdfcpu.pdf
cpu.pdf
 
While , For , Do-While Loop
While , For , Do-While LoopWhile , For , Do-While Loop
While , For , Do-While Loop
 
Flow control in c++
Flow control in c++Flow control in c++
Flow control in c++
 
nuts and bolts of c++
nuts and bolts of c++nuts and bolts of c++
nuts and bolts of c++
 
03 conditions loops
03   conditions loops03   conditions loops
03 conditions loops
 
Mesics lecture 7 iteration and repetitive executions
Mesics lecture 7   iteration and repetitive executionsMesics lecture 7   iteration and repetitive executions
Mesics lecture 7 iteration and repetitive executions
 
C++ problem solving operators ( conditional operators,logical operators, swit...
C++ problem solving operators ( conditional operators,logical operators, swit...C++ problem solving operators ( conditional operators,logical operators, swit...
C++ problem solving operators ( conditional operators,logical operators, swit...
 
Flow of control C ++ By TANUJ
Flow of control C ++ By TANUJFlow of control C ++ By TANUJ
Flow of control C ++ By TANUJ
 
Introducing Swift v2.1
Introducing Swift v2.1Introducing Swift v2.1
Introducing Swift v2.1
 
Programming Fundamentals in C++ structures
Programming Fundamentals in  C++ structuresProgramming Fundamentals in  C++ structures
Programming Fundamentals in C++ structures
 
Loops and conditional statements
Loops and conditional statementsLoops and conditional statements
Loops and conditional statements
 
Control statements anil
Control statements anilControl statements anil
Control statements anil
 
Java chapter 3
Java chapter 3Java chapter 3
Java chapter 3
 
06-Control-Statementskkkkkkkkkkkkkk.pptx
06-Control-Statementskkkkkkkkkkkkkk.pptx06-Control-Statementskkkkkkkkkkkkkk.pptx
06-Control-Statementskkkkkkkkkkkkkk.pptx
 
07 flow control
07   flow control07   flow control
07 flow control
 
C statements
C statementsC statements
C statements
 
Loop and while Loop
Loop and while LoopLoop and while Loop
Loop and while Loop
 
CONTROL STRUCTURE IN VB
CONTROL STRUCTURE IN VBCONTROL STRUCTURE IN VB
CONTROL STRUCTURE IN VB
 

More from LOVELY PROFESSIONAL UNIVERSITY

Enumerations, structure and class IN SWIFT
Enumerations, structure and class IN SWIFTEnumerations, structure and class IN SWIFT
Enumerations, structure and class IN SWIFT
LOVELY PROFESSIONAL UNIVERSITY
 
Dictionaries IN SWIFT
Dictionaries IN SWIFTDictionaries IN SWIFT
Dictionaries IN SWIFT
LOVELY PROFESSIONAL UNIVERSITY
 
Arrays and its properties IN SWIFT
Arrays and its properties IN SWIFTArrays and its properties IN SWIFT
Arrays and its properties IN SWIFT
LOVELY PROFESSIONAL UNIVERSITY
 
Array and its functionsI SWIFT
Array and its functionsI SWIFTArray and its functionsI SWIFT
Array and its functionsI SWIFT
LOVELY PROFESSIONAL UNIVERSITY
 
practice problems on array IN SWIFT
practice problems on array IN SWIFTpractice problems on array IN SWIFT
practice problems on array IN SWIFT
LOVELY PROFESSIONAL UNIVERSITY
 
practice problems on array IN SWIFT
practice problems on array  IN SWIFTpractice problems on array  IN SWIFT
practice problems on array IN SWIFT
LOVELY PROFESSIONAL UNIVERSITY
 
practice problems on array IN SWIFT
practice problems on array IN SWIFTpractice problems on array IN SWIFT
practice problems on array IN SWIFT
LOVELY PROFESSIONAL UNIVERSITY
 
practice problems on functions IN SWIFT
practice problems on functions IN SWIFTpractice problems on functions IN SWIFT
practice problems on functions IN SWIFT
LOVELY PROFESSIONAL UNIVERSITY
 
10. funtions and closures IN SWIFT PROGRAMMING
10. funtions and closures IN SWIFT PROGRAMMING10. funtions and closures IN SWIFT PROGRAMMING
10. funtions and closures IN SWIFT PROGRAMMING
LOVELY PROFESSIONAL UNIVERSITY
 
Variables and data types IN SWIFT
 Variables and data types IN SWIFT Variables and data types IN SWIFT
Variables and data types IN SWIFT
LOVELY PROFESSIONAL UNIVERSITY
 
Soft skills. pptx
Soft skills. pptxSoft skills. pptx
JAVA
JAVAJAVA
Unit 5
Unit 5Unit 5
Unit 4
Unit 4Unit 4
Unit 3
Unit 3Unit 3
STRINGS IN JAVA
STRINGS IN JAVASTRINGS IN JAVA
Unit 1
Unit 1Unit 1
COMPLETE CORE JAVA
COMPLETE CORE JAVACOMPLETE CORE JAVA
COMPLETE CORE JAVA
LOVELY PROFESSIONAL UNIVERSITY
 
Data wrangling IN R LANGUAGE
Data wrangling IN R LANGUAGEData wrangling IN R LANGUAGE
Data wrangling IN R LANGUAGE
LOVELY PROFESSIONAL UNIVERSITY
 

More from LOVELY PROFESSIONAL UNIVERSITY (19)

Enumerations, structure and class IN SWIFT
Enumerations, structure and class IN SWIFTEnumerations, structure and class IN SWIFT
Enumerations, structure and class IN SWIFT
 
Dictionaries IN SWIFT
Dictionaries IN SWIFTDictionaries IN SWIFT
Dictionaries IN SWIFT
 
Arrays and its properties IN SWIFT
Arrays and its properties IN SWIFTArrays and its properties IN SWIFT
Arrays and its properties IN SWIFT
 
Array and its functionsI SWIFT
Array and its functionsI SWIFTArray and its functionsI SWIFT
Array and its functionsI SWIFT
 
practice problems on array IN SWIFT
practice problems on array IN SWIFTpractice problems on array IN SWIFT
practice problems on array IN SWIFT
 
practice problems on array IN SWIFT
practice problems on array  IN SWIFTpractice problems on array  IN SWIFT
practice problems on array IN SWIFT
 
practice problems on array IN SWIFT
practice problems on array IN SWIFTpractice problems on array IN SWIFT
practice problems on array IN SWIFT
 
practice problems on functions IN SWIFT
practice problems on functions IN SWIFTpractice problems on functions IN SWIFT
practice problems on functions IN SWIFT
 
10. funtions and closures IN SWIFT PROGRAMMING
10. funtions and closures IN SWIFT PROGRAMMING10. funtions and closures IN SWIFT PROGRAMMING
10. funtions and closures IN SWIFT PROGRAMMING
 
Variables and data types IN SWIFT
 Variables and data types IN SWIFT Variables and data types IN SWIFT
Variables and data types IN SWIFT
 
Soft skills. pptx
Soft skills. pptxSoft skills. pptx
Soft skills. pptx
 
JAVA
JAVAJAVA
JAVA
 
Unit 5
Unit 5Unit 5
Unit 5
 
Unit 4
Unit 4Unit 4
Unit 4
 
Unit 3
Unit 3Unit 3
Unit 3
 
STRINGS IN JAVA
STRINGS IN JAVASTRINGS IN JAVA
STRINGS IN JAVA
 
Unit 1
Unit 1Unit 1
Unit 1
 
COMPLETE CORE JAVA
COMPLETE CORE JAVACOMPLETE CORE JAVA
COMPLETE CORE JAVA
 
Data wrangling IN R LANGUAGE
Data wrangling IN R LANGUAGEData wrangling IN R LANGUAGE
Data wrangling IN R LANGUAGE
 

Recently uploaded

The basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptxThe basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptx
heathfieldcps1
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
EverAndrsGuerraGuerr
 
Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.
Ashokrao Mane college of Pharmacy Peth-Vadgaon
 
A Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdfA Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdf
Jean Carlos Nunes Paixão
 
MATATAG CURRICULUM: ASSESSING THE READINESS OF ELEM. PUBLIC SCHOOL TEACHERS I...
MATATAG CURRICULUM: ASSESSING THE READINESS OF ELEM. PUBLIC SCHOOL TEACHERS I...MATATAG CURRICULUM: ASSESSING THE READINESS OF ELEM. PUBLIC SCHOOL TEACHERS I...
MATATAG CURRICULUM: ASSESSING THE READINESS OF ELEM. PUBLIC SCHOOL TEACHERS I...
NelTorrente
 
South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)
Academy of Science of South Africa
 
DRUGS AND ITS classification slide share
DRUGS AND ITS classification slide shareDRUGS AND ITS classification slide share
DRUGS AND ITS classification slide share
taiba qazi
 
The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
heathfieldcps1
 
Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
TechSoup
 
Advantages and Disadvantages of CMS from an SEO Perspective
Advantages and Disadvantages of CMS from an SEO PerspectiveAdvantages and Disadvantages of CMS from an SEO Perspective
Advantages and Disadvantages of CMS from an SEO Perspective
Krisztián Száraz
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
Peter Windle
 
Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
Jean Carlos Nunes Paixão
 
The History of Stoke Newington Street Names
The History of Stoke Newington Street NamesThe History of Stoke Newington Street Names
The History of Stoke Newington Street Names
History of Stoke Newington
 
clinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdfclinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdf
Priyankaranawat4
 
How to Fix the Import Error in the Odoo 17
How to Fix the Import Error in the Odoo 17How to Fix the Import Error in the Odoo 17
How to Fix the Import Error in the Odoo 17
Celine George
 
Group Presentation 2 Economics.Ariana Buscigliopptx
Group Presentation 2 Economics.Ariana BuscigliopptxGroup Presentation 2 Economics.Ariana Buscigliopptx
Group Presentation 2 Economics.Ariana Buscigliopptx
ArianaBusciglio
 
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama UniversityNatural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Akanksha trivedi rama nursing college kanpur.
 
Liberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdfLiberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdf
WaniBasim
 
Digital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental DesignDigital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental Design
amberjdewit93
 
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
National Information Standards Organization (NISO)
 

Recently uploaded (20)

The basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptxThe basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptx
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
 
Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.
 
A Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdfA Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdf
 
MATATAG CURRICULUM: ASSESSING THE READINESS OF ELEM. PUBLIC SCHOOL TEACHERS I...
MATATAG CURRICULUM: ASSESSING THE READINESS OF ELEM. PUBLIC SCHOOL TEACHERS I...MATATAG CURRICULUM: ASSESSING THE READINESS OF ELEM. PUBLIC SCHOOL TEACHERS I...
MATATAG CURRICULUM: ASSESSING THE READINESS OF ELEM. PUBLIC SCHOOL TEACHERS I...
 
South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)
 
DRUGS AND ITS classification slide share
DRUGS AND ITS classification slide shareDRUGS AND ITS classification slide share
DRUGS AND ITS classification slide share
 
The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
 
Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
 
Advantages and Disadvantages of CMS from an SEO Perspective
Advantages and Disadvantages of CMS from an SEO PerspectiveAdvantages and Disadvantages of CMS from an SEO Perspective
Advantages and Disadvantages of CMS from an SEO Perspective
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
 
Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
 
The History of Stoke Newington Street Names
The History of Stoke Newington Street NamesThe History of Stoke Newington Street Names
The History of Stoke Newington Street Names
 
clinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdfclinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdf
 
How to Fix the Import Error in the Odoo 17
How to Fix the Import Error in the Odoo 17How to Fix the Import Error in the Odoo 17
How to Fix the Import Error in the Odoo 17
 
Group Presentation 2 Economics.Ariana Buscigliopptx
Group Presentation 2 Economics.Ariana BuscigliopptxGroup Presentation 2 Economics.Ariana Buscigliopptx
Group Presentation 2 Economics.Ariana Buscigliopptx
 
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama UniversityNatural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
 
Liberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdfLiberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdf
 
Digital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental DesignDigital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental Design
 
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
 

Control structures IN SWIFT

  • 1. Control Structures in Swift > If Statement > If Else Statement > If Else If Statement > Nested If Statement > Switch Statement Branching/ Selection Statements Looping/ Iteration Statements Jumping/ Jump Statements > For In Loop > While Loop > Repeat While Loop > Break > Continue > Fallthrough Control Structures
  • 2. Branching/Selection Statements • If Statement • If Else Statement • If Else If Statement • Nested If Statement • Switch Statement
  • 3. If Statements • Indicates decision is to be made • Contains an expression that can be true or false • Performs action only if the condition is true • Single-entry/single-exit • Syntax: if expression { execute statement if expression is true } normal statements after if..
  • 5. IF Statement Example var grade = 66 if grade>=60 { print(“You are passed.”) } Result You are passed
  • 6. IF ELSE Statement • Indicates decision is to be made • Contains an expression that can be true or false • Performs different action if the condition is true and different action if condition is false • Single-entry/Two-exit • Syntax: if expression { execute statement if expression is true } else { execute statement if expression is false }
  • 8. IF ELSE Statement • Indicates decision is to be made • Contains an expression that can be true or false • Performs different action if the condition is true and different action if condition is false • Single-entry/Two-exit • Syntax: if expression { execute statement if expression is true } else { execute statement if expression is false }
  • 9. IF ELSE Statement Example let grade = 66 if grade>=60 { print(“You are passed.”) } else { print(“You are Failed.”) } Result You are passed.
  • 10. IF ELSE IF Statement • Indicates decision after another decision is to be made(i.e. Multiple Cases) • Contains an expression that can be true or false • Performs again expression check if the condition is false • Syntax: if expression { execute statement if expression is true } else if expression { execute statement else if expression is true}
  • 11.
  • 12. IF ELSE IF Statement var medical = true var attendance = 70 if attendance>=75 { print("Eligible for exam") } else if medical == true { print("Eligible for exam with medical") }
  • 13. NESTED IF Statement • If inside another If • Indicates decision after another decision is to be made • Performs again expression check if the condition is true • Syntax: if expression { if expression { execute statement if both if expressions are true } }
  • 14.
  • 15. NESTED IF Statement var attendance= 80 var grade=66 if attendance>= 75 { if ( grade >= 60 ) { print(“You are Passed“); } }
  • 16. Switch Statement • Contains multiple cases(Multiple Branching Statement) • Single-entry/Multiple-exit • Syntax: switch literal { case value1: execute statement if case1 matches case value2,value3: execute statement if case2 matches default: execute if none case matches }
  • 17. SWITCH Statement var c = 3 switch c { case 1: print("You got 1 diamond") case 2: print("You got 2 diamonds") case 3: print("You got 3 diamonds") default: print("Nothing here") }
  • 18. SWITCH Statement var c = 3 switch c { case 1,2: print("You got 5 diamond") case 3,4: print("You got 10 diamonds") case 5,6: print("You got 15 diamonds") default: print("Nothing here") } Note: switch does not require explicit break
  • 19. Looping/Iteration Statements • For In Loop • While Loop • Do While Loop(now. Repeat While)
  • 20. For In Loop • Indicates Iteration/Repetition of statements • It iterates over a sequence, such as ranges of numbers or items in an array. • Syntax: for index in variable { statement to be executed.. }
  • 21. For In Loop Example var arr:[Int] = [10, 20, 30, 40] for i in arr { print("Value of i is (i)") } Result Value of i is 10 Value of i is 20 Value of i is 30 Value of i is 40
  • 22. For In Loop Example for i in 1...5 { print("(i) times 5 is (i * 5)") } Result 1 times 5 is 5 2 times 5 is 10 3 times 5 is 15 4 times 5 is 20 5 times 5 is 25 Closed range operator
  • 23. For In Loop Example for i in 1..<5 { print("(i) times 5 is (i * 5)") } Result 1 times 5 is 5 2 times 5 is 10 3 times 5 is 15 4 times 5 is 20 Half Open range operator
  • 24. For In Loop Example let base = 3 let power = 4 var answer = 1 for i in 1...power { answer = answer * base } print("(base)'s power (power) is (answer)") Result 3's power 4 is 81
  • 25. For In Loop Example • If you don’t need each value from a sequence, you can ignore the values by using an underscore in place of a variable name. • Syntax: for _ in variable { statement to be executed.. }
  • 26. For In Loop Example let base = 3 let power = 4 var answer = 1 for _ in 1...power { answer = answer * base } print("(base)'s power (power) is (answer)") Result 3's power 4 is 81
  • 27. While Loop • Indicates Iteration/Repetition of statements • Contains an expression that should be true in order to perform actions repeatedly • Keep on performing action till the condition is true. • Syntax: while expression { statement to be executed.. }
  • 28. While Loop Example var i = 1 while i<5 { print("Value of i is (i)") i = i+1 } Result Value of i is 1 Value of i is 2 Value of i is 3 Value of i is 4
  • 29. (Do)Repeat While Loop • Indicates Iteration/Repetition of statements • Perform Operation and then check(Exit-Controlled) • Perform action at least once even if condition is false. • Do while in swift is replaced by repeat while. • Syntax: repeat { statement to be executed.. } while expression
  • 30. Repeat While Loop Example var i = 1 repeat { print("Value of i is (i)") i = i+1 } while i<5 Result Value of i is 1 Value of i is 2 Value of i is 3 Value of i is 4
  • 31. Jumping/Jump Statements • Break Statement • Continue Statement • Fallthrough Statement
  • 32. Break Statement • Stops the execution of the Loop. • Allows to exit from current Iteration. • Mostly used with branching statements. • Syntax: loop { statement to be executed.. if expression { break } }
  • 33. Break Statement Example var i = 1 while true { print("Value of i is (i)") i = i+1 if i==5 { break } } Result Value of i is 1 Value of i is 2 Value of i is 3 Value of i is 4
  • 34. Continue Statement • Skip the current iteration. • Prevent statements to be executed once only. • Mostly used with branching statements. • Syntax: loop { statement to be executed.. if expression { continue } }
  • 35. Continue Statement Example var cnt = 0 for i in 1...5 { if i==2 { continue } print("Start (i)") } Result Start 1 Start 3 Start 4 Start 5
  • 36. Fallthrough Statement • By default you don’t need to mention break explicitly while using switch cases. • But if you need to prevent default break behavior, you can use fallthrough statement. • Syntax: switch literal { case value1: execute statement if case1 matches fallthrough default: execute if none case matches }
  • 37. Fallthrough Statement Example var c = 1 switch c { case 1: print("You got 1 diamond") fallthrough case 2: print("You got 2 diamonds") fallthrough default: print("Nothing here") } Result You got 1 diamond You got 2 diamonds Nothing here
  • 38. MCQs
  • 39. 1. Find the output var i=2 var j=1 while j=i ? true : false { print(i) } a. Error b. 1 c. 2 d. None of these
  • 40. 1. Find the output var i=2 var j=1 while j=i ? true : false { print(i) } a. Error b. 1 c. 2 d. None of these
  • 41. 2. Find the output var cnt = 0 for i in 2...3 { print(i,terminator:"") if i==2 { continue } else { print(i,terminator:""); continue } } a. Error b. 223 c. 233 d. None of these
  • 42. 2. Find the output var cnt = 0 for i in 2...3 { print(i,terminator:"") if i==2 { continue } else { print(i,terminator:""); continue } } a. Error b. 223 c. 233 d. None of these
  • 43. 3. Find the output for i in 1...3 { print(i,terminator:"") i = i + 2 } a. Error b. 13 c. 1 d. None of these
  • 44. 3. Find the output for i in 1...3 { print(i,terminator:"") i = i + 2 } a. Error b. 13 c. 1 d. None of these
  • 45. 4. Find the output for i in 1...3 { print(i,terminator:"") }; print(2,terminator:“1") a. Error b. 1232 c. 12321 d. None of these
  • 46. 4. Find the output for i in 1...3 { print(i,terminator:"") }; print(2,terminator:“1") a. Error b. 1232 c. 12321 d. None of these