Logo
Pattern

Discover published sets by community

Explore tens of thousands of sets crafted by our community.

Swift Control Structures

20

Flashcards

0/20

Still learning
StarStarStarStar

if

StarStarStarStar

Executes a set of statements only if a condition is true. Example: if x > 0 { print("x is positive.") }

StarStarStarStar

fallthrough

StarStarStarStar

In a 'switch' case, allows execution to fall through to the next case. Example: switch number { case 1: print("One") fallthrough case 2: print("Two") default: print("Other number") }

StarStarStarStar

return

StarStarStarStar

Exits the current function and possibly provides a value to the function caller. Example: func double(value: Int) -> Int { return value * 2 }

StarStarStarStar

do

StarStarStarStar

Used with 'catch' to handle errors, and with 'try' to mark a block of code that can potentially throw an error. Example: do { try canThrowAnError() } catch { // handle the error }

StarStarStarStar

else

StarStarStarStar

Executes a set of statements if the if condition is false. Example: if x > 0 { print("x is positive.") } else { print("x is not positive.") }

StarStarStarStar

default

StarStarStarStar

Covers any case not explicitly mentioned in a 'switch' statement's cases. Example: switch grade { case "A": print("Excellent") case "B": print("Good") default: print("Other grade") }

StarStarStarStar

defer

StarStarStarStar

Schedules a block of code to be executed just before the current scope exits. Example: defer { // clean up code, executed when the current scope is exited } // rest of the code

StarStarStarStar

where

StarStarStarStar

Adds a condition that list items must meet to be included in the loop. Example: for number in 1...50 where number % 2 == 0 { print(number) // prints even numbers }

StarStarStarStar

break

StarStarStarStar

Immediately exits a loop or a 'switch' case. Example: for i in 1...10 { if i == 5 { break } print(i) }

StarStarStarStar

switch

StarStarStarStar

Executes different codes blocks based on a value's match with a case. Example: switch grade { case "A": print("Excellent") case "B": print("Good") default: print("Other grade") }

StarStarStarStar

guard

StarStarStarStar

Allows early exit from a function if a condition is not met. Example: guard let name = person["name"] else { return } // Use 'name' safely after the guard statement

StarStarStarStar

for-in

StarStarStarStar

Iterates over a sequence, such as an array or a range. Example: for number in 1...5 { print(number) }

StarStarStarStar

while

StarStarStarStar

Repeats a block of code as long as a condition is true. Example: while x < 5 { x += 1 }

StarStarStarStar

continue

StarStarStarStar

Skips the current loop iteration and proceeds with the next one. Example: for i in 1...10 { if i % 2 == 0 { continue } print(i) // prints odd numbers }

StarStarStarStar

throw

StarStarStarStar

Used to signal that an error has occurred and propagate it to a catch block. Example: func canThrowAnError() throws { // code that can throw an error throw MyError.someError }

StarStarStarStar

case

StarStarStarStar

Defines a match pattern inside a 'switch' statement. Example: switch grade { case "A": print("Excellent") case "B": print("Good") default: print("Other grade") }

StarStarStarStar

else if

StarStarStarStar

Provides a secondary condition to an 'if' statement. Example: if x > 0 { print("x is positive.") } else if x < 0 { print("x is negative.") }

StarStarStarStar

repeat-while

StarStarStarStar

Similar to 'while', but the condition is checked after the loop body has been executed. Example: repeat { x += 1 } while x < 5

StarStarStarStar

catch

StarStarStarStar

Defines a block of code for handling errors thrown within the preceding 'do' block. Example: do { try canThrowAnError() } catch MyError.someError { // handle the specific error } catch { // handle any other errors }

StarStarStarStar

inout

StarStarStarStar

Indicates that a parameter can be modified by a function and the changes will be reflected outside the function. Example: func swapTwoInts(_ a: inout Int, _ b: inout Int) { let temporaryA = a a = b b = temporaryA }

Know
0
Still learning
Click to flip
Know
0
Logo

© Hypatia.Tech. 2024 All rights reserved.