Explore tens of thousands of sets crafted by our community.
Swift Control Structures
20
Flashcards
0/20
if
Executes a set of statements only if a condition is true. Example: if x > 0 { print("x is positive.") }
fallthrough
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") }
return
Exits the current function and possibly provides a value to the function caller. Example: func double(value: Int) -> Int { return value * 2 }
do
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 }
else
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.") }
default
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") }
defer
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
where
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 }
break
Immediately exits a loop or a 'switch' case. Example: for i in 1...10 { if i == 5 { break } print(i) }
switch
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") }
guard
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
for-in
Iterates over a sequence, such as an array or a range. Example: for number in 1...5 { print(number) }
while
Repeats a block of code as long as a condition is true. Example: while x < 5 { x += 1 }
continue
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 }
throw
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 }
case
Defines a match pattern inside a 'switch' statement. Example: switch grade { case "A": print("Excellent") case "B": print("Good") default: print("Other grade") }
else if
Provides a secondary condition to an 'if' statement. Example: if x > 0 { print("x is positive.") } else if x < 0 { print("x is negative.") }
repeat-while
Similar to 'while', but the condition is checked after the loop body has been executed. Example: repeat { x += 1 } while x < 5
catch
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 }
inout
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 }
© Hypatia.Tech. 2024 All rights reserved.