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

Control structures IN SWIFT

  • 1.
    Control Structures inSwift > 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 • IfStatement • If Else Statement • If Else If Statement • Nested If Statement • Switch Statement
  • 3.
    If Statements • Indicatesdecision 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..
  • 4.
  • 5.
    IF Statement Example vargrade = 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 }
  • 7.
  • 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 StatementExample let grade = 66 if grade>=60 { print(“You are passed.”) } else { print(“You are Failed.”) } Result You are passed.
  • 10.
    IF ELSE IFStatement • 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}
  • 12.
    IF ELSE IFStatement 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 } }
  • 15.
    NESTED IF Statement varattendance= 80 var grade=66 if attendance>= 75 { if ( grade >= 60 ) { print(“You are Passed“); } }
  • 16.
    Switch Statement • Containsmultiple 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 • ForIn 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 LoopExample 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 LoopExample 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 LoopExample 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 LoopExample 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 LoopExample • 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 LoopExample 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 • IndicatesIteration/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 vari = 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 LoopExample 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 • BreakStatement • Continue Statement • Fallthrough Statement
  • 32.
    Break Statement • Stopsthe 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 vari = 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 • Skipthe 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 varcnt = 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 • Bydefault 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 varc = 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.
  • 39.
    1. Find theoutput 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 theoutput 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 theoutput 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 theoutput 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 theoutput for i in 1...3 { print(i,terminator:"") i = i + 2 } a. Error b. 13 c. 1 d. None of these
  • 44.
    3. Find theoutput for i in 1...3 { print(i,terminator:"") i = i + 2 } a. Error b. 13 c. 1 d. None of these
  • 45.
    4. Find theoutput 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 theoutput for i in 1...3 { print(i,terminator:"") }; print(2,terminator:“1") a. Error b. 1232 c. 12321 d. None of these