3์ผ์ฐจ: 100DaysOfSwift๐Ÿš€

3422 ๋‹จ์–ด 100daysofswiftswiftios100daysofcode

3์ผ์ฐจ



์—ฐ์‚ฐ์ž ๋ฐ ์กฐ๊ฑด



์—ฐ์‚ฐ์ž๋Š” ๋ณ€์ˆ˜์™€ ๊ฐ’์— ๋Œ€ํ•ด ์—ฐ์‚ฐ์„ ์ˆ˜ํ–‰ํ•˜๋Š” ํŠน์ˆ˜ ๊ธฐํ˜ธ์ž…๋‹ˆ๋‹ค.

1. ์‚ฐ์ˆ  ์—ฐ์‚ฐ์ž
์‚ฐ์ˆ  ์—ฐ์‚ฐ์ž๋Š” ๋”ํ•˜๊ธฐ, ๋นผ๊ธฐ, ๊ณฑํ•˜๊ธฐ์™€ ๊ฐ™์€ ์ˆ˜ํ•™ ์—ฐ์‚ฐ์„ ์ˆ˜ํ–‰ํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.

let firstScore = 12
let secondScore = 4


๋”ํ•˜๊ธฐ์™€ ๋นผ๊ธฐ

let total = firstScore + secondScore
let difference = firstScore - secondScore


๊ณฑํ•˜๊ณ  ๋‚˜๋ˆ„๊ธฐ

let product = firstScore * secondScore
let divided = firstScore / secondScore


2.๋ณตํ•ฉ ๋Œ€์ž… ์—ฐ์‚ฐ์ž

var score = 95
score -= 5  //90


3. ๋น„๊ต ์—ฐ์‚ฐ์ž
๋น„๊ต ์—ฐ์‚ฐ์ž๋Š” ๋‘ ๊ฐ’/๋ณ€์ˆ˜๋ฅผ ๋น„๊ตํ•˜๊ณ  ๋ถ€์šธ ๊ฒฐ๊ณผ(true ๋˜๋Š” false)๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด,

var a = 5, b =2
print (a > b)      // true




Operator    Meaning     Example
==     Is Equal To          3 == 5 gives us false
!=     Not Equal To     3 != 5 gives us true
>      Greater Than         3 > 5 gives us false
<      Less Than            3 < 5 gives us true
>=  Greater Than or Equal To    3 >= 5 give us false
<=  Less Than or Equal To           3 <= 5 gives us true


4. ๋…ผ๋ฆฌ ์—ฐ์‚ฐ์ž
๋…ผ๋ฆฌ ์—ฐ์‚ฐ์ž๋Š” ํ‘œํ˜„์‹์ด ์ฐธ์ธ์ง€ ๊ฑฐ์ง“์ธ์ง€ ํ™•์ธํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. ๊ทธ๋“ค์€ ์˜์‚ฌ ๊ฒฐ์ •์— ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด,

var a = 5, b = 6
print((a > 2) && (b >= 6))    // true



Operator Example      Meaning
&&       a && b    Logical AND:true only if both the operands are true

||  a || b     Logical OR:true if at least one of the operands is true
!     !a       Logical NOT:
true if the operand is false and vice-versa.



์ƒํƒœ
๋‹ค๋ฅธ ๊ฒฝ์šฐ์˜ ์˜ˆ

let firstCard = 11
let secondCard = 10

if firstCard + secondCard == 21 {
    print("Blackjack!")
} else {
    print("Regular cards")
}



  • ๊ฒฐํ•ฉ ์กฐ๊ฑด
    Swift์—๋Š” ์กฐ๊ฑด์„ ํ•จ๊ป˜ ๊ฒฐํ•ฉํ•  ์ˆ˜ ์žˆ๋Š” ๋‘ ๊ฐ€์ง€ ํŠน์ˆ˜ ์—ฐ์‚ฐ์ž๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค: &&("and"๋กœ ๋ฐœ์Œ) ๋ฐ || ("๋˜๋Š”"์œผ๋กœ ๋ฐœ์Œ).

  • ์˜ˆ์‹œ

    let age1 = 12
    let age2 = 21
    
    if age1 > 18 && age2 > 18 {
        print("Both are over 18")
    }
    
    if age1 > 18 || age2 > 18 {
        print("At least one is over 18")
    }
    


    ์‚ผํ•ญ ์—ฐ์‚ฐ์ž
    ์˜ˆ์‹œ

    let firstCard = 11
    let secondCard = 10
    print(firstCard == secondCard ? "Cards are the same" : "Cards are different")
    


    ์Šค์œ„์น˜ ๋ฌธ

    let weather = "sunny"
    
    switch weather {
    case "rain":
        print("Bring an umbrella")
    case "snow":
        print("Wrap up warm")
    case "sunny":
        print("Wear sunscreen")
    default:
        print("Enjoy your day!")
    }
    


    ๋ฒ”์œ„ ์—ฐ์‚ฐ์ž
    Swift์—๋Š” ๊ฐ’ ๋ฒ”์œ„๋ฅผ ํ‘œํ˜„ํ•˜๊ธฐ ์œ„ํ•œ ๋‹จ์ถ•ํ‚ค์ธ ๋‘ ๊ฐœ์˜ ๋ฒ”์œ„ ์—ฐ์‚ฐ์ž๊ฐ€ ํฌํ•จ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹ค์Œ ํ‘œ์—์„œ๋Š” ์ด๋Ÿฌํ•œ ๋‘ ์—ฐ์‚ฐ์ž์— ๋Œ€ํ•ด ์„ค๋ช…ํ•ฉ๋‹ˆ๋‹ค.
    1. ํ์‡„ ๋ฒ”์œ„
    (a...b)๋Š” a์—์„œ b๊นŒ์ง€์˜ ๋ฒ”์œ„๋ฅผ ์ •์˜ํ•˜๊ณ  ๊ฐ’ a์™€ b๋ฅผ ํฌํ•จํ•ฉ๋‹ˆ๋‹ค.
    ์˜ˆ์‹œ:

    1...5 gives 1, 2, 3, 4 and 5
    


    2.ํ•˜ํ”„์˜คํ”ˆ๋ ˆ์ธ์ง€

    1..< 5 gives 1, 2, 3, and 4
    

    ์ข‹์€ ์›นํŽ˜์ด์ง€ ์ฆ๊ฒจ์ฐพ๊ธฐ